I have a servlet which is used to fetch data from many third party REST datapoints, integrate all the data and report the data in a HTML format. I also have a filter which has the following flow -
Now if I use Asynchronous servlet using AsyncContext context = request.getAsyncContext();
, which will talk to the same REST datapoints but as and when data is ready, it will write to the response stream instead of waiting for all the REST data points to respond, how would I re write my filter ? Would it be attached to the thread which is responsible for flushing data from the REST data points so that once all the data is processed and flused, it will log the eventrecord ?
Is there any common pattern that I can study to understand how such use cases can be handled with Servlet 3.0's asynchronous servlets ?
I am using JDK 6.0, Tomcat 7.0.
Just add @WebFilter(urlPatterns = {"/*" }, asyncSupported = true)
in web-xml for your filter.
Or add <async-supported>true</async-supported>
I have put a bounty as I'm unsure myself how to properly support instrumenting or diagnostic filters (e.g. Codahales metrics filters).
While adding <async-supported>true</async-supported>
to your filters will certainly make them appear to work it may not get the results you expect (in the case of metrics all your requests will appear to be very fast).
It may seems like a good idea to get the AsyncContext right away in the filter to bind metric data but various containers apparently have issues with this and I believe frameworks like Spring have issues as well (this might just be my older version of Spring). That is most frameworks expect the first half of the request handling to be synchronous (I maybe massively wrong on this).
Consequently it seems like the only fool proof way is to integrate filters at the framework level. For example Spring offers org.springframework.web.context.request.async.DeferredResultProcessingInterceptor
which is somewhat analogous to the AsyncContext events.
This is somewhat unfortunate as not all request maybe handled by the web framework but then again there is difference between handling the first portion of the request and actually fulfilling (ie that is now two metrics you might want to monitor).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With