`

Camel的direct,Bean测试

 
阅读更多

//处理Bean

 

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 showBody(Exchange exchange) throws Exception { 
        System.out.println("\r\n\r\n");
        Message in=exchange.getIn();
        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 void showHeads(Map<String, Object> h){
        System.out.println("\r\n\r\n");
        System.out.println("showHeads--> heads="+h);
    }
   
    public Request parseRequest(Exchange exchange,String body) {
        System.out.println("\r\n\r\n");
        Message in=exchange.getIn();
        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  String process(Request req) {
        System.out.println("\r\n\r\n");
        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("\r\n\r\n");
        System.out.println("response-->"+html); 
        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("\r\n\r\n");
        System.out.println("toHtml--> object="+r); 
        return String.valueOf(r);
    }
   
    /*
    -->  parse(request) //got head and body
    -->  response=route(request) //call remote system  by head[system_id][service_id][method_id]
    -->  write_response(response)
   
    配置文件名为:system_id_value.ini, 如:opg.ini
   
    发起方:在head提供 system_id, service_id,method_id 三个参数,
         esb在收到请求后,将请求转发到配置的 地址
        system_url=  //远程的接口 地址
        uri_${service_id}_${method_id}=  //远程方法uri, 和 system_url 组成完整的接口url
        head_dealer=
        body_dealer=
       
    system_id=opg
    service_url=http://127.0.0.1/opg //rmi, soa ,
    method_uri=head    //或者  url即将  /service_id/method_id 加到  service_url 后面
    request_type=json  //或者 map , 请求的数据格式:目标需要的 请求数据格式 json 或 httpRequest的 Map
    response_type=json //text, 返回的数据格式
   
    service_id
   
     */
   
}

camel-config.xml 主要内容:

 

<camelContext id="x2" xmlns="http://camel.apache.org/schema/spring">
    <route id="enter">
       <from uri="servlet:time"/>
       <to uri="bean:testBean?method=showHeads(${in.headers})"/>
       <to uri="direct:toProcess"/> 
    </route>
   
    <route>
       <from uri="direct:toProcess"/>
       <to uri="bean:testBean?method=showBody(${exchange})"/>
       <to uri="bean:testBean?method=parseRequest(${exchange},${body})"/> 
       <to uri="bean:testBean?method=process(${body})"/>
       <to uri="bean:testBean?method=response(${exchange},${body})"/>
    </route>   
    </camelContext>

 

用浏览器访问: http://127.0.0.1:8080/esb/camel/time 可获得系统的时间。

本测试主要体验了: direct 和 bean , exchange 的基本用法。

 

下面是用  karaf 过程的一些记录:

 

//注册 camel feature 到 karaf
           feature:repo-add mvn:org.apache.camel.karaf/apache-camel/2.17.1/xml/features
          
      //安装 camel 到 karaf
           feature:install camel
          
           feature:install -v camel-blueprint
          
      //查看camel运行状态
           feature:list | grep camel
      //查看 repo
           feature:repo-list
      //加  camel-blueprint
           feature:install camel-blueprint
          
      mvn compile
      mvn install  / mv clean install
     
      bundle:install mvn:com.packt.camel/chapter3/1.0-SNAPSHOT
      bundle:list
      bundle:start id
      bundle:stop id
      camel:context-list
      bundle:uninstall chapter3
     
      feature:install camel-jetty

 

 

 

0
2
分享到:
评论
5 楼 白云天 2016-05-31  
export jars to local directory :
mvn dependency:copy-dependencies  --导出到Project的targed/dependency 下面
mvn dependency:copy-dependencies -DoutputDirectory=lib  --导出到lib文件夹
4 楼 白云天 2016-05-24  
location /esb/ {
         proxy_pass http://172.19.6.150:89/esb/;
}
3 楼 白云天 2016-05-23  
centos 中添加 shell_script.sh 为开机启动,
ln -s path-to-shell_script.sh /etc/init.d/service-name
chkconfig –add  service-name
2 楼 白云天 2016-05-17  
How do I let Jetty match wildcards

By default Jetty will only match on exact uri's. But you can instruct Jetty to match prefixes. For example
from("jetty://0.0.0.0:8123/foo").to("mock:foo");

In the route above Jetty will only match if the uri is an exact match, so it will match if you enter
http://0.0.0.0:8123/foo but not match if you do http://0.0.0.0:8123/foo/bar.

So if you want to enable wildcard matching you do as follows:
from("jetty://0.0.0.0:8123/foo?matchOnUriPrefix=true").to("mock:foo");

So now Jetty matches any endpoints with starts with foo.

To match any endpoint you can do:
from("jetty://0.0.0.0:8123?matchOnUriPrefix=true").to("mock:foo");
1 楼 白云天 2016-05-16  
Maven 的安装:

[root@localhost ~]# wget http://mirror.cc.columbia.edu/pub/
  software/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz
[root@localhost ~]# tar xzf apache-maven-3.1.1-bin.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local
[root@localhost ~]# ln -s apache-maven-3.1.1 maven

当然,解压完下载下来的maven包是现在还不能启用,需要在PATH里面设置一下路径,如下:

[root@localhost ~]# vim /etc/profile.d/maven.sh
export MAVEN_HOME=/usr/local/maven
export PATH=${MAVEN_HOME}/bin:${PATH}

设置好Maven的路径之后,需要运行下面的命令

[root@localhost ~]# source /etc/profile.d/maven.sh

使得上面设置的环境变量立即生效。

wget http://mirror.bit.edu.cn/apache/karaf/4.0.5/apache-karaf-4.0.5.tar.gz

相关推荐

Global site tag (gtag.js) - Google Analytics