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 = &quot;GreeterPort&quot;,
serviceName = &quot;JMSGreeterService&quot;,
targetNamespace = &quot;http://cxf.apache.org/jms_greeter&quot;,
endpointInterface = &quot;org.apache.cxf.jms_greeter.JMSGreeterPortType&quot;,
wsdlLocation = &quot;file:./wsdl/jms_greeter.wsdl&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(&quot;Invoking sayHi…&quot;);
System.out.println(&quot;server responded with: &quot; + greeter.sayHi());
System.out.println();
System.out.println(&quot;Invoking greetMe…&quot;);
System.out.println(&quot;server responded with: &quot; + greeter.greetMe(System.getProperty(&quot;user.name&quot;)));
System.out.println();
System.out.println(&quot;Invoking greetMeOneWay…&quot;);
greeter.greetMeOneWay(System.getProperty(&quot;user.name&quot;));
System.out.println(&quot;No response from server as method is OneWay&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(&quot;target/activemq-data&quot;);
broker.addConnector(&quot;tcp://localhost:61616&quot;);
broker.start();
System.out.println(&quot;JMS broker ready …&quot;);
Thread.sleep(125 * 60 * 1000);
System.out.println(&quot;JMS broker exiting&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]
Like this:
Like Loading...