Browse Source

Merge branch 'master' of http://192.168.1.220:10080/esb/esb

demon 8 năm trước cách đây
mục cha
commit
fde2480014

+ 1 - 0
hos-camel/hos-camel.iml

@ -129,6 +129,7 @@
    <orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.6.2" level="project" />
    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-core:2.6.2" level="project" />
    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.5.2" level="project" />
    <orderEntry type="library" name="Maven: io.springfox:springfox-swagger2:2.4.0" level="project" />
    <orderEntry type="library" name="Maven: io.swagger:swagger-annotations:1.5.6" level="project" />
    <orderEntry type="library" name="Maven: io.swagger:swagger-models:1.5.6" level="project" />

+ 4 - 1
hos-camel/src/main/java/api/processor/ApiProcessor.java

@ -1,4 +1,4 @@
package api.processor;
package apiasync.processor;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
@ -37,6 +37,9 @@ public class ApiProcessor implements Processor {
     * @return
     */
    private String analysisMessage(InputStream bodyStream) throws IOException {
        if (bodyStream == null) {
            return "";
        }
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] contextBytes = new byte[4096];
        int realLen;

+ 16 - 0
hos-camel/src/main/java/apiasync/route/ApiRouteBulider.java

@ -0,0 +1,16 @@
package apiasync.route;
import apiasync.processor.ApiProcessor;
import org.apache.camel.builder.RouteBuilder;
/**
 * @author Airhead
 * @since 2016-11-13
 */
public class ApiRouteBulider extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty:http://localhost:9090/api/v1/invokeAsync").routeId("invokeAsync")
                .process(new ApiProcessor());
    }
}

+ 57 - 0
hos-camel/src/main/java/apisync/processor/ApiProcessor.java

@ -0,0 +1,57 @@
package apisync.processor;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.http.common.HttpMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * @author Airhead
 * @since 2016-11-13
 */
public class ApiProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        // 因为很明确消息格式是http的,所以才使用这个类
        // 否则还是建议使用org.apache.camel.Message这个抽象接口
        HttpMessage message = (HttpMessage) exchange.getIn();
        InputStream bodyStream = (InputStream) message.getBody();
        String inputContext = this.analysisMessage(bodyStream);
        bodyStream.close();
        // 存入到exchange的out区域
        if (exchange.getPattern() == ExchangePattern.InOut) {
            Message outMessage = exchange.getOut();
            outMessage.setBody("hello.123," + inputContext);
        }
    }
    /**
     * 从stream中分析字符串内容
     *
     * @param bodyStream
     * @return
     */
    private String analysisMessage(InputStream bodyStream) throws IOException {
        if (bodyStream == null) {
            return "";
        }
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] contextBytes = new byte[4096];
        int realLen;
        while ((realLen = bodyStream.read(contextBytes, 0, 4096)) != -1) {
            outStream.write(contextBytes, 0, realLen);
        }
        // 返回从Stream中读取的字串
        try {
            return new String(outStream.toByteArray(), "UTF-8");
        } finally {
            outStream.close();
        }
    }
}

+ 3 - 3
hos-camel/src/main/java/api/route/ApiRouteBulider.java

@ -1,6 +1,6 @@
package api.route;
package apisync.route;
import api.processor.ApiProcessor;
import apisync.processor.ApiProcessor;
import org.apache.camel.builder.RouteBuilder;
/**
@ -10,7 +10,7 @@ import org.apache.camel.builder.RouteBuilder;
public class ApiRouteBulider extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty:http://localhost:9090/api/v1").routeId("api")
        from("jetty:http://localhost:9090/api/v1/invokeSync").routeId("invokeSync")
                .process(new ApiProcessor());
    }
}

+ 6 - 0
sdk/java/hos-client/pom.xml

@ -18,6 +18,12 @@
            <artifactId>jackson-databind</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

+ 3 - 1
sdk/java/hos-client/src/main/java/com/yihu/hos/client/BrokerServer.java

@ -105,7 +105,9 @@ class BrokerServer {
            ExecutorService executorService = Executors.newCachedThreadPool();
            Future<ClientResponse> future = executorService.submit(new AsyncCall(clientRequest));
            ClientResponse clientResponse = future.get();
            callback.onReturn(clientResponse);
            if (callback != null) {
                callback.onReturn(clientResponse);
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

+ 56 - 0
sdk/java/hos-client/src/test/java/com/yihu/hos/client/BrokerServerClientTest.java

@ -0,0 +1,56 @@
package com.yihu.hos.client;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
 * @author Airhead
 * @since 2016/11/24.
 */
public class BrokerServerClientTest {
    @Test
    public void invokeSync() throws Exception {
        BrokerServerClient brokerServerClient = new BrokerServerClient("127.0.0.1");
        ClientRequest clientRequest = new ClientRequest();
        clientRequest.setModule("serviceName");
        clientRequest.setMethod("methodName");
        Map<String, String> args = new HashMap<>();
        args.put("arg1", "value1");
        args.put("arg2", "value2");
        clientRequest.setArgs(args);
        ClientResponse clientResponse = brokerServerClient.invokeSync(clientRequest);
        if (clientResponse.getStackTrace() != null) {
            throw new Exception(clientResponse.getError());
        }
        System.out.println(clientResponse.getResult());
    }
    @Test
    public void invokeAsync() throws Exception {
        BrokerServerClient brokerServerClient = new BrokerServerClient("127.0.0.1");
        ClientRequest clientRequest = new ClientRequest();
        clientRequest.setModule("serviceName");
        clientRequest.setMethod("methodName");
        Map<String, String> args = new HashMap<>();
        args.put("arg1", "value1");
        args.put("arg2", "value2");
        clientRequest.setArgs(args);
        brokerServerClient.invokeAsync(clientRequest, null);
    }
    class AsyncCallback implements ResultCallback<ClientResponse>{
        @Override
        public void onReturn(ClientResponse result) {
            if (result.getStackTrace() != null) {
                System.out.println(result.getError().getMessage());
            }
            System.out.println(result.getResult());
        }
    }
}