1

In perfect world, I would imagine that there is an annotation like @QoS(10, CONSTANT_RETURN_OBJECT) which can be placed around java methods and if they take longer than the specified time (10 seconds in this example) to execute then the method execution is essentially considered timed out and we return a pre-packaged CONSTANT_RETURN_OBJECT defined by the developer to indicate empty results.

But realistically, what is a good approach supported by or recommended for enforcing how long a jersey-server method can run for before we say its too darn long and just return and go on our merry way?

And its a given (for my question here) that you have no control over the client side that is calling jersey-server so you can NOT go set a timeout there ... which is why you are enforcing some reasonable ones on the server side.

UPDATE:

I guess what I am asking to some degree is if there is an annotation that would essentially wrap a method call as an Executor driven task and run it with a timeout? Sort of like what the code written in the answers for these two posts does:

Community
  • 1
  • 1
pulkitsinghal
  • 3,855
  • 13
  • 45
  • 84

1 Answers1

1

The way to do this would be to implement a servlet filter. A basic filter looks like this:

public class TimeoutFilter implements Filter {

  @Override
  public void doFilter(final ServletRequest servletRequest,
                       final ServletResponse servletResponse,
                       final FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest req = (HttpServletRequest)servletRequest;

    try {
      // Execute this with a time limit
      chain.doFilter(servletRequest, servletResponse);
    }
    catch (Exception e) {
      // Handle exceptions here; you should state the exceptions explicitly
      // rather than just catching Exception
    }
  }
}

You can then set up an Executor or whatever in the filter to provide a time limit for the request.

  • Its not that this isn't a good approach ... its just that I'm looking for an answer that highlights some tooling that already exists and makes the task as simple as `@QoS(10, CONSTANT_RETURN_OBJECT)` ... I already have the `Object result = future.get(5, TimeUnit.SECONDS);` implemented inside relevant methods ... what I'm looking for is something more granular and convenient that doesn't change my code. Filter isn't granular at a method level for a Jersey resource class. But still a good idea, thanks! – pulkitsinghal Jul 23 '13 at 06:17
  • 1
    In that case your approach is slightly different, take a look at `ResourceFilterFactory`. This allows you to create filters dependent on annotations so you can manage your QoS parameters on a per-class or per-method basis. –  Jul 23 '13 at 10:34