`
liugang594
  • 浏览: 981482 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[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的服务器,可以从网上下载,也可以通过执行代码的方式启动,例如:

		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>

 

然后在代码里:
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/org/talend/esb/liugang/jms/client.xml");
		OrderProcess bean = context.getBean("orderProcessClient", OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		String processOrder = bean.processOrder(order);
		System.out.println(processOrder);
 
分享到:
评论
2 楼 wangyudong 2017-11-27  
由CXF实现的微服务需要有比较好的工具去测试RESTful API,很多REST Client是不支持自动化测试RESTful API,也不支持自动生成API文档.
之前习惯用一款名字为 WisdomTool REST Client,支持自动化测试RESTful API,输出精美的测试报告,并且自动生成精美的RESTful API文档。
轻量级的工具,功能却很精悍哦!

https://github.com/wisdomtool/rest-client

Most of REST Client tools do not support automated testing.

Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.

Lightweight tool with very powerful features!

https://github.com/wisdomtool/rest-client
1 楼 panamera 2016-08-02  
你好。可以提供maven pom配置是怎么配置的?不知道你使用cxf和spring版本

相关推荐

    apache-cxf-3.2.1

    - **多样化的绑定**:CXF支持SOAP over HTTP、RESTful服务、JMS消息传递等多种通信方式,可以根据项目需求灵活选择。 - **JAX-WS和JAX-RS支持**:CXF完全实现了Java API for XML Web Services (JAX-WS) 和 Java ...

    webservice(cxf)+ajax请求,客户端和服务器端

    压缩包中的 "server" 文件可能包含了服务器端的代码,如 CXF 服务实现、数据库配置、Maven/Gradle 构建文件等。而 "client" 文件则可能包含客户端应用的代码,如 HTML、CSS、JavaScript 文件以及调用 Web 服务的...

    cxf开发教程2

    "CXF开发教程2"旨在帮助开发者深入理解CXF的客户端和服务端开发,通过实践"Cxf_Client"和"Cxf_Server"示例,可以熟练掌握CXF的使用,为实际项目中的Web服务开发打下坚实基础。无论你是初学者还是经验丰富的开发者,...

    WebService发布框架CXF的源码

    - 服务器端(Server):处理来自客户端的请求,提供服务实现。 - 数据绑定(Data Binding):将XML消息转换为Java对象,反之亦然,如JAXB、XSD等。 - 消息传递(Message Transport):处理HTTP、HTTPS、JMS等传输...

    axis1 server client 程序

    标题 "Axis1 Server Client程序" 暗示我们即将探讨的是一个基于Axis1框架的Web服务客户端和服务端的实现。Axis1是Apache软件基金会开发的一个开源项目,它主要用于创建、部署和使用Web服务。这个框架基于Java语言,...

    利用mule服务总线代理cxf服务源码

    3. "CXFServer":这是CXF服务的实现,包含了服务的业务逻辑和接口定义。 4. ".metadata":这是Eclipse或Anypoint Studio工作空间的配置信息,包含了项目设置和偏好。 5. ".mule":这是Mule项目配置文件所在的目录...

    xfire 1.2.6 server

    《xfire 1.2.6 server与cxf 1.2.6 client:构建高效服务交互》 在IT行业中,服务化已经成为企业系统架构的重要组成部分。xfire和CXF是两个广泛使用的Java Web服务框架,它们允许开发人员创建、发布和消费Web服务。...

    cxf:Apache CXF

    这些服务可以说各种协议,例如SOAP,XML / HTTP,RESTful HTTP或CORBA,并且可以通过各种传输方式(例如HTTP,JMS或JBI)工作。 CXF包含广泛的功能集,但主要集中在以下领域: Web服务标准支持:CXF支持各种Web服务...

    android用xfire操作sqlserver源完整代码+数据库脚本

    总的来说,通过xfire,Android应用能够以SOAP方式与SQL Server数据库进行交互,但这是一种相对复杂且过时的方法。现代的Android开发通常倾向于使用RESTful API结合JSON数据交换,这更符合移动设备的特性。然而,对于...

    WebService(二):XFire实现WebService

    **与现代框架的比较**: 虽然XFire在当时是一个强大的工具,但随着技术的发展,它已经被更现代的框架如Apache CXF、Spring WebServices等取代。这些框架提供了更多的功能,如更好的WS-Security支持、更丰富的绑定...

    java webservice基于xfire实例

    XFire支持多种协议,包括SOAP、REST(Representational State Transfer)、JMS(Java Message Service)等,并且与Spring框架有良好的集成。 三、XFire环境搭建 首先,你需要在项目中引入XFire的依赖库。如果是...

    tuxedo教程和资料

    Tuxedo架构基于客户端-服务器模型,主要由三个主要组件构成:应用程序服务器(Application Server)、管理服务器(Monitor)和客户端接口(Client Interface)。应用程序服务器处理业务逻辑,管理服务器负责监控和...

Global site tag (gtag.js) - Google Analytics