CXF 提供的Service Transport-JMS Transpor

下面以一个例子来展示JMS Transport的功能:

1. 开发服务和实现类

import javax.jws.WebService;

@WebService
public interface OrderProcess {

	String processOrder(Order order);
}

 

import javax.jws.WebService;

@WebService
public class OrderProcessImpl implements OrderProcess {

	public String processOrder(Order order) {
		System.out.println("Processing order...");
		String orderID = validate(order);
		return orderID;
	}

	/**
	 * Validates the order and returns the order ID
	 **/
	private String validate(Order order) {
                ......
        }
} 

2. 配置内嵌的Broker

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.store.memory.MemoryPersistenceAdapter;

public final class MessageBroker {
	private MessageBroker() {
	}

	public static void main(String[] args) throws Exception {

		BrokerService broker = new BrokerService();
		broker.setPersistenceAdapter(new MemoryPersistenceAdapter());
		broker.addConnector("tcp://localhost:61616");
		broker.start();
		System.out.println("JMS broker ready ...");
        }
} 

3. Server端配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="
            http://cxf.apache.org/transports/http/configuration
            http://cxf.apache.org/schemas/configuration/http-conf.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
			http://cxf.apache.org/jaxws 
			http://cxf.apache.org/schemas/jaxws.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" />
	<import resource="classpath:META-INF/cxf/cxf-extension-jms.xml" />

	<jaxws:endpoint id="orderProcess" implementor="org.pbdp.sample.jms.OrderProcessImpl" address="jms://">
		<jaxws:features>
			<bean class="org.apache.cxf.transport.jms.JMSConfigFeature"
				p:jmsConfig-ref="jmsConfig" />
		</jaxws:features>
	</jaxws:endpoint>

	<bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration"
		p:connectionFactory-ref="jmsConnectionFactory" p:targetDestination="test.cxf.jmstransport.queue" />

	<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>

</beans> 

4. Client端配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	
	<jaxws:client id="orderClient" serviceClass="org.pbdp.sample.jms.OrderProcess" address="jms://">
		<jaxws:features>
			<bean class="org.apache.cxf.transport.jms.JMSConfigFeature"
				p:jmsConfig-ref="jmsConfig" />
		</jaxws:features>
	</jaxws:client>
	
	<bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration"
		p:connectionFactory-ref="jmsConnectionFactory" p:targetDestination="test.cxf.jmstransport.queue" />
	
	<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>

</beans> 

5. Client组件

import org.pbdp.sample.jms.Order;
import org.pbdp.sample.jms.OrderProcess;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Client {

	public Client() {
	}

	public static void main(String args[]) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "org/pbdp/sample/jms/client/client-bean.xml" });

		OrderProcess client = (OrderProcess) context.getBean("orderClient");
		Order order = new Order();
		order.setCustomerID("C001");
		order.setItemID("I001");
		order.setQty(100);
		order.setPrice(200.00);

		String orderID = client.processOrder(order);
		String message = (orderID == null) ? "Order not approved": "Order approved; order ID is " + orderID;
		System.out.println(message);
		System.exit(0);
	}

} 

6. 运行流程

     (1) 启动MessageBroker;

     (2) 启动Tomcat;

     (3) 运行客户端。

 

完整代码参考http://springsfeng.iteye.com/blog/1634753附件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值