本文介绍了什么是异步JAX-RS的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读REST风格的Java与JAX-RS 2.0一书。我与异步JAX-RS完全糊涂了,于是我问一个所有的问题。书中写道:异步服务器是这样的:

  @Path(/用户)
公共类CustomerResource {    @得到
    @Path((编号))
    @Produces(MediaType.APPLICATION_XML)
    公共无效getCustomer(@Suspended最终AsyncResponse asyncResponse,
                            @Context最后请求的要求,
                            @PathParam(值=ID)最终诠释的id){        新的Thread(){
            @覆盖
            公共无效的run(){
                asyncResponse.resume(Response.ok(新用户(ID))建立());
            }
        }。开始();
    }
}

Netbeans的创建异步服务器是这样的:

  @Path(/用户)
公共类CustomerResource {
    私人最终ExecutorService的ExecutorService的= java.util.concurrent.Executors.newCachedThreadPool();    @得到
    @Path((编号))
    @Produces(MediaType.APPLICATION_XML)
    公共无效getCustomer(@Suspended最终AsyncResponse asyncResponse,
                            @Context最后请求的要求,
                            @PathParam(值=ID)最终诠释的id){        executorService.submit(新的Runnable(){
            @覆盖
            公共无效的run(){
                doGetCustomer(ID);
                asyncResponse.resume(javax.ws.rs.core.Response.ok()建立());
            }
        });
    }    私人无效doGetCustomer(@PathParam(值=ID)最终诠释的id){
    }
}

那些不创建后台线程使用一些锁定方法来存储响应对象以便进一步处理。这个例子是用于发送股票报价给客户:

  @Path(qoute / RHT)
公共类RHTQuoteResource {    保护列表与LT; AsyncResponse>响应;    @得到
    @Produces(text / plain的)
    公共无效的getQuote(@Suspended AsyncResponse响应){
        同步(响应){
            responses.add(响应);
        }
    }
}

响应对象将一些后台作业共享,它会发送报价给所有客户端时,它已准备就绪。

我的问题:


  1. 在例1和2的W​​eb服务器线程(即处理请求的)死亡
    我们创建另一个后台线程。背后的整个思路
    异步服务器是减少空闲线程。这些实施例
    不降低空闲线程。一个线程死了,另一个出生。

  2. 我想过创造容器内非托管线程是一个坏主意。
    我们应该只使用管理线程并发使用公用事业
    的Java EE 7。

  3. 的想法背后异步服务器再一个是规模。例3不结垢,对吧?


解决方案

摘要:你过想这


Neither is particularly great, to be honest. In a production service, you wouldn't hold the executor in a private field like that but instead would have it as a separately configured object (e.g., its own Spring bean). On the other hand, such a sophisticated example would be rather harder for you to understand without a lot more context; applications that consist of systems of beans/managed resources have to be built to be that way from the ground up. It's also not very important for small-scale work to be very careful about this, and that's a lot of web applications.

The gripping hand is that the recovery from server restart is actually not something to worry about too much in the first place. If the server restarts you'll probably lose all the connections anyway, and if those AsyncResponse objects aren't Serializable in some way (no guarantee that they are or aren't), you can't store them in a database to enable recovery. Best to not worry about it too much as there's not much you can do! (Clients are also going to time out after a while if they don't get any response back; you can't hold them indefinitely.)

It's an example! Supply the executor from outside however you want for your fancy production system.

It's just enqueueing an object on a list, which isn't a very slow operation at all, especially when compared with the cost of all the networking and deserializing/serializing going on. What it doesn't show is the other parts of the application which take things off that list, perform the processing, and yield the result back; they could be poorly implemented and cause problems, or they could be done carefully and the system work well.

If you can do it better in your code, by all means do so. (Just be aware that you can't store the work items in the database, or at least you can't know for sure that you can do that, even if it happens to be actually possible. I doubt it though; there's likely information about the TCP network connection in there, and that's never easy to store and restore fully.)

这篇关于什么是异步JAX-RS的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:39