Better JMS Transport for CXF Webservice using Apache Camel
Excerpt |
---|
Configuring JMS in Apache CXF is possible but not really easy or nice. This article shows how to use Apache Camel to provide a better JMS Transport for CXF. |
You can find the original announcement for this Tutorial and some additional info on Christian Schneider´s Blog
Why not simply use JMS Features of Apache CXF
JMS configuration in Apache CXF is possible but the configuration is not very flexible and quite error prone. In CXF you have to configure a JMSConduit or a JMSDestination for each webservice. The connection between Conduit and the Service proxy is the endpoint name which looks like "{http://service.test\}HelloWorldPort.jms-conduit". As this name is never explicitly configured elsewhere it is quite probable that you misspell the name. If this happens then the JMS Transport just does not work. There is no good error reporting to show you what you did wrong. The next thing is that you have to use JNDI for the connectionFactory and the queue name. This is something that beginners with JMS have many problems with.
Why is using Apache Camel better in the JMS Transport layer
In Apache Camel you can simply reference the ConnectionFactory as a Spring bean. This means you can either define it directly in a Spring bean what is the ideal way to start or you can use Spring´s JNDI binding to retrieve it from your application server for production use.
The next nice thing is that you can configure all JMS options like receive timeout or username / password in a central location, the JMSComponent and then share this config for several services. On the other hand you can easily configure different JMS providers.
So how to connect Apache Camel and CXF
The best way to connect Camel and CXF is using the Camel transport for CXF. This is a camel module that registers with cxf as a new transport. It is quite easy to configure.
...
This bean registers with CXF and provides a new transport prefix camel:// that can be used in CXF address configurations. The bean references a bean cxf which will be already present in your config. The other refrenceis a camel context. We will later define this bean to provide the routing config.
How is JMS configured in Camel
In camel you need two things to configure JMS. A ConnectionFactory and a JMSComponent. As ConnectionFactory you can simply set up the normal Factory your JMS provider offers or bind a JNDI ConnectionFactory. In this example we use the ConnectionFactory provided by ActiveMQ.
...
You can find more details about the JMSComponent at the Camel Wiki. For example you find the complete configuration options and a JNDI sample there.
Setting up the CXF client
We will configure a simple CXF webservice client. It will use stub code generated from a wsdl. The webservice client will be configured to use JMS directly. You can also use a direct: Endpoint and do the routing to JMS in the Camel Context.
...
We explicitly configure serviceName and endpointName so they are not read from the wsdl. The names we use are arbitrary and have no further function but we set them to look nice. The serviceclass points to the service interface that was generated from the wsdl. Now the important thing is address. Here we tell cxf to use the camel transport, use the JmsComponent who registered the prefix "jms" and use the queue "CustomerService".
Setting up the CamelContext
As we do not need additional routing an empty CamelContext bean will suffice.
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext id="camelContext" xmlns="http://activemq.apache.org/camel/schema/spring"> </camelContext> |
Running the Example
- Follow the readme.txt
Conclusion
As you have seen in this example you can use Camel to dramatically ease JMS configuration compared with CXFs native JMS Transport. This is of course only a workaround. The CXF team already works on the native JMS config for CXF to make it at least as good as Camel´s. As CXF and Camel are two projects that have very good connections between them it is perhaps even possible to have a common transport layer for CXF and camel.