Axis2 WebService client with Eclipse

Discussion in 'Java' started by carmelo, Oct 20, 2008.

  1. carmelo

    carmelo Guest

    Hi everybody,
    I developed an Axis2 WebService with Netbeans, and its client. It was
    easy enough, because Netbeans made it simple...
    Now I'd like to develop the client with Eclipse, because I need to
    integrate it into an application developed with Eclipse. Importing the
    jar file created by Netbeans it works easily, but it doesn't seem a
    "clean" solution...

    Searching on the internet (http://today.java.net/pub/a/today/
    2006/12/13/invoking-web-services-using-apache-axis2.html) I've found
    the following code, which should work as client:

    import org.apache.axiom.om.OMAbstractFactory;
    import org.apache.axiom.om.OMElement;
    import org.apache.axiom.om.OMFactory;
    import org.apache.axiom.om.OMNamespace;
    import org.apache.axis2.AxisFault;
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.client.ServiceClient;
    import org.apache.axis2.client.async.AxisCallback;
    import org.apache.axis2.context.MessageContext;

    public class WSClient
    {

    public static boolean finish = false;

    public static void main(String[] args) throws Exception {

    ServiceClient sender = new ServiceClient();

    // create option object
    Options opts = new Options();

    try {
    // setting target EPR
    opts.setTo(new EndpointReference(
    "http://localhost:8080/axis2/services/HelloAxisWorld"));
    // Setting action ,and which can be found from the wsdl of the
    // service
    opts.setAction("urn:echo");

    //opts.setTransportInProtocol("http");
    //opts.setUseSeparateListener(true);

    AxisCallback callback = new AxisCallback() {

    /**
    * This is called when we receive a message.
    *
    * @param msgContext
    * the (response) MessageContext
    */
    public void onMessage(MessageContext msgContext) {
    OMElement result = msgContext.getEnvelope().getBody()
    .getFirstElement();
    System.out.println(msgContext.toString());
    System.out.println(msgContext.getEnvelope().toString());
    System.out.println(msgContext.getEnvelope().getBody()
    .getFirstElement());
    finish = true;
    }

    /**
    * This gets called when a fault message is received.
    *
    * @param msgContext
    * the MessageContext containing the fault.
    */
    public void onFault(MessageContext msgContext) {
    System.out.println(msgContext.getEnvelope().getBody()
    .getFault().toString());

    }

    /**
    * This gets called ONLY when an internal processing exception
    * occurs.
    *
    * @param e
    * the Exception which caused the problem
    */
    public void onError(Exception e) {
    }

    /**
    * This is called at the end of the MEP no matter what happens,
    * quite like a finally block.
    */
    public void onComplete() {
    System.out.println("OnComplete!!!");

    }
    };

    sender.setOptions(opts);

    sender.engageModule("addressing");

    System.out.println("-------Invoke the service---------");

    sender.sendReceiveNonBlocking(createPayLoad(), callback);

    // wait till you get the response, in real applications you do not
    // need
    // to do this, since once the response arrive axis2 will notify
    // callback,
    // then you can implement callback to do whatever you want, may be
    // to update GUI

    synchronized (callback) {
    if (!finish) {
    callback.wait(45000);
    if (!finish) {
    throw new AxisFault(
    "Server was shutdown as the async response take too long to
    complete");
    }
    }
    }
    } finally {
    if (sender != null)
    sender.disengageModule("addressing");
    sender.cleanup();
    }

    }

    public static OMElement createPayLoad() {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/
    axis2",
    "ns1");
    OMElement method = fac.createOMElement("echo", omNs);
    OMElement value = fac.createOMElement("value", omNs);
    value.setText("Hello , my first service utilization");
    method.addChild(value);
    return method;
    }

    }


    The only customization I did was to set Options to:

    opts.setTo(new EndpointReference(
    "http://localhost:8080/axis2/services/HelloAxisWorld"));

    - I get the following runtime exception:

    Exception in thread "main" org.apache.axis2.AxisFault: Unable to
    engage module : addressing


    I hope you can help me solving this problem
    Thank you very much in advance
    Carmelo
    carmelo, Oct 20, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cod3nam3

    jsp client for axis2 webservice

    cod3nam3, Oct 29, 2007, in forum: Java
    Replies:
    10
    Views:
    976
    cod3nam3
    Nov 10, 2007
  2. Andrew Thompson

    Re: jsp client for axis2 webservice

    Andrew Thompson, Oct 29, 2007, in forum: Java
    Replies:
    0
    Views:
    402
    Andrew Thompson
    Oct 29, 2007
  3. MC
    Replies:
    0
    Views:
    948
  4. Michael Post
    Replies:
    5
    Views:
    1,106
    Arne Vajhøj
    Aug 22, 2009
  5. Replies:
    1
    Views:
    382
    John Saunders
    Nov 14, 2006
Loading...

Share This Page