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就会将它作为结果返回给调用方。