You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Christophe Noël <ch...@gmail.com> on 2011/03/08 09:14:27 UTC

Client for single in , many out message pattern

Dear all,

Starting from the well-known newbie client example (copied below), I
would like to know how to implement a client that send one message to
the target WS, then receives many message back (notifications) from
the WS.

-> request

<- response

[..]

<- response


Would you have a tip or an example in order to achieve this ?

Thank you very much.

Christophe Noel.


The example I was talking about:

//client classes declared static obviates multi-threaded activity on client side

//options.setuseSeparateListener(true) will apprise the engine to use
2 separate channels
//one for send and one for receive
//try this first create a callback object which will receive envelope
in onComplete
class MyCallback extends Callback {

  public void onComplete(AsyncResult result) {
     // Get the response SOAP envelope from the result.
     SOAPEnvelope envelope = result.getResponseEnvelope();
     log.debug("OnComplete has received the envelope from Axis2 Engine
envelope="+envelope);
     // Process SOAP envelope.
   }

   public void onError(Exception e) {
     // Write here what you want to do in case of an error.
    log.debug("AXIS2 Engine has produced an error message="+e.getMessage());
   }
}

//client code informs client to use 2 separate channels one for send
and for receive
//with client.setUseSeparateListener(true)
try {
  // first create the payload of the message
  OMElement payload = getPayload(); // add your payload here

  // create an instance of service client
  ServiceClient serviceClient = new ServiceClient();

  // create an options object to pass parameters to service client
  Options options = new Options();

  // set EPR of the web service
  options.setTo(new EndpointReference
    ("http://www.sample-web-service.com/StockQuote/getQuote"));

  // inform Axis2 engine that you need to use a different channel for
the response
  options.setUseSeparateListener(true);

// Create a callback object which will receive response in onComplete
or exception in onError
  Callback callback = new MyCallback();

//But for the 2 channel approach to work, there
//  should be a message correlation mechanism. Apache Axis2 uses the
WS-Addressing
//  specification for message correlation so lets engage addressing
module here
  serviceClient.engageModule(Constants.MODULE_ADDRESSING);

//set the options for the client (useSeparateListener)
  serviceClient.setOptions(options);

  // invoke service apprising callback object when transport has
completed (or errored out)
  serviceClient.sendReceiveNonBlocking(payload, callback);

//use callback.isComplete to determine when the callback has completed
processing e.g.
  while(callback.isComplete()==false) ; //tarry here until the
transport has received its envelope

} catch (AxisFault axisFault) {
  axisFault.printStackTrace();
 }