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.

Note: The naming of versions and releases used by Axis causes confusion. Typically, Axis 1.4 refers to the JAX-RPC implementation, and Axis2 to the JAX-WS implementation.

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.

  1. Configure the application server used in Eclipse IDE for Java EE Developers to support Axis2. In this example, configure the Liberty profile.
    1. Open the workspace preferences to configure the server: Open Window > Preferences.
    2. Check the installed JRE is Java50: Click Installed JREs.
    3. Add the Liberty profile as the server:
    4. Add Axis2: Click Web Services > Axis2 Preferences. On the Axis2 Runtime tab > Browse... Open the directory containing the many Axis2 jar files > Apply.
    5. 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
    6. 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.
  2. Check that you have deployed the StockQuoteAxis service to Liberty to run the Web service wizard.
  3. 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

  1. Create a Web project in a new Enterprise project:
    1. With nothing selected in the Project Explorer > Right-click the white space > New > Enterprise Application Project > Name it StockQuoteAxis2EAR > Finish. Reply No to the window giving you the option of opening the Java EE perspective.
      The defaults are set to use Liberty.
    2. Right-click StockQuoteAxis2EAR > New > Dynamic Web Project. Name the project StockQuoteAxis2WebClient > Check the EAR membership box to add the project to StockQuoteAxis2EAR. Liberty is selected as the Target runtime.
    3. In the Configuration section of the New Dynamic Web Project page > Modify... > Check the Axis2 Web services project facet. Dynamic Web Module 2.5, Java 6.0, and Liberty are already checked. > OK > Finish. Reply No to the window giving you the option of opening the Java EE perspective.
  2. Import WSDL for the service into the workspace and generate the client proxy:

    In this example, the WSDL document contains the HTTP service binding and becomes the target for the static Web client proxy. You can modify the URL in the Web service binding to point to the IBM MQ transport for SOAP URL before generating the client proxy. The static Web client proxy is then the service that is deployed to IBM MQ transport for SOAP.

    1. Launch the Web Services Explorer: either use the icon in the action bar, or Run > Launch the Web Services Explorer.
    2. Select the WSDL explorer by clicking the WSDL icon in the Web Services Explorer window > Click WSDL Main in the Navigator window > Type the URL of the StockQuoteAxis WSDL file > Go.
      In this example, obtain the WSDL directly from the HTTP service: http://localhost:8080/StockQuoteAxis/services/StockQuoteAxis?wsdl
    3. In the Navigator, click the line with the URL of the Web service. In the Actions window, click Import WSDL to Workbench > Select a StockQuoteAxis2WebClient as the Workbench project > Type the WSDL file name, StockQuoteAxisHTTP.wsdl > Go.
    4. Right-click StockQuoteAxisHTTP.wsdl > Web Services > Generate Client. Check the configuration information about the Web services page of the wizard is as follows: Server: IBM Liberty Server, Web service runtime: Apache Axis2, Client project: StockQuoteAxis2WebClient, Client EAR project: StockQuoteAxisEAR. To correct the configuration, click the lines that are wrong.
    5. Click Next > verify the code generation settings > Finish.
      Notice that a new package, soap.server, is created and it contains the proxies you require.
  3. Configure the project to run IBM MQ transport for SOAP as the JMS transport.
    IBM MQ transport for SOAP provides a transportSender, but no transportReceiver. In other words, IBM MQ transport for SOAP supports Axis2 clients. Currently it does not support Axis2 services.
    1. In the StockQuoteAxis2WebClient project, right-click WebContent\WEB-INF\conf\axis2.xml > Open with... > XML editor.
    2. Search for the last transportSender (towards the end of the file) and find the commented out JMS transportSender > Right-click the line > Add before... > transportSender.
    3. Right-click transportSender > Add Attribute > Name > Right-click transportSender > Add Attribute > Class.
    4. Right-click Name > Edit Attribute > Type the Value: jms
    5. Right-click Class > Edit Attribute > Type the Value: com.ibm.mq.axis2.transport.jms.WMQJMSTransportSender.> Save.
    6. Add com.ibm.mq.axis2.transport.jms.WMQJMSTransportSender to the build path: Right-click StockQuoteAxis2WebClient > Build Path > Configure Build Path... > Click the Libraries tab > Add External JARs.... Select all the JARs in MQ_INSTALLATION_PATH \java\lib > OK.
      MQ_INSTALLATION_PATH is the directory where IBM MQ is installed.
  4. Create a synchronous static client, test it using HTTP, then convert the proxy to run the static client using IBM MQ transport for SOAP.
    1. Right-click Java Resources: src > New > Package > Name the package soap.client > Finish
    2. Right-click soap.client > New > Class > Name the class SQA2StaticClient > Finish.
    3. Replace the class with the following code, then click Save.
      Figure 1. SQA2DynamicClient.java
      
      package soap.client;
      import soap.server.StockQuoteAxisServiceStub;
      import soap.server.StockQuoteAxisServiceStub.GetQuote;
      public class SQA2StaticClient {
        public static void main(String[] args) {
          try {
            StockQuoteAxisServiceStub stub = new StockQuoteAxisServiceStub();
            GetQuote request = new GetQuote();
            request.setSymbol("ibm");
            System.out.println("Response is: "
                + (stub.getQuote(request)).getGetQuoteReturn());
          } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            e.printStackTrace();
          }
        }
      }
      
  5. Test the client with the StockQuoteAxis service deployed to Liberty, and with IBM MQ transport for SOAP.
    1. In the Project Explorer, right-click SQA2StaticClient > Run as... > Java Application.
      The result, Response is 55.25 , appears in the Console view. You can also select the Liberty console window in the Console view, and see the output on the Liberty server, StockQuoteAxis called with parameter: ibm .
    2. The proxy was built with the service address, http://localhost:8080/StockQuoteAxis/services/StockQuoteAxis, and so the static client calls the service running on HTTP. You can change the static client to call the service using IBM MQ transport for SOAP. The following instructions change the service address in StockQuoteAxisServiceStub.java without rebuilding the proxy, and configure the SQA2StaticClient runtime parameters to load axis2.xml. You configure axis2.xml configures Axis2 to use IBM MQ transport for SOAP.
    3. Open StockQuoteAxisServiceStub.java >
      Replace the two occurrences of http://localhost:8080/StockQuoteAxis/services/StockQuoteAxis with,
      
      jms:/queue?destination=REQUESTAXIS@QM1
      &connectionFactory=()
      &initialContextFactory=com.ibm.mq.jms.Nojndi
      &targetService=StockQuoteAxis
      &replyDestination=SYSTEM.SOAP.RESPONSE.QUEUE
      
    4. If you run SQA2StaticClient now, it throws an exception because it has not found a transportSender configured for JMS
      The exception is:
      Exception: null java.lang.NullPointerException at
      soap.server.StockQuoteAxisServiceStub.getQuote(StockQuoteAxisServiceStub.java:547)
      at soap.client.SQA2StaticClient.main(SQA2StaticClient.java:11)
      
    5. In the Project Explorer, right-click SQA2StaticClient > Run as... > Run Configurations.... Switch to the (x)= Arguments tab, and in the VM arguments input area, type the path to the axis2.conf file> Apply > Run.
      The VM argument is: -Daxis2.xml=${workspace_loc:StockQuoteAxis2WebClient/WebContent/WEB-INF/conf}/axis2.xml. Or you can provide a standard path to the Axis2 configuration file.
    6. Run SQA2StaticClient again. On this run, you are using the IBM MQ transport for SOAP. Confirm it by checking there is no new output in the Liberty console. Open the console or command window that is associated with SimpleJavaListener, and the output there is StockQuoteAxis called with parameter: ibm .
  6. Create a dynamic client for HTTP and IBM MQ transport for SOAP, and test it.
    1. Right-click soap.client > New > Class > Name the class SQA2DynamicClient > Finish.
    2. Replace the class with the following code, then click Save.
      
      package soap.client;
      import soap.server.StockQuoteAxisServiceStub;
      import soap.server.StockQuoteAxisServiceStub.GetQuote;
      public class SQA2DynamicClient {
        public static void main(String[] args) {
          try {
            StockQuoteAxisServiceStub stub = new StockQuoteAxisServiceStub(
                "http://localhost:8080/StockQuoteAxis/services/StockQuoteAxis");
            GetQuote request = new GetQuote();
            request.setSymbol("ibm");
            System.out.println("HTTP Sync: "
                + (stub.getQuote(request)).getGetQuoteReturn());
            stub = new StockQuoteAxisServiceStub(
                "jms:/queue?destination=REQUESTAXIS@QM1"
                + "&connectionFactory=()&initialContextFactory=com.ibm.mq.jms.Nojndi"
                + "&targetService=StockQuoteAxis&replyDestination=SYSTEM.SOAP.RESPONSE.QUEUE");
             System.out.println("JMS sync: " 
                + (stub.getQuote(request)).getGetQuoteReturn());
           } catch (Exception e) {
             System.out.println("Exception: " + e.getMessage());
             e.printStackTrace();
           }
         }
       }
      
    3. Create a Run configuration for SQA2DynamicClient.java, and add the path to axis2.xml:
      -Daxis2.xml=${workspace_loc:StockQuoteAxis2WebClient/WebContent/WEB-INF/conf}/axis2.xml
    4. Run SQA2DynamicClient. Check the console output for the SQA2DynamicClient, Liberty and SimpleJavaListener.
  7. Create an asynchronous client, and access the result in a callback handler, and in the main program thread.

    The asynchronous client proxies created by the Web service wizard for Eclipse Java EE IDE for Web Developers differ from the proxies created by wsimport. The wsimport proxies use Future, Response, and AsyncHandler generic types.

    The Web service wizard for Eclipse Java EE IDE for Web Developers creates a StockQuoteAxisServiceCallbackHandler abstract class. You must extend StockQuoteAxisServiceCallbackHandler and create a callback handler.

    1. Right-click soap.client > New > Class > Name the class SQA2CallbackHandler > Finish.
    2. Replace the class with the following code.
      
      package soap.client;
      import soap.server.StockQuoteAxisServiceCallbackHandler;
      import soap.server.StockQuoteAxisServiceStub.GetQuoteResponse;
      public class SQA2CallbackHandler
          extends StockQuoteAxisServiceCallbackHandler {
        private boolean complete = false;
        SQA2CallbackHandler() {
          super();
          System.out.println("Callback constructor");
        }
        public void receiveResultgetQuote(GetQuoteResponse response) {
          System.out.println("Result in Callback " + response.getGetQuoteReturn());
          super.clientData = response;
          complete = true;
        }
        public boolean isComplete() {
          return complete;
        }
      }
      
    3. Right-click soap.client > New > Class > Name the class SQA2AsyncClient > Finish.
    4. Replace the class with the following code.
      Figure 2. SQA2AsyncClient.java
      
      package soap.client;
      import soap.server.StockQuoteAxisServiceStub;
      import soap.server.StockQuoteAxisServiceStub.GetQuote;
      import soap.server.StockQuoteAxisServiceStub.GetQuoteResponse;
      import soap.server.StockQuoteAxisServiceCallbackHandler;
      @SuppressWarnings("unused")
      public class SQA2AsyncClient {
        public static void main(String[] args) {
          try {
            StockQuoteAxisServiceStub stub = new StockQuoteAxisServiceStub(
                "http://localhost:8080/StockQuoteAxis/services/StockQuoteAxis");
            GetQuote request = new GetQuote();
            request.setSymbol("ibm");
            System.out.println("HTTP Sync: "
                + (stub.getQuote(request)).getGetQuoteReturn());
            SQA2CallbackHandler callback = new SQA2CallbackHandler();
            stub.startgetQuote(request, callback);
            do {
              System.out.println("Waiting for HTTP callback");
              Thread.sleep(2000);
            } while (!callback.isComplete());
            System.out.println("HTTP poll: "
                + ((GetQuoteResponse) (callback.getClientData()))
                        .getGetQuoteReturn());
            stub = new StockQuoteAxisServiceStub(
                "jms:/queue?destination=REQUESTAXIS@QM1"
                + "&connectionFactory=()&initialContextFactory=com.ibm.mq.jms.Nojndi"
                + "&targetService=StockQuoteAxis&replyDestination=SYSTEM.SOAP.RESPONSE.QUEUE");
            System.out.println("JMS Sync: "
                + (stub.getQuote(request)).getGetQuoteReturn());
            callback = new SQA2CallbackHandler();
            stub.startgetQuote(request, callback);
            while (!callback.isComplete()) {
              System.out.println("Waiting for JMS callback");
              Thread.sleep(2000);
            }
            System.out.println("JMS poll: "
              + ((GetQuoteResponse) (callback.getClientData())).getGetQuoteReturn());
          } catch (Exception e) {
              System.out.println("Exception: " + e.getMessage());
              e.printStackTrace();
           }
         }
       }
      
      The console output is as follows:
      
      HTTP Sync: 55.25
      Callback constructor
      Waiting for HTTP callback
      Result in Callback 55.25
      HTTP poll: 55.25
      JMS Sync: 55.25
      Callback constructor
      Waiting for JMS callback
      Result in Callback 55.25
      JMS poll: 55.25
      

Sample Axis2 clients

The sample proxies are generated using the wsimport tool that is packaged with Java 6. Six samples are provided:
  1. DynamicProxyClientSync.java
  2. DynamicProxyClientAsyncPolling.java
  3. DynamicProxyClientAsyncCallback.java
  4. DispatchClientSync.java
  5. DispatchClientAsyncPolling.java
  6. DispatchClientAsyncCallback.java
The client samples are generated for the sample StockQuoteAxis server. Generate the WSDL with the amqwdepoyWMQServer command, specifying the -w switch to select rpcLiteral style. Use the following command to generate the proxies for the samples:

wsimport soap.server.StockQuoteAxis_Wmq.wsdl -d generated -keep -p com.ibm.mq.axis2.samples
Figure 3. 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
  }
}
Figure 4. 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
  }
}
Figure 5. 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
  }

}
Figure 6. 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
  }
}
Figure 7. 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
  }
}
Figure 8. 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
  }
}