package userguide.clients;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.async.Callback;
import org.apache.axis2.client.async.AsyncResult;
import org.apache.axis2.Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axiom.om.OMElement;

/**
 * Created by IntelliJ IDEA.
 * User: Jaliya Ekanayake
 * Date: May 12, 2006
 * Time: 2:09:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestClient extends Thread{

    private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyService");
    private ServiceClient sender = null;
    private int count;

    public TestLoad(ServiceClient sender,int count) {
        this.sender = sender;
        this.count=count;
    }

    public void run() {
        try {
            OMElement payload = ClientUtil.getEchoOMElement();

            Options options = new Options();
            options.setTo(targetEPR);
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
            options.setUseSeparateListener(true);
            options.setAction("urn:echo");  // this is the action mapping we put within the service.xml

            //Callback to handle the response
            Callback callback = new Callback() {
                public void onComplete(AsyncResult result) {
                    System.out.println(count+"  "+result.getResponseEnvelope().toString());
                }

                public void onError(Exception e) {
                    e.printStackTrace();
                }
            };

            //sender.engageModule(new QName(Constants.MODULE_ADDRESSING));
            sender.setOptions(options);
            sender.sendReceiveNonBlocking(payload, callback);

            //Wait till the callback receives the response.
            while (!callback.isComplete()) {
                Thread.sleep(1000);
            }
            //Need to close the Client Side Listener.

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                sender.finalizeInvoke();
            } catch (AxisFault axisFault) {
                //have to ignore this
            }
        }
     }

    public static void main(String[] args) {
        try{
         ConfigurationContext configContext = ConfigurationContextFactory.
                createConfigurationContextFromFileSystem("C:\\Projects\\axis2\\samples\\userguide\\client-repository",
                         "client-repository\\conf\\axis2.xml");

          //Non-Blocking Invocation
          for(int i=0;i<5;i++){
            ServiceClient  sender = new ServiceClient(configContext,null); //Even if we share the sender it does not work
            TestLoad tl= new TestLoad(sender,i);
            tl.start();
            Thread.sleep(100);
          }

    }catch(Exception e){
            e.printStackTrace();
        }
    }
}
