soapjax-wssoaphandler

JAX-WS SoapHandler with large messages: OutOfMemoryError


Using JAX-WS 2, I see an issue that others have spoken about as well. The issue is that if a SOAP message is received inside a handler, and that SOAP message is large - whether due to inline SOAP body elements that happen to have lots of content, or due to MTOM attachments - then it is dangerously easy to get an OutOfMemoryError.

The reason is that the call to getMessage() seems to set off a chain of events that involve reading the entire SOAP message on the wire, and creating an object (or objects) representing what was on the wire.

For example:

...
public boolean handleMessage(SOAPMessageContext context)
{
    // for a large message, this will cause an OutOfMemoryError
    System.out.println( context.getMessage().countAttachments() );
...

My question is: is there a known mechanism/workaround for dealing with this? Specifically, it would be nice to access the SOAP part in a SOAP message without forcing the attachments (if MTOM for example) to also be vacuumed up.


Solution

  • There's actually a JAX-WS RI (aka Metro) specific solution for this which is very effective.

    See https://javaee.github.io/metro/doc/user-guide/ch02.html#efficient-handlers-in-jax-ws-ri. Unfortunately that link is now broken but you can find it on WayBack Machine. I'll give the highlights below:

    The Metro folks back in 2007 introduced an additional handler type, MessageHandler<MessageHandlerContext>, which is proprietary to Metro. It is far more efficient than SOAPHandler<SOAPMessageContext> as it doesn't try to do in-memory DOM representation.

    Here's the crucial text from the original blog article:

    MessageHandler:

    Utilizing the extensible Handler framework provided by JAX-WS Specification and the better Message abstraction in RI, we introduced a new handler called MessageHandler to extend your Web Service applications. MessageHandler is similar to SOAPHandler, except that implementations of it gets access to MessageHandlerContext (an extension of MessageContext). Through MessageHandlerContext one can access the Message and process it using the Message API. As I put in the title of the blog, this handler lets you work on Message, which provides efficient ways to access/process the message not just a DOM based message. The programming model of the handlers is same and the Message handlers can be mixed with standard Logical and SOAP handlers. I have added a sample in JAX-WS RI 2.1.3 showing the use of MessageHandler to log messages and here is a snippet from the sample:

    public class LoggingHandler implements MessageHandler<MessageHandlerContext> {
        public boolean handleMessage(MessageHandlerContext mhc) {
            Message m = mhc.getMessage().copy();
            XMLStreamWriter writer = XMLStreamWriterFactory.create(System.out);
            try {
                m.writeTo(writer);
            } catch (XMLStreamException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        public boolean handleFault(MessageHandlerContext mhc) {
            ..... 
            return true;
        }
    
        public void close(MessageContext messageContext) {    }
    
        public Set getHeaders() {
            return null;
        }
    }
    

    (end quote from 2007 blog post)

    You can find a full example in the Metro GitHub repo.