下面以一个例子来展示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附件。