Apache CXF结合camel blueprint demo

cxf框架是apache的web service框架,提供简单易用的webservice功能,同时可以实现restful风格的soa。

camel也是apache出品,他将request到response以route的形式进行处理,同时将response返回给请求端。这一点类似前端emberjs的思路,每一个请求都通过一个route作为入口,然后根据XML的配置经过每个处理,最后将结果返回。

为了方便实用和配置,camel同时提供了blueprint的配置方式,将请求配置到xml文件中,从而可以更加清晰的了解访问路由,利于维护。

本例是基于jboss fuse, karaf的osgi程序。

  1. java程序代码:
  2. @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    public interface LoginEndPoint{
    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/addCustomer")
    public String addCustomer(CustomerVo cust);
    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/loginCustomer/{name}")
    public CustomerVo loginCustomer(@PathParam("name")String name);
    }
     具体business实现
    public class LoginEndPointImpl implements LoginEndPoint{
    
    @Override
    public String addCustomer(CustomerVo cust){
    
    return null;
    }
    
    @Override
    public CustomerVo loginCustomer(String name){
    
    return null;
    }
    }
     
    public interface Processor{
    
    public Object process(String name);
    }
     
    Processor 实现
    public LoginCustomerProcessor implements Processor{

    public Object process(String name){

    CustomerVo cust=new CustomerVo();
    cust.setName(name);
    cust.setStatus("Y");
    return cust;
    }
    }
     
  3. blueprint.xml默认配置路径在./resources/OSGI-INF/blueprint/blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws" 
xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" 
    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://camel.apache.org/schema/blueprint
http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
">

<bean id="loginService" class="com.XX.endpoint.LoginEndPointImpl"/>
<bean id="loginProcessor" class="com.XX.processor.LoginCustomerProcessor"/>

<cxf:rsServer id="rsServer" address="login" serviceClass="com.XX.endpoint.LoginEndPointImpl"/>

<camelContext id="loginContext" xmlns="http://camel.apache.org/schema/blueprint">
<route id="login">
<from uri="cxfrs://bean://rsServer"/>
<recipientList>
  <simple>direct:${header.operationName}</simple>
</recipientList>
</route>

<route id="loginCustomer">
  <from uri="direct:loginCustomer"/>
  <process ref="loginProcessor"></process>
</route>
</camelContext>
</blueprint>

相关推荐