Social Media

Apache CXF – JMS Queue

This sample(samples\java_first_jms) demonstrates how to connect to web services across JMS transport, instead of HTTP as in the previous examples.

Web Service Contract – jms_greeter.wsdl

  • <wsdl:operation name=”greetMe”>
  • <wsdl:operation name=”sayHi”>
  • <wsdl:operation name=”greetMeOneWay”>

The wsdl:binding contains the important information that this sample will operate on JMS –

  •  <soap:binding style=”document” transport=””/>

Finally the wsdl:port definition defines the JMS Queue properties –

[sourcecode lang=”xml”] <wsdl:service name="JMSGreeterService">
<wsdl:port binding="tns:JMSGreeterPortBinding" name="GreeterPort">
<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:clientConfig useConduitIdSelector="false"/>

The implementation classes can be generated through the maven wsdl2java task.

Service Implementation

GreeterJMSImpl implements the JMSGreeterPortType interface –

[sourcecode lang=”java”] @javax.jws.WebService(portName = &amp;quot;GreeterPort&amp;quot;,
serviceName = &amp;quot;JMSGreeterService&amp;quot;,
targetNamespace = &amp;quot;;quot;,
endpointInterface = &amp;quot;org.apache.cxf.jms_greeter.JMSGreeterPortType&amp;quot;,
wsdlLocation = &amp;quot;file:./wsdl/jms_greeter.wsdl&amp;quot;)
public class GreeterJMSImpl implements JMSGreeterPortType {
  • portName = “GreeterPort” –> maps to wsdl:port attribute – name
  • serviceName = “JMSGreeterService” –> maps to wsdl:service attribute – name
  • targetNamespace = “” –> wsdl:definitions attribute targetNamespace
  • endpointInterface = “org.apache.cxf.jms_greeter.JMSGreeterPortType” –> Interface that is implemente
  • wsdlLocation = “file:./wsdl/jms_greeter.wsdl” –> wsdl location on disc


The Client class demonstrates how simple it is to call a JMS service using the wsdl URI –

[sourcecode lang=”java”] JMSGreeterService service = new JMSGreeterService(wsdl.toURI().toURL(), SERVICE_NAME);
JMSGreeterPortType greeter = (JMSGreeterPortType)service.getPort(PORT_NAME, JMSGreeterPortType.class);

System.out.println(&amp;quot;Invoking sayHi…&amp;quot;);
System.out.println(&amp;quot;server responded with: &amp;quot; + greeter.sayHi());

System.out.println(&amp;quot;Invoking greetMe…&amp;quot;);
System.out.println(&amp;quot;server responded with: &amp;quot; + greeter.greetMe(System.getProperty(&amp;quot;;quot;)));

System.out.println(&amp;quot;Invoking greetMeOneWay…&amp;quot;);
System.out.println(&amp;quot;No response from server as method is OneWay&amp;quot;);


The demonstration stores the message queue in Memory –

[sourcecode lang=”java”] public final class EmbeddedBroker {
private EmbeddedBroker() { }

public static void main(String[] args) throws Exception {
BrokerService broker = new BrokerService();
broker.setPersistenceAdapter(new MemoryPersistenceAdapter());
System.out.println(&amp;quot;JMS broker ready …&amp;quot;);
Thread.sleep(125 * 60 * 1000);
System.out.println(&amp;quot;JMS broker exiting&amp;quot;);

Running The Example

mvn install (this will build the demo)

In separate command windows/shells:

mvn -Pserver
mvn -Pclient

With the output –

[sourcecode] INFO: Creating Service {}JMSGreeterService from
WSDL: file:/…/apache-cxf-3.0.3/samples/jms_queue/src/main/config/jms_
Invoking sayHi…
server responded with: Bonjour

Invoking greetMe…
server responded with: Hello Martin

Invoking greetMeOneWay…
No response from server as method is OneWay

Invoking sayHi with JMS Context information …
server responded with: Bonjour
Received expected contents in response context

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: