1 package userguide.clients; 2 3 import org.apache.axis2.Constants; 4 import org.apache.axis2.addressing.AddressingConstants; 5 import org.apache.axis2.addressing.EndpointReference; 6 import org.apache.axis2.clientapi.AsyncResult; 7 import org.apache.axis2.clientapi.Call; 8 import org.apache.axis2.clientapi.Callback; 9 import org.apache.axis2.engine.AxisFault; 10 import org.apache.axis2.om.OMElement; 11 import org.apache.axis2.om.OMOutput; 12 13 import javax.xml.namespace.QName ; 14 import javax.xml.stream.XMLOutputFactory; 15 import javax.xml.stream.XMLStreamException; 16 import java.io.StringWriter ; 17 18 24 public class EchoNonBlockingDualClient { 25 private static EndpointReference targetEPR = new EndpointReference(AddressingConstants.WSA_TO, 26 "http://127.0.0.1:8080/axis2/services/MyService/echo"); 27 28 public static void main(String [] args) { 29 try { 30 OMElement payload = ClientUtil.getEchoOMElement(); 31 32 Call call = new Call(); 33 call.setTo(targetEPR); 34 35 call.engageModule(new QName (Constants.MODULE_ADDRESSING)); 38 call.setTransportInfo(Constants.TRANSPORT_HTTP, Constants.TRANSPORT_HTTP, true); 39 40 Callback callback = new Callback() { 42 public void onComplete(AsyncResult result) { 43 try { 44 StringWriter writer = new StringWriter (); 45 result.getResponseEnvelope().serializeWithCache( 46 new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(writer))); 47 writer.flush(); 48 49 System.out.println(writer.toString()); 50 51 } catch (XMLStreamException e) { 52 reportError(e); 53 } 54 } 55 56 public void reportError(Exception e) { 57 e.printStackTrace(); 58 } 59 }; 60 61 call.invokeNonBlocking("echo", payload, callback); 63 64 while (!callback.isComplete()) { 66 Thread.sleep(1000); 67 } 68 call.close(); 70 71 } catch (AxisFault axisFault) { 72 axisFault.printStackTrace(); 73 } catch (Exception ex) { 74 ex.printStackTrace(); 75 } 76 77 } 78 } 79 | Popular Tags |