Search in sources :

Example 1 with AxisCallback

use of org.apache.axis2.client.async.AxisCallback in project wso2-axis2-transports by wso2.

the class TCPEchoRawXMLTest method testEchoXMLASync.

public void testEchoXMLASync() throws Exception {
    OMElement payload = createPayload();
    Options options = new Options();
    options.setTo(targetEPR);
    options.setTransportInProtocol(Constants.TRANSPORT_TCP);
    options.setAction(Constants.AXIS2_NAMESPACE_URI + "/" + operationName.getLocalPart());
    AxisCallback axisCallback = new AxisCallback() {

        public void onMessage(MessageContext msgContext) {
            try {
                msgContext.getEnvelope().serialize(StAXUtils.createXMLStreamWriter(System.out));
                finish = true;
            } catch (XMLStreamException e) {
                onError(e);
            }
        }

        public void onFault(MessageContext msgContext) {
            try {
                msgContext.getEnvelope().serialize(StAXUtils.createXMLStreamWriter(System.out));
                finish = true;
            } catch (XMLStreamException e) {
                onError(e);
            }
        }

        public void onError(Exception e) {
            log.info(e.getMessage());
            finish = true;
        }

        public void onComplete() {
            finish = true;
        }
    };
    ServiceClient sender = new ServiceClient(configContext, clientService);
    sender.setOptions(options);
    sender.sendReceiveNonBlocking(operationName, payload, axisCallback);
    int index = 0;
    while (!finish) {
        Thread.sleep(1000);
        index++;
        if (index > 10) {
            throw new AxisFault("Server was shutdown as the async response take too long to complete");
        }
    }
    sender.cleanup();
}
Also used : AxisFault(org.apache.axis2.AxisFault) Options(org.apache.axis2.client.Options) AxisCallback(org.apache.axis2.client.async.AxisCallback) XMLStreamException(javax.xml.stream.XMLStreamException) ServiceClient(org.apache.axis2.client.ServiceClient) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.axis2.context.MessageContext) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with AxisCallback

use of org.apache.axis2.client.async.AxisCallback in project wso2-axis2-transports by wso2.

the class TCPTwoChannelEchoRawXMLTest method testEchoXMLCompleteASync.

public void testEchoXMLCompleteASync() throws Exception {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
    OMElement method = fac.createOMElement("echoOMElement", omNs);
    OMElement value = fac.createOMElement("myValue", omNs);
    value.setText("Isaac Asimov, The Foundation Trilogy");
    method.addChild(value);
    ServiceClient sender;
    try {
        Options options = new Options();
        options.setTo(targetEPR);
        options.setTransportInProtocol(Constants.TRANSPORT_TCP);
        options.setUseSeparateListener(true);
        options.setAction(operationName.getLocalPart());
        AxisCallback axisCallback = new AxisCallback() {

            public void onMessage(MessageContext msgContext) {
                try {
                    msgContext.getEnvelope().serialize(StAXUtils.createXMLStreamWriter(System.out));
                    finish = true;
                } catch (XMLStreamException e) {
                    onError(e);
                }
            }

            public void onFault(MessageContext msgContext) {
                try {
                    msgContext.getEnvelope().serialize(StAXUtils.createXMLStreamWriter(System.out));
                    finish = true;
                } catch (XMLStreamException e) {
                    onError(e);
                }
            }

            public void onError(Exception e) {
                log.info(e.getMessage());
                finish = true;
            }

            public void onComplete() {
                finish = true;
            }
        };
        AxisService serviceClient = Utils.createSimpleServiceforClient(serviceName, Echo.class.getName(), operationName);
        sender = new ServiceClient(configContext, serviceClient);
        sender.setOptions(options);
        sender.sendReceiveNonBlocking(operationName, method, axisCallback);
        int index = 0;
        while (!finish) {
            Thread.sleep(1000);
            index++;
            if (index > 10) {
                throw new AxisFault("Server was shutdown as the async response take too long to complete");
            }
        }
    } finally {
        if (finish) {
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) Options(org.apache.axis2.client.Options) OMNamespace(org.apache.axiom.om.OMNamespace) AxisCallback(org.apache.axis2.client.async.AxisCallback) AxisService(org.apache.axis2.description.AxisService) OMElement(org.apache.axiom.om.OMElement) XMLStreamException(javax.xml.stream.XMLStreamException) OMFactory(org.apache.axiom.om.OMFactory) XMLStreamException(javax.xml.stream.XMLStreamException) ServiceClient(org.apache.axis2.client.ServiceClient) MessageContext(org.apache.axis2.context.MessageContext)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)2 OMElement (org.apache.axiom.om.OMElement)2 AxisFault (org.apache.axis2.AxisFault)2 Options (org.apache.axis2.client.Options)2 ServiceClient (org.apache.axis2.client.ServiceClient)2 AxisCallback (org.apache.axis2.client.async.AxisCallback)2 MessageContext (org.apache.axis2.context.MessageContext)2 OMFactory (org.apache.axiom.om.OMFactory)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 AxisService (org.apache.axis2.description.AxisService)1