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=”http://cxf.apache.org/transports/jms”/>

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:address
destinationStyle="queue"
jndiConnectionFactoryName="ConnectionFactory"
jndiDestinationName="dynamicQueues/test.cxf.jmstransport.queue">
<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:clientConfig useConduitIdSelector="false"/>
</wsdl:port>
</wsdl:service>
[/sourcecode]

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;http://cxf.apache.org/jms_greeter&amp;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 {
//…
}
[/sourcecode]
  • portName = “GreeterPort” –> maps to wsdl:port attribute – name
  • serviceName = “JMSGreeterService” –> maps to wsdl:service attribute – name
  • targetNamespace = “http://cxf.apache.org/jms_greeter” –> 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

Client

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();

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;user.name&amp;quot;)));
System.out.println();

System.out.println(&amp;quot;Invoking greetMeOneWay…&amp;quot;);
greeter.greetMeOneWay(System.getProperty(&amp;quot;user.name&amp;quot;));
System.out.println(&amp;quot;No response from server as method is OneWay&amp;quot;);
System.out.println();
[/sourcecode]

ActiveMQ

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());
broker.setDataDirectory(&amp;quot;target/activemq-data&amp;quot;);
broker.addConnector(&amp;quot;tcp://localhost:61616&amp;quot;);
broker.start();
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;);
broker.stop();
System.exit(0);
}
}
[/sourcecode]

Running The Example

mvn install (this will build the demo)

In separate command windows/shells:

mvn -Pjms.broker
mvn -Pserver
mvn -Pclient

With the output –

[sourcecode] INFO: Creating Service {http://cxf.apache.org/jms_greeter}JMSGreeterService from
WSDL: file:/…/apache-cxf-3.0.3/samples/jms_queue/src/main/config/jms_
greeter.wsdl
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
[/sourcecode]

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: