我正在我的 Web 应用程序中实现 AXIS2 服务。我们客户的生产盒有点不稳定,所以当性能下降时我想提醒一下。具体来说:
- 请求进入我的 AXIS2 服务
- 测量请求花费的时间
- 如果时间大于 X,则记录错误
所以我写了一个这样的 AXIS2 模块:
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;
}
}
之后,我适当地编辑了 module.xml、axis2.xml,创建了 *.mar 文件并运行了应用程序。
然而,似乎
HttpServletRequest r = ( HttpServletRequest )msgContext.getProperty( HTTPConstants.MC_HTTP_SERVLETREQUEST )
一片空白。那是出乎意料的。
所以我的问题是:
- 如何访问 AXIS2 模块中的 servlet 请求?
- 如果不允许这样做,我有什么替代方法来跟踪请求开始处理和结束处理之间的时间?
- 我应该使用其他一些现有的 AXIS2 功能,可以给我同样的结果吗?
非常感谢,Dave C.