users@jersey.java.net

[Jersey] Re: ManagedAsync and ContainerRequestFilter

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Wed, 18 Dec 2013 20:41:57 +0100

Please, file a new enhancement request.

What you are asking for is currently not possible. Only the resource method itself is invoked asynchronously at the moment. We may need to change the implementation to also run all the resource-bound filters asynchronously.

Marek

On 15 Dec 2013, at 16:22, Alden Quimby <aldenquimby_at_gmail.com> wrote:

> I am using a ContainerRequestFilter to handle authorization, and ManagedAsync on all endpoints for scaling. My authorization filter hits a remote service, but when the filter is run, Jersey hasn't yet added the current thread to it's internal ExecutorService, so I'm losing the benefit of ManagedAsync.
>
> How can I set up my ContainerRequestFilter to be asynchronous, and run AFTER ManagedAsync is recognized?
>
>
> Here is my filter:
>
>
> @Priority(Priorities.AUTHORIZATION)
> public class AuthorizationFilter implements ContainerRequestFilter
> {
> @Inject
> private AuthorizationService authSvc;
>
> @Override
> public void filter(ContainerRequestContext requestContext) throws IOException
> {
> String authToken = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
>
> // HITS A REMOTE SERVER
> AuthorizationResponse authResponse = authSvc.authorize(authToken);
>
> if (!authResponse.isAuthorized())
> {
> requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED)
> .entity("unauthorized!")
> .build());
> }
> }
> }
>
>
>
> And here's a resource:
>
>
> @Path("/stuff")
> @Produces(MediaType.APPLICATION_JSON)
> public class StuffResource
> {
> @GET
> @Path("/{id}")
> @ManagedAsync
> public void getById(@PathParam("id") long id, @Suspended final AsyncResponse ar)
> {
> Stuff s;
>
> // HIT THE DATABASE FOR STUFF
>
> ar.resume(s);
> }
> }
>