Jersey async request handling with programmatical resource registration

Kom*_*ulu 4 java jax-rs jersey jersey-client jersey-2.0

We used org.glassfish.jersey.server.model.ResourceMethod$Builder

to register the method and the handler.

ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod);
methodBuilder.produces(restContext.getProduceContent()).handledBy(inflector);
methodBuilder.consumes(restContext.getConsumeContent()).handledBy(inflector);
Run Code Online (Sandbox Code Playgroud)

The handler class implements the org.glassfish.jersey.process.Inflector<ContainerRequestContext, Response>

public class CommonMethodInflector implements Inflector<ContainerRequestContext, Response>
{
 @Override
    public Response apply(ContainerRequestContext request)
    {
      //sync bloc
      //using resqest object we do processing in different maner
        incRestFeRequestCounters(request.getMethod());
        Response response = processIncoming(request);`enter code here`
     }
}
Run Code Online (Sandbox Code Playgroud)

Could you please help us in creating the async handler.

our requirement in short:

  1. At runtime only we know the http method and other resources to register. So, we can not use annotations for resource & httpMethod registration. We need only programmatic resource registration.

  2. In handler We need the request object so that we can access what method and what json body in it.

  3. 我们需要做出异步响应,因为我们在处理请求中正在进行大量操作。

Mic*_*dos 6

您需要做的第一件事是暂停资源方法:

ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod)
    .suspend(AsyncResponse.NO_TIMEOUT, TimeUnit.Seconds);
Run Code Online (Sandbox Code Playgroud)

那么你几乎没有选择如何以异步模式处理请求:

“任意”处理程序类

builder.addMethod("GET")
        // Suspend without time limit.
        .suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.SECONDS)
        .handledBy(MyHandler.class, MyHandler.class.getMethod("handle", AsyncResponse.class));
Run Code Online (Sandbox Code Playgroud)

public class MyHandler {

    public void handle(final @Suspended AsyncResponse response) {
        Executors.newSingleThreadExecutor().submit(new Runnable() {
            @Override
            public void run() {
                // Simulate long-running operation.
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException ie) {
                    // NOOP
                }

                response.resume("Hello World!");
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

偏转器类

Resource.builder("helloworld")
        .addMethod("GET")
        // Suspend without time limit.
        .suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.SECONDS)
        // Can be registered only as a class otherwise the
        // @Context injection would be out of request scope.
        .handledBy(MyAsyncInflector.class);
Run Code Online (Sandbox Code Playgroud)

public class MyAsyncInflector implements Inflector<ContainerRequestContext, Response> {

    @Context
    private AsyncResponse response;

    @Override
    public Response apply(final ContainerRequestContext context) {
        Executors.newSingleThreadExecutor().submit(new Runnable() {
            @Override
            public void run() {
                // Simulate long-running operation.
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException ie) {
                    // NOOP
                }

                response.resume("Hello World!");
            }
        });

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

无名变形者

Resource.builder("helloworld")
        .addMethod("GET")
        // Suspend without time limit.
        .suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.SECONDS)
        .handledBy(new Inflector<ContainerRequestContext, Response>() {

            @Inject
            private javax.inject.Provider<AsyncResponse> responseProvider;

            @Override
            public Response apply(final ContainerRequestContext context) {
                // Make sure we're in request scope.
                final AsyncResponse response = responseProvider.get();

                Executors.newSingleThreadExecutor().submit(new Runnable() {
                    @Override
                    public void run() {
                        // Simulate long-running operation.
                        try {
                            Thread.sleep(1000);
                        } catch (final InterruptedException ie) {
                            // NOOP
                        }

                        response.resume("Hello World!");
                    }
                });

                return null;
            }
        });
Run Code Online (Sandbox Code Playgroud)