Apache camel简单demo

package com.camel.helloworld;

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.http.common.HttpMessage;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;
import org.apache.log4j.PropertyConfigurator;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class APPHelloWorld01 extends RouteBuilder {

    public static void main(String[] args) throws Exception {

        PropertyConfigurator.configure("./conf/log4j.properties");
        PropertyConfigurator.configureAndWatch("./conf/log4j.properties", 1000);

        ModelCamelContext camelContext = new DefaultCamelContext();
        camelContext.start();
        camelContext.addRoutes(new APPHelloWorld01());

        synchronized (APPHelloWorld01.class) {
            APPHelloWorld01.class.wait();
        }
    }

    @Override
    public void configure() throws Exception {
        from("jetty:http://127.0.0.1:8282/doHelloWorld").process(new HttpProcessor())
                .to("log:helloworld?showExchangeId=true");
    }

    public class HttpProcessor implements Processor {

        @Override
        public void process(Exchange exchange) throws Exception {
            HttpMessage message = (HttpMessage) exchange.getIn();
            InputStream bodyStream = (InputStream) message.getBody();
            String inputContext = this.analysisMessage(bodyStream);
            bodyStream.close();

            if (exchange.getPattern() == ExchangePattern.InOut) {
                Message outMessage = exchange.getOut();
                outMessage.setBody(inputContext + " || out");
            }
        }

        private String analysisMessage(InputStream bodyStream) throws IOException {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] contextBytes = new byte[4096];
            int realLen;
            while ((realLen = bodyStream.read(contextBytes, 0, 4096)) != -1) {
                outStream.write(contextBytes, 0, realLen);
            }

            try {
                return new String(outStream.toByteArray(), "UTF-8");
            } finally {
                outStream.close();
            }
        }
    }

}

其中

 from("jetty:http://127.0.0.1:8282/doHelloWorld").process(new HttpProcessor())
                    .to("log:helloworld?showExchangeId=true");

可以直接创建一个可供调用的webservice

HttpProcessor类继承了Processor类,重写process方法,可将从该接口获得的数据进行处理,然后再写入log中,下面是测试代码:

package com.camel.helloworld;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.PropertyConfigurator;

public class TestHttpClient {

	public static void main(String[] args) throws Exception {

		PropertyConfigurator.configure("./conf/log4j.properties");
		PropertyConfigurator.configureAndWatch("./conf/log4j.properties", 1000);

		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost("http://127.0.0.1:8282/doHelloWorld");

		// 添加参数
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("fu", "fu"));

		httpPost.setHeader("ContentType", "UTF-8");
		UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
		httpPost.setEntity(urlEntity);

		HttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();

		InputStream inputStream = entity.getContent();
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

		// 读取HTTP请求内容
		String buffer = null;
		StringBuffer sb = new StringBuffer();
		while ((buffer = br.readLine()) != null) {
			// 在页面中显示读取到的请求参数
			sb.append(buffer + "\n");
		}

		System.out.println("接收返回数据:\n" + URLDecoder.decode(sb.toString().trim(), "utf-8"));

		System.out.println("Login form get: " + response.getStatusLine() + entity.getContent());

}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值