java - 使用编程资源注册的 Jersey 异步请求处理

标签 java jersey jax-rs jersey-2.0 jersey-client

我们用了 org.glassfish.jersey.server.model.ResourceMethod$Builder

注册方法和处理程序。

ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod);
methodBuilder.produces(restContext.getProduceContent()).handledBy(inflector);
methodBuilder.consumes(restContext.getConsumeContent()).handledBy(inflector);

处理程序类实现了 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`
     }
}

能否请您帮助我们创建异步处理程序。

我们的要求简而言之:

  1. 在运行时只有我们知道要注册的 http 方法和其他资源。 所以,我们不能使用注释来注册资源和 httpMethod。 我们只需要程序化资源注册。

  2. 在handler中我们需要request对象,这样我们就可以访问里面有什么方法,什么json body。

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

最佳答案

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

ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod)
    .suspend(AsyncResponse.NO_TIMEOUT, TimeUnit.Seconds);

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

“任意”处理程序类

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

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!");
            }
        });
    }
}

变形器类

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);

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;
    }
}

匿名变形器

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;
            }
        });

关于java - 使用编程资源注册的 Jersey 异步请求处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31562618/

相关文章:

java - 如何指定maven的distributionManagement组织范围?

java - Jersey 服务器 : Return a string and a non-200 return code

android - Jersey 客户端需要 Android 中缺少的 XMLInputFactory

jersey - Jersey 2.x (2.6) 所需的 Maven 依赖项

java - JAX-RS 是建立在 Servlet API 之上的吗?如何?

java - Java 中的无符号长整型

java - hibernate 和存储过程

rest - ContainerRequestContext abortWith() 不会中止

java - JDK > 8 中带有 OPTIONS header 的 Jersey JAX_RS 中存在错误?

java - 执行更新查询时MySQL语法错误异常