Announcement Announcement Module
No announcement yet.
Combining short and long asynchronous requests in Spring MVC Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining short and long asynchronous requests in Spring MVC


    I have a Spring MVC endpoint /work whose processing is IO bound and can take a few seconds to complete. Enter Servlet 3 asynchronous requests. I can return a Callable<Foo> or DeferredResult<Foo> and free up my servlet container threads to do something useful while that processing is happening.

    Now it's likely that other people will make the identical request soon afterwards. There's no point repeating the work that was done and hurting response times, so I'm going to cache the result of the work the first time it completes, and want to return that cached result. The result of this is that the same handler method can either take a few seconds to complete, or a couple of milliseconds.

    My problem is that I can only give the handler method one signature, and I've defined it as returning Callable<Foo>. I can't figure out how to kill this overhead for the short requests; as it stands, I'd be creating a Callable<Foo>, enqueue it behind long-running tasks and wait for a thread in the thread pool to get its hands on it, and all to eventually serve the response I had in hand. I'd like to avoid all this.

    If I use a DeferredResult<Foo> instead of a Callable<Foo>, things get a bit better performance-wise, but I've taken the processing out of a Spring managed thread, lost Spring Security's SecurityContext, and have polluted non-web code with the Spring-specific DeferredResult.

    Is there a better way to do this?


  • #2
    This question is probably a better candidate for StackOverflow, perhaps with the "spring" and "spring-mvc" tags.

    Could you post your question there?
    The forums are moving to stackoverflow, a page explaining all this will be available there in a few days: