The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Invoking Web Services using Apache Axis2
Subject:  Slightly different asynchronous model in Axis 1.3
Date:  2007-08-10 00:07:19
From:  alpatino


Your examples in scenarios number 2 and 3 use now deprecated methods. To invoke asynchronously an operation using the new model in Axis2 1.3 see the following example, as you can see the difference is very slight but is good to know this!



package com.axis2.clients;

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 Scenario2Client {

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/Tutorial/services/MyService"));
// 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;
}

}


 Feed java.net RSS Feeds