[CXF] Server与Client实现方式四:JMS

【参考:http://cxf.apache.org/docs/jms-transport.html 】

【参考:http://cxf.apache.org/docs/using-the-jmsconfigfeature.html 】

【参考:http://cxf.apache.org/scalable-cxf-applications-using-jms-transport.html 】

之前介绍的Server与Client之间的通信方式都是基于HTTP,这节介绍怎么将CXF的服务发布在JMS上,通过发送、接收JMS消息来调用服务。


一、启动JMS Broker

要使用JMS,首先需要有一个JMS服务器,这里我使用Apache ActiveMQ作为JMS的服务器,可以从网上下载,也可以通过执行代码的方式启动,例如:

Java代码   收藏代码
  1. BrokerService bs = new BrokerService();  
  2. bs.setPersistenceAdapter(new MemoryPersistenceAdapter());  
  3. bs.addConnector("tcp://localhost:61616");  
  4. bs.start();  
  5. System.out.println("jms broker started");  


上面启动一个ActiveMQ的broker,地址为tcp://localhost:61616,并且消息存储在内存里。


二、定义服务接口

这个和前面介绍的内容差不多,服务接口定义如下:

Java代码   收藏代码
  1. @WebService(targetNamespace="http://localhost:8080/cxf/jms")  
  2. public interface OrderProcess {  
  3.   
  4.     @WebMethod(operationName="processOrder")  
  5.     public String processOrder( @WebParam(name="order") Order order);  
  6.       
  7. }  


三、发布服务

之前所有的服务都是以缺省的http协议发布的,这里要使用jms协议,就需要让CXF知道,这是通过一个JMSConfigFeature来实现的,如下:

Java代码   收藏代码
  1. //basic setting on service  
  2. JaxWsServerFactoryBean jwfb = new JaxWsServerFactoryBean();  
  3. jwfb.setServiceClass(OrderProcess.class);  
  4. jwfb.setServiceBean(new OrderProcessImpl());  
  5. jwfb.setAddress("jms://"); //specify jms transport  
  6.   
  7. //create jms connection factory  
  8. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();  
  9. connectionFactory.setBrokerURL("tcp://localhost:61616");  
  10.   
  11. //set target destination queue  
  12. JMSConfiguration jmsConfig = new JMSConfiguration();  
  13. jmsConfig.setTargetDestination("cxf.orderprocess.queue");  
  14. jmsConfig.setConnectionFactory(connectionFactory);  
  15.   
  16. //add feature  
  17. JMSConfigFeature jmsFeature = new JMSConfigFeature();  
  18. jmsFeature.setJmsConfig(jmsConfig);  
  19. jwfb.getFeatures().add(jmsFeature);  
  20.   
  21. //create  
  22. jwfb.create();  


首先需要指定服务接口和实现类;接下来在address变量里,需要指定的值为"jms://"开头的值,以告知cxf这里使用jms的传输协议。

然后就是普通的jms连接和配置的创建过程:指定broker的url和目标queue,接下来创建一个JMSConfigFeature,  设置它的jms配置信息,最后作为feature添加。这样这个服务就准备就诸,等待请求了。


Spring版本:


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="
  7.        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
  8.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  10.   
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  14.   
  15.     <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  16.         <property name="brokerURL" value="tcp://localhost:61616" />
  17.     </bean>
  18.   
  19.     <bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration">
  20.         <property name="connectionFactory" ref="connectionFactory" />
  21.         <property name="targetDestination" value="cxf.orderprocess.queue" />
  22.     </bean>
  23.   
  24.     <jaxws:endpoint id="orderProcess"
  25.         implementor="com.liulutu.liugang.jms.OrderProcessImpl" address="jms://">
  26.         <jaxws:features>
  27.             <bean class="org.apache.cxf.transport.jms.JMSConfigFeature">
  28.                 <property name="jmsConfig" ref="jmsConfig" />
  29.             </bean>
  30.         </jaxws:features>
  31.     </jaxws:endpoint>
  32. </beans>


四、请求服务

显然和创建服务过程一样,需要配置client的jms信息,以将请求消息发送到jms服务器上,供服务端消费:

Java代码   收藏代码
  1. //create jms connection and configuration  
  2. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();  
  3. connectionFactory.setBrokerURL("tcp://localhost:61616");  
  4.   
  5. JMSConfiguration jmsConfig = new JMSConfiguration();  
  6. jmsConfig.setTargetDestination("cxf.orderprocess.queue");  
  7. jmsConfig.setConnectionFactory(connectionFactory);  
  8.   
  9. //create feature and config it with jms configuration  
  10. JMSConfigFeature jmsFeature = new JMSConfigFeature();  
  11. jmsFeature.setJmsConfig(jmsConfig);  
  12.   
  13. //create client, and add the feature  
  14. JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();  
  15. client.setAddress("jms://"); //specify tranport type  
  16. client.getFeatures().add(jmsFeature);  
  17.   
  18. //call service  
  19. OrderProcess orderProcess = client.create(OrderProcess.class);  
  20. String s = orderProcess.processOrder(order);  
  21. System.out.println(s);  


可以看出,无论Server还是Client端,关键步骤都在于添加JMSConfigFeature,并且设置它所需的jms连接信息。


Spring版本:


点击(此处)折叠或打开

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xmlns:context="http://www.springframework.org/schema/context"
  4.     xsi:schemaLocation="
  5.        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
  6.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8.   
  9.     <import resource="classpath:META-INF/cxf/cxf.xml" />
  10.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  12.   
  13.     <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  14.         <property name="brokerURL" value="tcp://localhost:61616" />
  15.     </bean>
  16.   
  17.     <bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration">
  18.         <property name="connectionFactory" ref="connectionFactory" />
  19.         <property name="targetDestination" value="cxf.orderprocess.queue" />
  20.     </bean>
  21.   
  22.     <jaxws:client id="orderProcessClient" serviceClass="com.liulutu.liugang.jms.OrderProcess"
  23.         address="jms://">
  24.         <jaxws:features>
  25.             <bean class="org.apache.cxf.transport.jms.JMSConfigFeature">
  26.                 <property name="jmsConfig" ref="jmsConfig" />
  27.             </bean>
  28.         </jaxws:features>
  29.     </jaxws:client>
  30. </beans>


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9399028/viewspace-1822740/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9399028/viewspace-1822740/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值