-->

Spring: Response time

2019-06-14 09:15发布

问题:

I have a SOAP webservice(spring ws)and need to save the response time of every request that I receive. I could create a servlet filter that measures time difference between HTTP Request and HTTP Response. But, I need to log the response time together with some values that I read from soap request envelope, and since at that time request is raw and needs to be unmarshalled, that's an expensive and redundant operation to unmarshall for every request.

So is there a way to calculate it using SpringWS? like in a payloadInterceptor?

回答1:

Yes, implementing an EndpointInterceptor is the best fit for this task, as it gives you access to the SOAP messages through the MessageContext. See the Reference Documenation.



回答2:

I think you can use two tools.

  1. AspectJ with its annotation @Before and @AfterReturning. The pointcut could be the method that receives the request (@WebMethod).

    @Before("call([package, class and method that receives the request]")
    public void before(JoinPoint joinPoint) throws Throwable {
        ...
    }
    
    @AfterReturning(pointcut = "call([package, class and method that receives the request])", returning = "result")
    public void after(JoinPoint joinPoint, Object result) throws Throwable {
        ...
    }
    

The JoinPoint object has the information of the method's parameters

  1. Override the method handleMessage of a custom class that implements the SOAPHandler class. This method will be executed every time a SOAP request is received.

I hope this can give you ideas to resolve your problem.