Java Rest API在不等待响应的情况下调用另一个Rest-在JAX-RS中 [英] Java Rest API Calling another Rest without waiting for the response - in JAX-RS

查看:159
本文介绍了Java Rest API在不等待响应的情况下调用另一个Rest-在JAX-RS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在我的项目中实施的案例.下面是一个必须实施的示例休息服务

I have a case to be implemented in my project.Below is a sample rest service which has to be implemented

    @GET
    @Path("/test/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getData(@PathParam("id") String id) {
        //Some processing to get value of String
        String result = doSomeProcessing();

        //I want to return this result to GUI and call one more rest api  
        // and end this process without waiting for response from second 
        //call

       new Thread(){
       //call second rest api
       }.start();

       return result;      

    }

这种好方法是使用新线程来调用第二休息API并返回结果,而无需等待第二休息API的响应吗? 我也研究了异步Rest调用,但是它不完全符合我的要求.请指教.在此先感谢

Is this good approach using new Thread to call second rest API and return result without waiting for response from second rest API ? I have also looked into Asynchronous Rest call, but it doesn't exactly suit my requirement. Please advice. Thanks in Advance

推荐答案

避免启动 Thread 直接.考虑使用 ExecutorService 如下图所示:

Avoid starting Threads directly. Consider an ExecutorService instead as shown below:

@Singleton
@Path("foo")
public class FooResource {

    private ExecutorService executor;

    @PostConstruct
    public void onCreate() {

        // Creates a thread pool that reuses a fixed number 
        // of threads operating off a shared unbounded queue
        this.executor = Executors.newFixedThreadPool​(10);
    }

    @GET
    public Response getFoo() {

        String result = doSomeProcessing();

        // Submits a Runnable task for execution
        executor.submit(new LongRunningTask());

        return Response.ok(result).build();
    }

    @PreDestroy
    public void onDestroy() {

        // Initiates an orderly shutdown in which previously submitted 
        // tasks are executed, but no new tasks will be accepted.
        this.executor.shutdownNow();
    }
}

public class LongRunningTask implements Runnable {

    @Override
    public void run() {

        try {
            // Simulate a long running task
            // Don't do it in a real application
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

浏览 Executors 有关如何创建的详细信息的API ExecutorService 实例.

Explore the Executors API for details on how to create an ExecutorService instance.

在Java SE和Servlet容器中,可以使用 ExecutorService 用于长时间运行的任务.在Java EE容器中,您应该使用 代替:

In Java SE and in a Servlet container, you can use an ExecutorService for your long running task. In a Java EE container, you should use a ManagedExecutorService instead:

@Resource
ManagedExecutorService executor;

一旦它是由容器管理的资源,则无需手动实例化和处置它.

Once it's a resource managed by the container, you don't need to instantiate and dispose it manually.

这篇关于Java Rest API在不等待响应的情况下调用另一个Rest-在JAX-RS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆