title: Thread Safety in Jax-WS Request-Response Payload Logging
body: <p>I am working on a SoapHandler implementation. Basically my aim is to get both request and response payloads and insert them to the database.</p>
  
  <p>Although I can get the request and response payloads, I couldn't make sure if my code is working thread-safe. In other words, I am not sure if responses match with the proper requests.</p>
  
  <pre><code>public boolean handleMessage(SOAPMessageContext mContext) {
  
      boolean isResponse = (Boolean) mContext
              .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
  
      if (!isResponse) {
          try {
              mContext.put("currentStream", new ByteArrayOutputStream());
              mContext.getMessage().writeTo((OutputStream) mContext.get("currentStream"));
  
          } catch (SOAPException | IOException e) {
              e.printStackTrace();
          }
      } else {
          try {
              mContext.getMessage().writeTo(
                      (OutputStream) mContext.get("currentStream"));
              System.out.println(((OutputStream) mContext.get("currentStream"))
                      .toString());
              ((OutputStream) mContext.get("currentStream")).flush();
          } catch (SOAPException | IOException e) {
              e.printStackTrace();
          }
      }
  
      return true;
  }
  </code></pre>
  
  <p>I found this in JCP specs:</p>
  
  <blockquote>
    <p>9.3.3 Handler Implementation Considerations </p>
    
    <p>Handler instances may be pooled by a JAX-RPC runtime system. All instances of a specific handler are 
    considered equivalent by a JAX-RPC runtime system and any instance may be chosen to handle a particular 
    message. Different handler instances may be used to handle each messages of an MEP. Different threads 
    may be used for each handler in a handler chain, for each message in an MEP or any combination of the 
    two. Handlers should not rely on thread local state to share information. Handlers should instead use the 
    message context, see section 9.4. </p>
    
    <p>9.4 Message Context </p>
    
    <p>Handlers are invoked with a message context that provides methods to access and modify inbound and 
    outbound messages and to manage a set of properties. 
    Different types of handler are invoked with different types of message context. Sections 9.4.1 and 9.4.2 
    describe MessageContext and LogicalMessageContext respectively. In addition, JAX-RPC bindings 12
    may define a message context subtype for their particular protocol binding that provides access to protocol 
    specific features. Section10.3 describes the message context subtype for the JAX-RPC SOAP binding.</p>
  </blockquote>
  
  <p><a href="http://download.oracle.com/otn-pub/jcp/jaxrpc-2.0-edr2-spec-oth-JSpec/jaxrpc-2_0-edr2-spec.pdf?AuthParam=1431341511_1ac4403a34d7db108bce79eda126df49" rel="nofollow">http://download.oracle.com/otn-pub/jcp/jaxrpc-2.0-edr2-spec-oth-JSpec/jaxrpc-2_0-edr2-spec.pdf?AuthParam=1431341511_1ac4403a34d7db108bce79eda126df49</a></p>
  
  <p>Does this imply that a new MessageContext object is created for each request (in which case I think the code will be thread safe), or the same MessageContext object can be used for multiple requests (then my code will not be thread safe).</p>
  
  <p>Any help / alternative solution will be appreciated.</p>