Apache ServiceMix集成了Camel、Camel-CXF,可以轻松地发布Web Service。
与传统的通过Servlet发布web Service的方式不同,我们可以通过Camel路由分别处理不同的web service调用。以下是一个简单的例子:
我们首先看看以下服务接口定义:
package com.ponder.ws;
public interface IService{
public long add(long p1,long p2);
public long sub(long p1,long p2);
}
接着看看blueprint:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:cxfcore="http://cxf.apache.org/blueprint/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd"
>
<bean id="addProcessor" class="com.ponder.ws.Impl.ServiceAdd"/>
<bean id="subProcessor" class="com.ponder.ws.Impl.ServiceSub"/>
<camel-cxf:cxfEndpoint id="IServiceWS"
address="http://0.0.0.0:8000/ws/IService"
serviceClass="com.ponder.ws.IService">
<camel-cxf:properties>
<entry key="dataFormat" value="POJO"/>
</camel-cxf:properties>
</camel-cxf:cxfEndpoint>
<camelContext id="ServiceCamelContext" xmlns="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<route id="IServiceRoute">
<from uri="IServiceWS" />
<log message="Request:${in.header.operationName}"/>
<choice>
<when>
<simple>${in.header.operationName} == 'add'</simple>
<to uri="bean:addProcessor"/>
</when>
<when>
<simple>${in.header.operationName} == 'sub'</simple>
<to uri="bean:subProcessor"/>
</when>
</choice>
</route>
</camelContext>
再看看addProcessor的写法:
package com.ponder.ws.Impl;
... ...
public class ServiceAdd implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
MessageContentsList mclist = exchange.getIn().getBody(MessageContentsList.class);
long p1=(long)mclist.get(0);
long p2=(long)mclist.get(1);
long result=p1+p2;
exchange.getOut().setBody(result);
}
}
subProcessor类似。
我们从blueprint里可以看到首先是两个bean的定义,不解释;
然后我们有一段:
<camel-cxf:cxfEndpoint id="IServiceWS"
address="http://0.0.0.0:8000/ws/IService"
serviceClass="com.ponder.ws.IService">
<camel-cxf:properties>
<entry key="dataFormat" value="POJO"/>
</camel-cxf:properties>
</camel-cxf:cxfEndpoint>
这里定义了一个Camel的endpoint,是Camel-CXF component的endpoint,component是camel的组件定义,你可以把它看成是endpoint Factory。这里实际上就是创建并配置了一个cxf endpoint,它负责接收客户端的请求,并将请求内容按Camel-CXF的规则填成一个Exchange,在后续的CamelContext里定义的路由就负责处理这个Exchange。
<route id="IServiceRoute">
<from uri="IServiceWS" />
<log message="Request:${in.header.operationName}"/>
<choice>
<when>
<simple>${in.header.operationName} == 'add'</simple>
<to uri="bean:addProcessor"/>
</when>
<when>
<simple>${in.header.operationName} == 'sub'</simple>
<to uri="bean:subProcessor"/>
</when>
</choice>
</route>
以上这段路由就是根据Exchange里的in.header.operationName判断,如果operationName为“add”的话,就交由addProcessor处理。
再看看addProcessor(也就是ServiceAdd的实例)里的processor方法从Exchange里的in.body里得到一个Object,根据Camel-cxf的定义,这个Object是个MessageContentsList,从这里可以拿到相应的参数p1和p2的值,处理后,将结果放到out.body里,camel-cxf的endpoint就会将它作为结果返回给调用方。
分享到:
相关推荐
servicemix代理web service
Apache ServiceMix Specs :: JAXP API 1.3
servicemix:Apache ServiceMix
org.apache.servicemix.bundles.xmlbeans-2.4.0_1.jar
servicemix5:Apache ServiceMix 5的镜像
gooddata-http-client.zip,能够处理gooddata身份验证的http客户端处理gooddata身份验证的专用java客户端
java-game-server.zip,nadron是一个基于socket的高速java游戏服务器,使用netty和mike rettig的jetlang编写。它专门针对基于网络的多人游戏进行了调整,并支持TCP和UDP网络协议。
mysql-binlog-connector-java.zip,mysql二进制日志连接器mysql二进制日志连接器
smaller-dev-server.zip,一个开发服务器,利用较小的程序为本地
fuse esb 第二部分。总共分割了五分
servicemix-bundles:Apache ServiceMix捆绑包的镜像
官网下的实在太慢了................................
NULL 博文链接:https://newjava-sina-cn.iteye.com/blog/2359797
How does ServiceMix compare to Tuscany or SCA
Servicemix做代理服务器发布WebService,Login登录实际代码。对应博客中ServiceMix做代理服务器发布WebService(附带案例源码) 该篇例子,需要的可以去看看,这个是源码。
apache-servicemix-3.3-src.zip
使用Servicemix(ESB)发布一个外部的WebService
创建3个表示形式: 包含文档webapp的战争文件pdf文件(位于target/sitegen/manual.pdf ) 使用相同模板(位于target/sitegen )生成的静态网站将文档发布到网站如果您是Apache ServiceMix提交者,则可以使用以下命令
kiln-java-adaptor.zip,回转窑测井服务适配器回转窑测井服务适配器
embedly-java.zip,用于api的嵌入式客户端库和用于embed.ly api的projava客户端