package sample;

import java.util.Iterator;

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.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;


public class HelloWorldClient {

  public static void main(String[] args) {
    try {
      Options options = new Options();
      options.setTo(new EndpointReference("http://localhost:8080/helloworld/HelloWorldService"));
      options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
      options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Boolean.TRUE);

      ServiceClient sender = new ServiceClient();
      sender.setOptions(options);

      for(int i=0;i<5;i++){
        OMElement response = sender.sendReceive(getBody());

        sender.cleanupTransport();

        // process response
        Iterator<?> it = response.getChildElements();
        while(it.hasNext()){
          System.out.println(it.next());
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static OMElement getBody(){
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://sample/", "tns");

    OMElement ome = fac.createOMElement("multiHello", omNs);

    return ome;

  }

}
