Apache CXF With POJO
Apache CXF with JAX-WS
Apache CXF with WSDL First
Apache CXF With JAX-RS
Apache CXF With JMS
Apache CXF Conclusion
Apache CXF Useful Resources
Apache CXF with JMS
As mentioned earlier, you can use CXF with JMS transport. In this case, the client will send a JMS message to a known Messaging Server. Our server application is continuously listening to the messaging server for the incoming messages. When the message arrives, it processes the message, executes the client request and sends the response as another message to the client.
As earlier, we will first create a sample server application that provides a singular web method called sayHi.
Creating Service Interface
The service interface for our HelloWorld service is shown here −
package com.tutorialspoint.service;
import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHi(@WebParam(name = "name") String name);
}
Implementing Service
The implementation of the service interface is defined as follows −
package com.tutorialspoint.service.impl;
import jakarta.jws.WebService;
import com.tutorialspoint.service.HelloWorld;
@WebService
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHi(String name) {
return "Hello " + name;
}
}
The implementation simply returns a Hello message to the user. As you see, the interface and its implementation are similar to all the earlier projects in this tutorial that you have studied so far.
Now, comes the most important point which is to create a server application that sets up a message queue and keeps on listening to the incoming messages.
Creating Server
In the server application, first we create a JMS end point as follows −
private static final String JMS_ENDPOINT_URI =
"jms:queue:test.cxf.jmstransport.queue?timeToLive=1000"
+ "&jndiConnectionFactoryName=ConnectionFactory"
+ "&jndiInitialContextFactory"
+ "= org.apache.activemq.jndi.ActiveMQInitialContextFactory"
+ "&jndiURL = tcp://localhost:61616";
Note that we set up a queue on a specified port that lives for a specified amount of time. We now create a messaging service by instantiating org.apache.activemq.broker.BrokerService class. This is a server class for ActiveMQ messaging server.
BrokerService broker = new BrokerService();
You may use any other messaging server of your choice other than ActiveMQ. We now connect this server to a desired URI.
broker.addConnector("tcp://localhost:61616");
We set up the directory for the data storage of the incoming messages −
broker.setDataDirectory("target/activemq-data");
Finally, we start the server using the start method −
broker.start();
Next, we create an instance of our service bean HelloWorld using the server factory bean class as used in our earlier POJO application −
Object implementor = new HelloWorldImpl(); JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(HelloWorld.class);
Next, we set up the JMS endpoint on the factory so that the factory will keep on listening to the incoming messages −
factory.setTransportId (JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID); factory.setAddress(JMS_ENDPOINT_URI);
Finally, we set up the implementer class in the factory and start running it −
factory.setServiceBean(implementor); factory.create();
At this point your server is up and running. Note that since we have used the factory bean class as in the POJO application, the need for CXFServlet and the web.xml file is not required.