【参考: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的服务器,可以从网上下载,也可以通过执行代码的方式启动,例如:
BrokerService bs = new BrokerService(); bs.setPersistenceAdapter(new MemoryPersistenceAdapter()); bs.addConnector("tcp://localhost:61616"); bs.start(); System.out.println("jms broker started");
上面启动一个ActiveMQ的broker,地址为tcp://localhost:61616,并且消息存储在内存里。
二、定义服务接口
这个和前面介绍的内容差不多,服务接口定义如下:
@WebService(targetNamespace="http://localhost:8080/cxf/jms") public interface OrderProcess { @WebMethod(operationName="processOrder") public String processOrder( @WebParam(name="order") Order order); }
三、发布服务
之前所有的服务都是以缺省的http协议发布的,这里要使用jms协议,就需要让CXF知道,这是通过一个JMSConfigFeature来实现的,如下:
//basic setting on service JaxWsServerFactoryBean jwfb = new JaxWsServerFactoryBean(); jwfb.setServiceClass(OrderProcess.class); jwfb.setServiceBean(new OrderProcessImpl()); jwfb.setAddress("jms://"); //specify jms transport //create jms connection factory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("tcp://localhost:61616"); //set target destination queue JMSConfiguration jmsConfig = new JMSConfiguration(); jmsConfig.setTargetDestination("cxf.orderprocess.queue"); jmsConfig.setConnectionFactory(connectionFactory); //add feature JMSConfigFeature jmsFeature = new JMSConfigFeature(); jmsFeature.setJmsConfig(jmsConfig); jwfb.getFeatures().add(jmsFeature); //create jwfb.create();
首先需要指定服务接口和实现类;接下来在address变量里,需要指定的值为"jms://"开头的值,以告知cxf这里使用jms的传输协议。
然后就是普通的jms连接和配置的创建过程:指定broker的url和目标queue,接下来创建一个JMSConfigFeature, 设置它的jms配置信息,最后作为feature添加。这样这个服务就准备就诸,等待请求了。
Spring版本:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration"> <property name="connectionFactory" ref="connectionFactory" /> <property name="targetDestination" value="cxf.orderprocess.queue" /> </bean> <jaxws:endpoint id="orderProcess" implementor="com.liulutu.liugang.jms.OrderProcessImpl" address="jms://"> <jaxws:features> <bean class="org.apache.cxf.transport.jms.JMSConfigFeature"> <property name="jmsConfig" ref="jmsConfig" /> </bean> </jaxws:features> </jaxws:endpoint> </beans>
四、请求服务
显然和创建服务过程一样,需要配置client的jms信息,以将请求消息发送到jms服务器上,供服务端消费:
//create jms connection and configuration ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("tcp://localhost:61616"); JMSConfiguration jmsConfig = new JMSConfiguration(); jmsConfig.setTargetDestination("cxf.orderprocess.queue"); jmsConfig.setConnectionFactory(connectionFactory); //create feature and config it with jms configuration JMSConfigFeature jmsFeature = new JMSConfigFeature(); jmsFeature.setJmsConfig(jmsConfig); //create client, and add the feature JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean(); client.setAddress("jms://"); //specify tranport type client.getFeatures().add(jmsFeature); //call service OrderProcess orderProcess = client.create(OrderProcess.class); String s = orderProcess.processOrder(order); System.out.println(s);
可以看出,无论Server还是Client端,关键步骤都在于添加JMSConfigFeature,并且设置它所需的jms连接信息。
Spring版本:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration"> <property name="connectionFactory" ref="connectionFactory" /> <property name="targetDestination" value="cxf.orderprocess.queue" /> </bean> <jaxws:client id="orderProcessClient" serviceClass="com.liulutu.liugang.jms.OrderProcess" address="jms://"> <jaxws:features> <bean class="org.apache.cxf.transport.jms.JMSConfigFeature"> <property name="jmsConfig" ref="jmsConfig" /> </bean> </jaxws:features> </jaxws:client> </beans>
然后在代码里:
相关推荐
cxf spring server client demo.rar soap webservice
本实例是基于CXF的一个客户端的实现,使用maven生成客户端代码,集成spring配置客户端实现类,访问外部接口。“CXF契约优先开发方式”中的实例为服务器端代码。
Maven坐标:org.apache.cxf:cxf-rt-rs-client:3.0.1; 标签:cxf、rt、apache、client、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性...
cxfserver+spring+mybatis
cxf+spring+client+springmvc
关于java CXF 一个简单的例子,所用jar包请自己下载 谢谢
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验
使用webservice的cxf做server与client例子
CXF实现SSL安全验证,实现https的WebService
CXF 编写 WebServer DEMO,使用 CXF 工具编写 WebServer
2、将cxf_jar包文件夹下所有包,拷至MyCXFService与MyCXFClient项目WebRoot\WEB-INF\lib\cxf_2.7.8 目录下(注意:两个项目都需要这些jar包) 3、将两个项目都导入myEclipse 4、将MyCXFService与MyCXFClient 一起...
使用spring集成cxf,在两个web ...server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外所有jar 内附 使用文档,数据库脚本
CXF源码:CXF_Spring源码 CXF源码:CXF_Spring源码
3.抛弃cxf默认的log方式,使用log4j的方式,方便以后整合到其他app中而不改变之前系统log的方式。 答谢: 关于cxf的深入研究,我发现国人翻译的东西很少,我想尽自己的微薄之力能给大家带些有用的东西,如果有什么...
cxf实现soap webservice服务,服务端\客户端均与spring结合使用,多种方式实现,花了三天时间弄懂,还有许多不懂的地方,欢迎大家交流.参考我的博文:http://blog.csdn.net/qq_33764491/article/details/78831010
使用CXF开发Web Service,包含服务器端和客户端
cxf 所有jar 和环境配置详细说明,一看就会好运行环境
Maven坐标:org.apache.cxf:cxf-core:3.0.1; 标签:cxf、core、apache、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中...
CXFServer 已实现功能 1. 集成 spring 2. 集成 WSS4J 3. mtom 功能 4. 基本数据类型service接口 5. 复杂数据类型service接口
CXF视频:1、使用CXF实现简单的HelloWorld