Change example to use the cookie

To change the Keep-Alive example to use the cookie you can modify the following methods.

The method createService ( ) in IncidentManagementServiceUtility.java file:

public static IncidentManagementStub createService(Map arguments) 
throws Exception
  {
    String host = (String) arguments.get(ARGUMENT_HOST);
    String port = (String) arguments.get(ARGUMENT_PORT);
    String address = "http://" + host + ":" + port + "/SM/7/ws";
    IncidentManagementStub stub = new IncidentManagementStub(address);
    
    stub._getServiceClient().getOptions().setManageSession(true);
    stub._getServiceClient().getOptions().setProperty
       (HTTPConstants.REUSE_HTTP_CLIENT,true);
    // set connection: close
    //Header hdr = new Header(HTTPConstants.HEADER_CONNECTION, 
        HTTPConstants.HEADER_CONNECTION_CLOSE);
    //ArrayList<Header> headers = new ArrayList<Header>();
    //headers.add(hdr);
    //stub._getServiceClient().getOptions().setProperty
       (HTTPConstants.HTTP_HEADERS, headers);
    stub._getServiceClient().getOptions().setProperty
        (Constants.Configuration.ENABLE_MTOM,
        Constants.VALUE_TRUE);
    ServiceUtility.initServiceAuthentication(stub, arguments);

    return stub;
  }

The method createIncidents() in CreateIncidentSample.java file:

public void createIncidents() throws Exception, IOException
  {
    /* Open a port to the Incident Management Web Service */
    IncidentManagementStub stub = 
       IncidentManagementServiceUtility.createService(arguments);
    int totalIM = 10;
    
    /* Create details about the new incident */
    for (int i = 1; i <= totalIM; i++)
    {      
      if (i == totalIM)
      {
        // close the connection if this is the last request
        Header hdr = new Header(HTTPConstants.HEADER_CONNECTION, 
           HTTPConstants.HEADER_CONNECTION_CLOSE);
        ArrayList<Header> headers = new ArrayList<Header>();
        headers.add(hdr);
        stub._getServiceClient().getOptions().setProperty
           (HTTPConstants.HTTP_HEADERS, headers);  
      }
      
      createIncident(stub);
    }

    return;
  }

The client is responsible for echoing back this value in a Cookie header in all subsequent POST requests. If the client fails to do this, the servlet container will quickly run out of sessions.

If a client request causes any Service Manager Server error or exception then this session will be terminated by the Service Manager Server. Once this happens the current JSESSIONID becomes invalid and a new JSESSIONID will be returned on the following client request. The SOAP client should echo back the new JSESSIONID for the subsequent requests to avoid the user login/logout overhead and dangling sessions saturation.