2 February 2014

Creating a JAX-WS based Dispatch client

import java.io.IOException;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.*;
import javax.xml.ws.soap.SOAPBinding;

public class DispatchClient {

    public static void main(String args[]) {

        String svcNamespace = "http://cater.com/";
        String prefix = "ns1";

        Service svc = Service.create(new QName(svcNamespace, "stockService"));
        QName portQName = new QName(svcNamespace, "stockPort");
        svc.addPort(portQName,
            SOAPBinding.SOAP11HTTP_BINDING,
            "http://localhost:7001/Stock/StockService");
        Dispatch<SOAPMessage> dispatch = svc.createDispatch(portQName,
            SOAPMessage.class,
            Service.Mode.MESSAGE);
        try {
            MessageFactory msgFactory = MessageFactory.newInstance(
                SOAPConstants.SOAP_1_1_PROTOCOL);
            SOAPMessage request = msgFactory.createMessage();
            request.getSOAPBody().
                addChildElement("getBeersFrom", prefix, svcNamespace).addChildElement(
                    "arg0").
                addTextNode("Belgium");
            SOAPMessage response = dispatch.invoke(request);
            response.writeTo(System.out);
        } catch (SOAPException | IOException ex) {
            ex.printStackTrace();
        }
    } // end main
} // end class