فهرست منبع

更新hos-camel,预留了gateway接口

Airhead 8 سال پیش
والد
کامیت
262250a099

+ 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());
    }
}