If you are only concerned with http requests, an alternative would be to 
implement a servlet Filter[1].

Here is a very basic outline of what it could look like:

public class PerformanceFilter implements javax.servlet.Filter {

  public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) {
    final long start = System.currentTimeMillis();
    try {
        chain.doFilter(request, response);
    } finally {
      final long elapsed = System.currentTimeMillis() - start;
      if (elapsed > threshold) {
        logger.error((HttpServletRequest)request.getRequestURI() + " from " + 
request.getRemoteAddr() + " took " + elapsed + "ms");
      }
    }
  }
}

[1] - http://docs.oracle.com/javaee/5/api/javax/servlet/Filter.html

Brett Okken | CAMM Platform Services | Lead Architect | 816.201.6112 | 
www.cerner.com | bok...@cerner.com

-----Original Message-----
From: Dave Cherkassky [mailto:dch...@djinnsoft.com] 
Sent: Tuesday, April 30, 2013 9:08 AM
To: java-user@axis.apache.org
Subject: 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 Cherkassky
   DJiNN Software Inc
   VP of Software Development

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@axis.apache.org
For additional commands, e-mail: java-user-h...@axis.apache.org

CONFIDENTIALITY NOTICE This message and any included attachments are from 
Cerner Corporation and are intended only for the addressee. The information 
contained in this message is confidential and may constitute inside or 
non-public information under international, federal, or state securities laws. 
Unauthorized forwarding, printing, copying, distribution, or use of such 
information is strictly prohibited and may be unlawful. If you are not the 
addressee, please promptly delete this message and notify the sender of the 
delivery error by e-mail or you may call Cerner's corporate offices in Kansas 
City, Missouri, U.S.A at (+1) (816)221-1024.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@axis.apache.org
For additional commands, e-mail: java-user-h...@axis.apache.org

Reply via email to