I have a scenario where I need to implement a mechanism to show in log the time taken for my rest api to execute. I am planning to have a PreProcessorInterceptor containing something like
long startTime = system.currenttimemillis()
And in PostProcessInterceptor I will get the endTime and planning to subtract the startTime from endTime. How can I get the starttime in my PostProcessInterceptor ?
Using a ContainerRequestFilter and a ContainerResponseFilter you could add an property to the request:
@Provider
@PreMatching
public class TimerRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.setProperty("startTime", System.currentTimeMillis());
}
}
@Provider
public class TimerResponseFilter implements ContainerResponseFilter {
private static final Logger LOG = LoggerFactory.getLogger(TimerResponseFilter.class);
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
long startTime = (long) requestContext.getProperty("foo.bar.startTime");
LOG.info("Execution took {}ms", System.currentTimeMillis() - startTime);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.