title: Pool the JAXWS wsimport service object - possible?
body: <p>we are using JAXWS metro client to interface with a 3rd Party .Net web service. We need to maintain state with the web service. 
  So, here's the scenario. There are several user applications that would invoke the metro client which in turn invokes the .Net web service.</p>
  
  <p>I've run the wsimport tool and generated the necessary classes. 
  But since, we have to maintain the state, I'm thinking implement object pool of the service class. 
  That way, each user app is always married to the specific service object that it is using. 
  So, the flow would be: </p>
  
  <p>COSServiceImpl -> COSServiceFactory instantiates/maintains COSService (wsimport generated service class that will be pooled) -> .Net web service.</p>
  
  <p>So, the implementation is as follows. Anyone has any better suggestions? Thoughts?</p>
  
  <p>UserApp.java</p>
  
  <pre><code>COSServiceImpl impl = new COSServiceImpl();
  ClaimantAccount claimantAccount = impl.getClaimantAccount(String claimantID)
  </code></pre>
  
  <p>COSServiceImpl.java
  </p>
  
  <pre><code>public ClaimantAccount getClaimantAccount(String claimantID) {
  ICOSService port = COSServiceFactory.getCOSServicePort();
  ClaimantInfo info = port.retrieveClaimantInfo(claimantID);
  ClaimantAccount account = new ClaimantAccount();
  account.setXXX(info.getXXX);
  return account;
  }
  </code></pre>
  
  <p>COSServiceFactory.java
  </p>
  
  <pre><code>public class COSServiceFactory extends BasePoolableObjectFactory&lt;COSService&gt; {
  
  private static GenericObjectPool&lt;COSService&gt; servicePool = null;
  
  static {
  try { 
  init();
  } catch(Exception e) {
  throw new ExceptionInInitializerError(e);
  }
  }
  
  public static void init() {
  servicePool = new GenericObjectPool&lt;COSService&gt;(new COSServiceFactory());
  for (int i=0; i &lt; poolSize; i++) {
  servicePool.addObject();
  }
  
  public COSService makeObject() throws Exception {
  URL wsdlURL = null;
  service = new COSService(wsdlURL, new QName(nameSpace,localPart) );
  return service;
  }
  
  private static COSService getCOSService() {
      COSService service = null;
      try {
          service = (COSService) servicePool.borrowObject();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return service;
      }
  
   public static ICOSService getWebServicePort() {
      ICOSService port = getCOSService().getWSHttpBindingICOSService();
      BindingProvider bindingProvider = (BindingProvider) port;
  // Is there any other place to set the request timeout, may be a handler???
      bindingProvider.getRequestContext().put("com.sun.xml.internal.ws.request.timeout", Config.getIntProperty("request.timeout")); 
      return port;
      }
  </code></pre>
  
  <p>Also, is there any other place where we can set the request timeout? Is it okay do it this way? With the above code, I don't think we are modifying the port object. I haven't tested this yet, but will the request timeout property work?</p>
  
  <p>Thanks and appreciate your comments.</p>
  
  <p>Vijay Ganapathy</p>