Camel Bean的简单例子

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值