程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> CXF使用JMS作為傳輸協議的配置

CXF使用JMS作為傳輸協議的配置

編輯:關於JAVA

因為CXF(v2.2.3)原生提供的例子不友好,文檔也不完整,摸索了幾個小時,才發現問題,貼出來共飨。

服務端配置:

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:jaxws="http://cxf.apache.org/jaxws"
     xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-extension-jms.xml" />
     <jaxws:endpoint
         xmlns:customer="http://customerservice.service.test/"
         id="CustomerService" address="jms://localhost:61616"
         serviceName="customer:CustomerServiceService"
         endpointName="customer:CustomerServiceEndpoint"
         implementor="test.service.impl.CustomerServiceImpl">
         <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.springframework.jms.connection.SingleConnectionFactory">
         <property name="targetConnectionFactory">
             <bean
                 class="org.apache.activemq.ActiveMQConnectionFactory">
                 <property name="brokerURL"
                     value="tcp://localhost:61616" />
             </bean>
         </property>
     </bean>
</beans>

客戶端配置:

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:jaxws="http://cxf.apache.org/jaxws"
     xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-extension-jms.xml" />
     <jaxws:client id="CustomerService"
         xmlns:customer="http://customerservice.service.test/"
         serviceName="customer:CustomerServiceService"
         endpointName="customer:CustomerServiceEndpoint"
         address="jms://localhost:61616"
         serviceClass="test.service.CustomerService">
         <jaxws:features>
             <bean xmlns="http://www.springframework.org/schema/beans"
                 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.springframework.jms.connection.SingleConnectionFactory">
         <property name="targetConnectionFactory">
             <bean
                 class="org.apache.activemq.ActiveMQConnectionFactory">
                 <property name="brokerURL"
                     value="tcp://localhost:61616" />
             </bean>
         </property>
     </bean>
</beans>

服務端App:

public class ServerApp {

     public static void main(String[] args) {
         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
         "server-applicationContext.xml");
     }

}

客戶端App:

public class ClientApp {
     /**
      * @param args
      */
     public static void main(String[] args) {
         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                 "client-applicationContext.xml");
         CustomerService hello = (CustomerService) applicationContext
         .getBean("CustomerService");
         System.out.println(hello.getOrder(null).getName());
     }
}

Broker:

public final class EmbeddedBroker {
     private EmbeddedBroker() {
     }
     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 ");
         Thread.sleep(125 * 60 * 1000);
         System.out.println("JMS broker exiting");
         broker.stop();
         System.exit(0);
     }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved