首页 > 其他 > 详细

Camel Bean的简单例子

时间:2016-05-13 06:00:54      阅读:236      评论:0      收藏:0      [点我收藏+]

web.xml

?

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
???????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
???????? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

? <display-name>My Web Application</display-name>

? <!-- location of spring xml files -->
? <context-param>
??? <param-name>contextConfigLocation</param-name>
??? <param-value>classpath:camel-config.xml</param-value>
? </context-param>

? <!-- the listener that kick-starts Spring -->
? <listener>
??? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>

? <!-- Camel servlet -->
? <servlet>
??? <servlet-name>CamelServlet</servlet-name>
??? <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
??? <load-on-startup>1</load-on-startup>
? </servlet>

? <!-- Camel servlet mapping -->
? <servlet-mapping>
??? <servlet-name>CamelServlet</servlet-name>
??? <url-pattern>/camel/*</url-pattern>
? </servlet-mapping>

</web-app>

?

camel-config.xml

?

<?xml version="1.0" encoding="UTF-8"?>
?
<beans xmlns="http://www.springframework.org/schema/beans"
?????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?????? xmlns:camel="http://camel.apache.org/schema/spring"
?????? xsi:schemaLocation="
???????? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
???????? http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
?
? <bean id="testBean" class="com.suneee.common.TestBean"/>

? <camelContext id="x1" xmlns="http://camel.apache.org/schema/spring">
??? <route id="helloRoute">
????? <!-- incoming requests from the servlet is routed -->
????? <from uri="servlet:hello"/>
????? <choice>
??????? <when>
????????? <header>name</header>
????????? <transform>
??????????? <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
????????? </transform>
??????? </when>
??????? <otherwise>
????????? <transform>
??????????? <constant>Add a name parameter to uri, eg ?name=foo</constant>
????????? </transform>
??????? </otherwise>
????? </choice>
??? </route>
? </camelContext>

? <camelContext id="x2" xmlns="http://camel.apache.org/schema/spring">
??? <route id="helloRoute">
????? <!-- incoming requests from the servlet is routed -->
????? <from uri="servlet:show"/>
??? ? <!--
??? ? <to uri="bean:testBean?method=readBody(${body})"/>
??? ? -->
??? ? <!--? 从 Message / Exchange.getIn() 读取 body -->
??? ? <to uri="bean:testBean?method=showBody(*)"/>
??? ?
??? ? <!--? 从 Message / Exchange.getIn() 构造 Request 对象 -->
??? ? <to uri="bean:testBean?method=parseRequest(*,${body})"/>

????? <!--? 处理? Request 对象, 返回 生产 html 内容 的 主要数据 -->
??? ? <to uri="bean:testBean?method=process(${body})"/>?
????
??? ? <!--? 更加主要数据 生成 html 内容 -->
????? <to uri="bean:testBean?method=toHtml(${body})"/>
???
??? ? <!-- 输出内容 通过 Exchange 进行输出 html 内容, 设置 http-head -->
??? ? <to uri="bean:testBean?method=response(*,${body})"/>

??? ? <!--直接返回 html 内容,将会输出到 http客户端
??? ? <to uri="bean:testBean?method=toHtml(${body})"/>
??? ? -->

??? ? <!--
??? ? <process ref="helloProcessor"/>
??? ? -->
??? </route>
? </camelContext>

</beans>

?


import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.Message;

public class TestBean {
???
??? public TestBean(){
??? ??? super();
??? }
???
??? public String readBody(Object body) throws Exception {
??? ??? System.out.println("readBody-->"+body);
??? ??? byte []bs=read((ByteArrayInputStream)body);?
??? ??? String msg=new String(bs,"utf-8");
??? ??? System.out.println("Input="+msg);
??? ??? return msg;
??? }
???
??? public String showBody(Message in) throws Exception {?
??? ??? byte []bs=read((ByteArrayInputStream)in.getBody());?
??? ??? String msg=new String(bs,"utf-8");
??? ??? System.out.println("showBody-->"+msg);?
??? ??? return msg;
??? }
???
??? private byte[] read(ByteArrayInputStream in){
??? ??? int len=in.available();
??? ??? byte []data=new byte[len];
??? ??? in.read(data,0,data.length);
??? ??? return data;
??? }
???
??? public Request parseRequest(Message in,String body) {
??? ??? Map<String, Object> ms=in.getHeaders();
??? ??? Request r=new Request();
??? ??? r.setHead(ms);
??????? System.out.println("parseRequest--> heads="+ms+",body="+body);
??????? r.setBody(body);
??? ??? return r;
??? }
???
??? public? Object process(Request req) {
??????? System.out.println("process--> heads="+req.getHead()+",body="+req.getBody());
??????? String s="你好,系统时间为:"+new Date();
??? ??? return s;
??? }
??? public? void response(Exchange out,String html) {
??? ??? System.out.println("setHeads-->");?
??? ??? //out.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html" + "; charset=utf-8");
??? ??? out.getOut().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream" + "; charset=utf-8");?
??? ??? out.getOut().setBody(html);
??? }
???
??? public? String toHtml(Object r) {
??????? System.out.println("toHtml--> object="+r);?
??? ??? return String.valueOf(r);
??? }
???
???
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class TestHttp {

??? public static void main(String[] args) throws Exception {
??? ??? String to = "http://127.0.0.1:8080/esb/camel/show";
??? ??? String param="name=中国&sex=100&date=20178091&rid=make_id_001";
??? ???
??? ??? URL url = new URL(to);
??? ??? PrintWriter out = null;
??? ??? BufferedReader in = null;
??? ??? String result = "";

??? ??? // 打开和URL之间的连接
??? ??? URLConnection conn = url.openConnection();
??? ??? // 设置通用的请求属性
??? ??? conn.setRequestProperty("accept", "*/*");
??? ??? conn.setRequestProperty("connection", "Keep-Alive");
??? ??? conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
??? ??? // 发送POST请求必须设置如下两行
??? ??? conn.setDoOutput(true);
??? ??? conn.setDoInput(true);
??? ??? // 获取URLConnection对象对应的输出流
??? ??? out = new PrintWriter(conn.getOutputStream());
??? ??? // 发送请求参数
??? ??? out.print(param);
??? ??? // flush输出流的缓冲
??? ??? out.flush();
??? ??? // 定义BufferedReader输入流来读取URL的响应
??? ??? in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
??? ??? String line;
??? ??? while ((line = in.readLine()) != null) {
??? ??? ??? result += line;
??? ??? }
??? ??? System.out.println(result);
??? }
}

Camel Bean的简单例子

原文:http://iwin.iteye.com/blog/2297180

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!