我正在我的网络应用程序中实现AXIS2服务。我们客户的生产箱有点不稳定,所以当性能下降时我想要抬头。具体做法是:
所以我写了一个像这样的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 )
为空。那是出乎意料的。
所以我的问题是:
非常感谢, 戴夫C.
答案 0 :(得分:0)
您应该使用HttpServletRequest
(您可以使用OperationContext
方法从MessageContext
获取)来存储请求的时间戳,而不是使用getOperationContext
。传入请求和相应的响应的操作上下文相同。