Developing a JAX-WS client for WebSphere® transport for SOAP using Eclipse
Develop an Axis2 Web service client to run using the IBM® MQ transport for SOAP. The sample Axis2 clients provided with IBM MQ transport for SOAP are listed, and the wsimport command used to generate proxies.
Before you begin
Obtain the Axis2 libraries, and configure a development and test environment to run the client.
Axis 1.4 is a version level. If you search for Axis 1.4 on the internet, you are taken to http://ws.apache.org/axis/. The page contains a list of preceding versions of Axis (1.2, 1.3) and the April 22, 2006, final release of Axis 1.4. There are later releases of Axis 1.4, that fix bugs, but they are all known as Axis 1.4. It is one of these bug fix releases that is shipped with IBM MQ. For Axis 1.4, use the version of axis.jar that is shipped with IBM MQ rather than the one obtainable from http://ws.apache.org/axis/.
The Axis website also refers to Axis 1.1 to refer to all the versions of what is more typically called Axis 1.4. Axis 1.2 is used to refer to what is typically called Axis2.
Axis 1.5 is not a later release of Axis 1.4, it is an Axis2 release. If you search for Axis 1.5 you are directed to http://ws.apache.org/axis2/. https://ws.apache.org/axis2/download.cgi contains a list of release versions of Axis2, labeled 0.9 to 1.5.1 (and including, confusingly version 1.4). The release version of Axis2 to use with IBM MQ transport for SOAP is 1.4.1. Download Axis2 1.4.1 from http://ws.apache.org/axis2/download/1_4_1/download.cgi.
You can choose to generate proxies for the Web service clients for IBM MQ transport for SOAP using either wsimport or the tooling provided with an IDE. Eclipse IDE for Java EE Developer 3.5 SR1 uses wsdl2java. wsimport is supplied with Java 6. You can use Java 5 to run client proxies generated either with wsimport or wsdl2java.
The sample Web service Axis2 clients provided with IBM MQ transport for SOAP were developed using wsimport ; see Sample Axis2 clients.
The task that follows demonstrates how to generate and use the proxies produced by the Web services wizard that is packaged with Eclipse IDE for Java EE Developers. The sample clients show how to use the proxies produced by wsimport.
To use the Web services wizard, you must add an application server that supports Axis2 to the workbench. The steps show how to configure the Liberty profile to support Axis2 using the workbench.
- Configure the application server used in Eclipse IDE for Java EE Developers to support Axis2. In this example,
configure the Liberty profile.
- Open the workspace preferences to configure the server: Open Window > Preferences.
- Check the installed JRE is Java50: Click Installed JREs.
- Add the Liberty profile as the server:
- Add Axis2: Click Web Services > Axis2 Preferences. On the Axis2 Runtime tab > Browse... Open the directory containing the many Axis2 jar files > Apply.
- Associate Liberty with Axis2: Click Web Services > Server and Runtime. Under Server select IBM Liberty Server, and under Web service runtime, select Apache Axis2 > Apply > OK
- Start the server: Open the Web perspective and open the Servers view. Right click in the Servers view > New > Server. IBM Liberty Server is selected and configured > Finish. Start the server.
- Check that you have deployed the StockQuoteAxis service to Liberty to run the Web service wizard.
- To test the service with the IBM MQ transport for SOAP service, deploy the service to an IBM MQ transport for SOAP listener for Axis 1.4; see the Liberty profile.
About this task
The Eclipse IDE for Java EE Developers uses Java50 and the Web services wizard to generate the proxy classes for the service. The proxy classes are different to the classes created by the wsimport tool provided with Java 6. An alternative approach is to generate the proxy classes using wsimport and import the packages it creates into your Eclipse Java EE IDE for Web Developers.
The Web services wizard in the Eclipse IDE for Java EE Developers builds a Web service client in a Web project. You can run the client as a simple Java application; it does not require an application server. You can also transfer the code to a Java project, and configure the build path to include the Axis2 JAR files.
Procedure
Sample Axis2 clients
The sample proxies are generated using the wsimport tool that is packaged with Java 6. Six samples are provided:-
DynamicProxyClientSync.java
-
DynamicProxyClientAsyncPolling.java
-
DynamicProxyClientAsyncCallback.java
-
DispatchClientSync.java
-
DispatchClientAsyncPolling.java
-
DispatchClientAsyncCallback.java
wsimport soap.server.StockQuoteAxis_Wmq.wsdl -d generated -keep -p com.ibm.mq.axis2.samples
DynamicProxyClientSync.java
package com.ibm.mq.axis2.samples;
import com.ibm.mq.axis2.samples.proxy.StockQuoteAxis;
import com.ibm.mq.axis2.samples.proxy.StockQuoteAxisService;
public class DynamicProxyClientSync {
public static void main(String[] args) {
try {
System.out.println("Starting sample DynamicProxyClientSync");
System.out.println("Creating proxy instance for service StockQuoteAxisService");
StockQuoteAxisService stub = new StockQuoteAxisService();
StockQuoteAxis service = stub.getSoapServerStockQuoteAxisWmq();
System.out.println("Invoking getQuoteOneWay OneWay operation synchronously...");
service.getQuoteOneWay("48");
System.out.println(" > getQuoteOneWay has returned");
System.out.println("Invoking getQuote Request Reply operation synchronously...");
float result = service.getQuote("48");
System.out.println(" > getQuote has returned result of " + result);
System.out.println("End of sample");
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
}
DynamicProxyClientAsyncPolling.java
package com.ibm.mq.axis2.samples;
import java.util.concurrent.CancellationException;
import javax.xml.ws.Response;
import com.ibm.mq.axis2.samples.proxy.StockQuoteAxis;
import com.ibm.mq.axis2.samples.proxy.StockQuoteAxisService;
public class DynamicProxyClientAsyncPolling {
public static void main(String[] args) {
try {
System.out.println("Starting sample DynamicProxyClientAsyncPolling");
System.out.println("Creating proxy instance for service StockQuoteAxisService");
StockQuoteAxisService stub = new StockQuoteAxisService();
StockQuoteAxis service = stub.getSoapServerStockQuoteAxisWmq();
System.out
.println("Invoking getQuoteAsync Request Reply operation asynchronously by polling...");
Response<Float> response = service.getQuoteAsync("49");
/** Sleep main thread until response arrives **/
System.out.println("Waiting for response to arrive...");
while (!response.isDone()) {
Thread.sleep(100);
}
System.out.println(" > Response received");
/** Retrieve the result **/
try {
Float result = response.get();
System.out.println(" > getQuoteAsync call has returned result of " + result);
}
catch (CancellationException ce) {
// processing was cancelled via response.cancel()
}
System.out.println("End of sample");
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
}
DynamicProxyClientAsyncCallback.java
package com.ibm.mq.axis2.samples;
import java.util.concurrent.Future;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;
import com.ibm.mq.axis2.samples.proxy.StockQuoteAxis;
import com.ibm.mq.axis2.samples.proxy.StockQuoteAxisService;
public class DynamicProxyClientAsyncCallback implements AsyncHandler<Float> {
public static void main(String[] args) {
try {
System.out.println("Starting sample DynamicProxyClientAsyncCallback");
System.out.println("Creating proxy instance for service StockQuoteAxisService");
StockQuoteAxisService stub = new StockQuoteAxisService();
StockQuoteAxis service = stub.getSoapServerStockQuoteAxisWmq();
DynamicProxyClientAsyncCallback handler = new DynamicProxyClientAsyncCallback();
System.out
.println("Invoking getQuoteAsync Request Reply operation asynchronously using a callback...");
Future<?> monitor = service.getQuoteAsync("50", handler);
System.out.println(" > Invoke call has returned");
/** Sleep main thread until handler has been notified **/
System.out.println("Waiting for handler to be called...");
while (!monitor.isDone()) {
Thread.sleep(100);
}
System.out.println("End of sample");
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
public void handleResponse(Response<Float> response) {
try {
Float result = response.get();
System.out.println(" > Async Handler has received a result of " + result);
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println("Exception in handleResponce");
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
}
DispatchClientSync.java
package com.ibm.mq.axis2.samples;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class DispatchClientSync {
public static void main(String[] args) {
try {
System.out.println("Starting sample DispatchClientSync");
String endpointUrl = "jms:/queue?destination=SOAPJ.demos@WMQSOAP.DEMO.QM&"
+ "connectionFactory=connectQueueManager(WMQSOAP.DEMO.QM)"
+ "&initialContextFactory=com.ibm.mq.jms.Nojndi&targetService=soap.server.StockQuoteAxis.java";
QName serviceName = new QName("soap.server.StockQuoteAxis_Wmq", "StockQuoteAxisService");
QName portName = new QName("soap.server.StockQuoteAxis_Wmq", "soap.server.StockQuoteAxis_Wmq");
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);
/** Create a Dispatch instance from a service **/
System.out.println("Creating dispatch instance for service StockQuoteAxisService");
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class,
Service.Mode.MESSAGE);
System.out.println(" > Dispatch instance created.");
/*************************************
* Create OneWay SOAPMessage request.
*************************************/
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
System.out.println("\nCreating a OneWay SOAP Message");
SOAPMessage request = mf.createMessage();
/** Obtain the SOAPEnvelope and header and body elements **/
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
SOAPBody body = env.getBody();
/** Construct the message payload **/
SOAPElement operation = body.addChildElement("getQuoteOneWay", "ns1",
"soap.server.StockQuoteAxis_Wmq");
SOAPElement value = operation.addChildElement("in0");
value.addAttribute(new QName("https://www.w3.org/2001/XMLSchema-instance", "type"), "string");
value.addTextNode("XXX");
request.saveChanges();
System.out.println(" > SOAP Message created.");
/** Invoke the service endpoint **/
System.out.println("Invoking getQuoteOneWay OneWay operation synchronously...");
dispatch.invokeOneWay(request);
System.out.println(" > getQuoteOneWay call has returned");
/********************************************
* Create Request Reply SOAPMessage request.
********************************************/
mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
System.out.println("\nCreating a Request Reply SOAP Message");
request = mf.createMessage();
/** Obtain the SOAPEnvelope and header and body elements **/
part = request.getSOAPPart();
env = part.getEnvelope();
header = env.getHeader();
body = env.getBody();
/** Construct the message payload **/
operation = body.addChildElement("getQuote", "ns1", "soap.server.StockQuoteAxis_Wmq");
value = operation.addChildElement("in0");
value.addAttribute(new QName("https://www.w3.org/2001/XMLSchema-instance", "type"), "string");
value.addTextNode("XXX");
request.saveChanges();
System.out.println(" > SOAP Message created.");
/** Invoke the service endpoint **/
System.out.println("Invoking getQuote Request Reply operation synchronously...");
SOAPMessage ans = dispatch.invoke(request);
System.out.println(" > getQuote call has returned");
/** Retrieve the result **/
part = ans.getSOAPPart();
env = part.getEnvelope();
body = env.getBody();
/** Define name of the SOAP folders we are interested in **/
QName responseName = new QName("soap.server.StockQuoteAxis_Wmq", "getQuoteResponse");
QName resultName = new QName("getQuoteReturn");
/** Retrieve result from SOAP envelope **/
System.out.println("Parsing SOAP response...");
SOAPElement bodyElement = (SOAPElement) body.getChildElements(responseName).next();
SOAPElement responseElement = (SOAPElement) bodyElement.getChildElements(resultName).next();
String message = responseElement.getValue();
System.out.println(" > Response contains result of " + message);
System.out.println("End of sample");
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
}
DispatchClientAsyncPolling.java
package com.ibm.mq.axis2.samples;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Response;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class DispatchClientAsyncPolling {
public static void main(String[] args) {
try {
System.out.println("Starting sample DispatchClientAsyncPolling");
String endpointUrl = "jms:/queue?destination=SOAPJ.demos@WMQSOAP.DEMO.QM&"
+ "connectionFactory=connectQueueManager(WMQSOAP.DEMO.QM)"
+ "&initialContextFactory=com.ibm.mq.jms.Nojndi&targetService=soap.server.StockQuoteAxis.java";
QName serviceName = new QName("soap.server.StockQuoteAxis_Wmq", "StockQuoteAxisService");
QName portName = new QName("soap.server.StockQuoteAxis_Wmq", "soap.server.StockQuoteAxis_Wmq");
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);
/** Create a Dispatch instance from a service.* */
System.out.println("Creating dispatch instance for service StockQuoteAxisService");
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class,
Service.Mode.MESSAGE);
System.out.println(" > Dispatch instance created.");
/** Create SOAPMessage request. * */
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
System.out.println("Creating a Request Reply SOAP Message");
SOAPMessage request = mf.createMessage();
/** Obtain the SOAPEnvelope and header and body elements **/
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
SOAPBody body = env.getBody();
/** Construct the message payload **/
SOAPElement operation = body.addChildElement("getQuote", "ns1",
"soap.server.StockQuoteAxis_Wmq");
SOAPElement value = operation.addChildElement("in0");
value.addAttribute(new QName("https://www.w3.org/2001/XMLSchema-instance", "type"), "string");
value.addTextNode("XXX");
request.saveChanges();
System.out.println(" > SOAP Message created.");
/** Invoke the service endpoint **/
System.out.println("Invoking getQuote Request Reply operation asynchronously by polling...");
Response<SOAPMessage> response = dispatch.invokeAsync(request);
System.out.println(" > getQuote call has returned");
/** Sleep main thread until response arrives **/
System.out.println("Waiting for response to arrive...");
while (!response.isDone()) {
Thread.sleep(100);
}
System.out.println(" > Response received");
/** retrieve the result **/
SOAPMessage ans = response.get();
part = ans.getSOAPPart();
env = part.getEnvelope();
body = env.getBody();
/** Define name of the SOAP folders we are interested in **/
QName responseName = new QName("soap.server.StockQuoteAxis_Wmq", "getQuoteResponse");
QName resultName = new QName("getQuoteReturn");
/** Retrieve result from SOAP envelope **/
SOAPElement bodyElement = (SOAPElement) body.getChildElements(responseName).next();
SOAPElement responseElement = (SOAPElement) bodyElement.getChildElements(resultName).next();
String message = responseElement.getValue();
System.out.println(" > Response contains result of " + message);
System.out.println("End of sample");
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
}
DispatchClientAsyncCallback.java
package com.ibm.mq.axis2.samples;
import java.util.concurrent.Future;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Response;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class DispatchClientAsyncCallback implements AsyncHandler<SOAPMessage> {
public static void main(String[] args) {
try {
System.out.println("Starting sample DispatchClientAsyncCallback");
String endpointUrl = "jms:/queue?destination=SOAPJ.demos@WMQSOAP.DEMO.QM&"
+ "connectionFactory=connectQueueManager(WMQSOAP.DEMO.QM)"
+ "&initialContextFactory=com.ibm.mq.jms.Nojndi&targetService=soap.server.StockQuoteAxis.java";
QName serviceName = new QName("soap.server.StockQuoteAxis_Wmq", "StockQuoteAxisService");
QName portName = new QName("soap.server.StockQuoteAxis_Wmq", "soap.server.StockQuoteAxis_Wmq");
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);
/** Create a Dispatch instance from a service.* */
System.out.println("Creating dispatch instance for service StockQuoteAxisService");
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class,
Service.Mode.MESSAGE);
System.out.println(" > Dispatch instance created.");
/** Create SOAPMessage request. * */
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
System.out.println("Creating a Request Reply SOAP Message");
SOAPMessage request = mf.createMessage();
/** Obtain the SOAPEnvelope and header and body elements **/
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
SOAPBody body = env.getBody();
/** Construct the message payload. **/
SOAPElement operation = body.addChildElement("getQuote", "ns1",
"soap.server.StockQuoteAxis_Wmq");
SOAPElement value = operation.addChildElement("in0");
value.addAttribute(new QName("https://www.w3.org/2001/XMLSchema-instance", "type"), "string");
value.addTextNode("XXX");
request.saveChanges();
System.out.println(" > SOAP Message created.");
/** Invoke the service endpoint. **/
DispatchClientAsyncCallback handler = new DispatchClientAsyncCallback();
System.out
.println("Invoking getQuote Request Reply operation asynchronously using a callback...");
Future<?> monitor = dispatch.invokeAsync(request, handler);
System.out.println(" > getQuote call has returned");
/** Sleep main thread until handler has been notified **/
System.out.println("Waiting for handler to be called...");
while (!monitor.isDone()) {
Thread.sleep(100);
}
System.out.println("End of sample");
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
public void handleResponse(Response<SOAPMessage> response) {
try {
// retrieve the result
SOAPMessage ans = response.get();
SOAPPart part = ans.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPBody body = env.getBody();
/** Define name of the SOAP folders we are interested in **/
QName responseName = new QName("soap.server.StockQuoteAxis_Wmq", "getQuoteResponse");
QName resultName = new QName("getQuoteReturn");
/** Retrieve result from SOAP envelope **/
SOAPElement bodyElement = (SOAPElement) body.getChildElements(responseName).next();
SOAPElement responseElement = (SOAPElement) bodyElement.getChildElements(resultName).next();
String result = responseElement.getValue();
System.out.println(" > Async Handler has received a result of " + result);
}
catch (Exception fault) {
// Identify the cause of the Axis Fault
System.err.println("Exception in handleResponce");
System.err.println(fault.toString());
Throwable e = fault.getCause();
for (int i = 1; e != null; i++) {
// The toString method on an MQAxisException will cause the message, explanation and user
// action.
System.err.println("Exception(" + i + "): " + e.toString());
if (e.getCause() != null) {
e = e.getCause();
}
else {
break;
}
} // end of for loop
} // end of catch block
}
}