apache camel helloWorld

ProcessorImpl

package com.jxt.camelDemo;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.http.common.HttpMessage;
import org.apache.camel.util.json.JsonObject;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Map;

public class ProcessorImpl implements Processor {
    public void process(Exchange exchange) throws Exception {
        HttpServletRequest httpServletRequest = exchange.getIn().getBody(HttpServletRequest.class);
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        System.out.println(parameterMap.toString());
        HttpMessage message = (HttpMessage)exchange.getIn();
        InputStream bodyStream = (InputStream)message.getBody();
        String inputContext =  this.analysisMessage(bodyStream);
        bodyStream.close();
        System.out.println(inputContext);
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH-mm-ss");
        JsonObject jsonObject = new JsonObject();
        jsonObject.put("errorCode",0);
        jsonObject.put("msg","OK");
        exchange.getMessage().setBody(jsonObject.toJson());
    }

    private String analysisMessage(InputStream bodyStream) throws IOException, IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] contextBytes = new byte[outStream.size()];
        int realLen;
        while((realLen = bodyStream.read(contextBytes , 0 ,outStream.size())) != -1) {
            outStream.write(contextBytes, 0, realLen);
        }
// 返回从Stream中读取的字串
        try {
            return new String(outStream.toByteArray() , "UTF-8");
        } finally {
            outStream.close();
        }
    }
}
MyRoute
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;

public class MyRoute extends RouteBuilder {

    public void configure() throws Exception {
        from("timer://queryAward?period=5s&repeatCount=1")
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                .to("http://39.98.60.120:5050/getJvmInfo").process(new ProcessorImpl())
                .to("log:getJvmInfo?showExchangeId=false");
    }
}
DemoMain
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

public class DemoMain {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        MyRoute route = new MyRoute();
        context.addRoutes(route);
        context.start();
        synchronized (DemoMain.class) {
            DemoMain.class.wait();
        }
        context.stop();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值