Search code examples
apache-axisaxis

Accessing an HttpServletRequest from inside an AXIS2 module


I am implementing AXIS2 services in my web application. Our client's production boxes are a bit flaky, so I want a heads up when performance degraded. Specifically:

  • request comes into my AXIS2 service
  • measure the time that the request takes
  • if the time is greater than X, log an error

So I wrote an AXIS2 module like this:

public class PerformanceHandler extends AbstractHandler implements Handler {
protected Logger logger = null;

public PerformanceHandler() {
    logger = LoggerFactory.getLogger( this.getClass() );
}

public InvocationResponse invoke( MessageContext msgContext ) throws AxisFault {
    HttpServletRequest r = ( HttpServletRequest )msgContext.getProperty( HTTPConstants.MC_HTTP_SERVLETREQUEST );
    if( msgContext.getFLOW() == MessageContext.IN_FLOW || msgContext.getFLOW() == MessageContext.IN_FAULT_FLOW ) {
        // incoming request
        Date timeIn = new Date( System.currentTimeMillis() );
        r.setAttribute( this.getClass().getName() + ".timeIn", timeIn );
        if( logger.isDebugEnabled() ) {
            logger.debug( "Request " + r.toString()  + " started processing at " + timeIn );
        }
    } else {
        // outgoing response
        Date timeIn = ( Date )r.getAttribute( this.getClass().getName() + ".timeIn" );
        Date timeOut = new Date( System.currentTimeMillis() );
        if( logger.isDebugEnabled() ) {
            logger.debug( "Request " + r.toString()  + " finished processing at " + timeOut );
        }
        long delta = timeOut.getTime() - timeIn.getTime();
        if( delta > 300 ) { // todo: parameterize the delta threshold
            logger.error( "Request " + r.toString() + " took " + delta + "ms to process." );
        }
    }

    return InvocationResponse.CONTINUE;
}
}

After that, I edited the module.xml, axis2.xml appropriately, created the *.mar file and ran the app.

However, it seems that

HttpServletRequest r = ( HttpServletRequest )msgContext.getProperty( HTTPConstants.MC_HTTP_SERVLETREQUEST )

is null. That was unexpected.

So my questions are:

  • How can I access the servlet request in an AXIS2 module?
  • If this is not allowed, what's the alternative for me to track the time between request starting processing and ending processing?
  • I should be using some other existing AXIS2 functionality that can give me the same kind of result?

Many thanks in advance, Dave C.


Solution

  • Instead of using HttpServletRequest, you should use the OperationContext (which you can get from the MessageContext using the getOperationContext method) to store the timestamp of the request. The operation context is the same for an incoming request and the corresponding response.