CXF JMS Transport

This example works almost the same as th JAX WS Java First Example with http. The only two things that are different is that you configure the jms:conduit (JMS Config for client) and jms:endpoint(JMS Config for server) in the cxf.xml. This file will be loaded automatically when cxf starts.
Of course you could do the complete config in spring but I wanted to stick with the JAX WS Java First example.

Things to consider when doing the jms config:

  • name the jms:conduit like the client endpoint name + jms.conduit
  • name the jms:endpoint like the server endpoint name + jms.endpoint

The address in the proxy factory and server factory has to be set to jms://. The real addess has to be set in the conduit and destination.

In fact once all is set up it is quite easy. Only the fact that you have to connect jms:conduit and service by the endpoint name is not very intuitive.

You can download the complete example above. Additionally you need the activemq binaries. Start activemq before testing the service. There is no configuration required.

cxf.xml

<?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:cxf="http://cxf.apache.org/core"
 xmlns:soap="http://cxf.apache.org/bindings/soap"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xmlns:jms="http://cxf.apache.org/transports/jms"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schema/bindings/soap.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/jms http://cxf.apache.org/schemas/configuration/jms.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-local.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-jms.xml" />
 <cxf:bus>
 <cxf:features>
 <cxf:logging/>
 </cxf:features>
 </cxf:bus>

 <jms:conduit name="{http://service.test/}IHelloPort.jms-conduit">
 <jms:address destinationStyle="queue" jndiConnectionFactoryName="ConnectionFactory"
 jndiDestinationName="dynamicQueues/test.cxf.jmstransport.queue" connectionUserName="testUser" connectionPassword="testPassword">
 <jms:JMSNamingProperty name="java.naming.factory.initial" value="org.apache.activemq.jndi.ActiveMQInitialContextFactory" />
 <jms:JMSNamingProperty name="java.naming.provider.url" value="tcp://localhost:61616" />
 </jms:address>
 </jms:conduit>

 <jms:destination name="{http://service.test/}IHelloPort.jms-destination">
 <jms:address destinationStyle="queue" jndiConnectionFactoryName="ConnectionFactory" jndiDestinationName="dynamicQueues/test.cxf.jmstransport.queue"
 connectionUserName="testUser" connectionPassword="testPassword">
 <jms:JMSNamingProperty name="java.naming.factory.initial" value="org.apache.activemq.jndi.ActiveMQInitialContextFactory" />
 <jms:JMSNamingProperty name="java.naming.provider.url" value="tcp://localhost:61616" />
 </jms:address>
 </jms:destination>
</beans>

Server:

Object implementor = new Hello();
 JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
 svrFactory.setServiceClass(IHello.class);
 svrFactory.setAddress("jms://");
 svrFactory.setServiceBean(implementor);
 svrFactory.getInInterceptors().add(new LoggingInInterceptor());
 svrFactory.getOutInterceptors().add(new LoggingInInterceptor());
 svrFactory.create();

Client:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
 factory.getInInterceptors().add(new LoggingInInterceptor());
 factory.getOutInterceptors().add(new LoggingInInterceptor());
 factory.setServiceClass(IHello.class);
 factory.setAddress("jms://");
 IHello hello = (IHello) factory.create(); 

If you don't want to use the spring configuration file to configure JMS transport, you could use the Java code to configure them.

Server:

Object implementor = new Hello();
 JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
 svrFactory.setServiceClass(IHello.class);
 svrFactory.setAddress("jms://");
 svrFactory.setServiceBean(implementor);
 svrFactory.getInInterceptors().add(new LoggingInInterceptor());
 svrFactory.getOutInterceptors().add(new LoggingInInterceptor());
 // We need to create the server first but not start the server
 svrFactory.setStart(false);
 Server server = svrFactory.create();
 // Get the destination from the server
 JMSDestination jmsDestination = (JMSDestination)server.getDestination();
 ServerConfig serverConfig = new ServerConfig();
 serverConfig.setMessageTimeToLive(1000);
 ...
 jmsDestination.setServerConfig(serverConfig);
 ...
 // Start the server after the configuration
 server.start();

Client:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
 factory.getInInterceptors().add(new LoggingInInterceptor());
 factory.getOutInterceptors().add(new LoggingInInterceptor());
 factory.setServiceClass(IHello.class);
 factory.setAddress("jms://");
 IHello hello = (IHello) factory.create();
 // You could also configure the jmsConduit with Java code
 Client client = ClientProxy.getClient(hello);
 JMSConduit jmsConduit = (JMSConduit) client.getConduit();
 ClientConfig clientConfig = new ClientConfig();
 clientConfig.setClientReceiveTimeout(1000);
 ...
 jmsConduit.setClientConfig(clientConfiguration);
 AddressType address = new AddressType();
 address.setConnectionUserName("testUser");
 address.setConnectionPassword("testPasswd");
 ...
 jmsConduit.setJMSAddress(address);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值