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:
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:
Many thanks in advance, Dave C.
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.