Skip to main content

Consume the service with the API

Sample code to consume a SOAP-over-JMS service is as follows:

public void invoke() throws Exception {
   // You just need to set the address with JMS URI
   String address = 
      "jms:jndi:dynamicQueues/test.cxf.jmstransport.queue3"
      + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory"
      + "&jndiConnectionFactoryName=ConnectionFactory"
      + "&jndiURL=tcp://localhost:61500";
   JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
   // And specify the transport ID with SOAP over JMS specification
   factory.setTransportId(
      JMSSpecConstants.SOAP_JMS_SPECIFICIATION_TRANSPORTID);
   factory.setServiceClass(Hello.class);
   factory.setAddress(address);
   Hello client = (Hello)factory.create();
   String reply = client.sayHi(" HI");
   System.out.println(reply);
}

// Alternatively using the JAXWS API with jms details defined in WSDL while avoiding JNDI
SOAPService2 service = new SOAPService2(wsdl, serviceName); // Using the generated service
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61500");
ConnectionFactoryFeature cff = new ConnectionFactoryFeature(cf);
Greeter greeter = service.getPort(portName, Greeter.class, cff); 
// Connection Factory can be set as a feature in CXF >= 3.0.0

If you specify queue or topic as variant and use cxf >= 3.0.0 then the jndi settings are not necessary.

svrFactory.setAddress("jms:queue:test.cxf.jmstransport.queue?timeToLive=1000");
// For CXF >= 3.0.0
svrFactory.setFeatures(Collections.singletonList(new ConnectionFactoryFeature(cf)));

In this case case the connection factory is supplied using a feature. For CXF 2.x the connection factory can only be supplied using jndi.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!