我不明白异步支持的Servlet 3.0 API [英] I don't understand Async support in servlets 3.0 API

查看:124
本文介绍了我不明白异步支持的Servlet 3.0 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个Java SE背景,而我做了一些小服务程序的教程和读取头第一个JSP和放大器; servlet的。我读的 JavaWorld.com 的文章,但现在我不太明白。

I come from a Java SE background, and I did some servlets tutorials and read Head First JSP & servlet. I'm reading the JavaWorld.com article about Async support now but I don't quite understand it.

什么是异步简直是什么? 阿贾克斯之间的区别是什么Servlet的异步?

What's Async is simply about ? What is the difference between Ajax and Servlet Async?

PS我有一个PHP背景,阿贾克斯,我知道这个概念,但我还没有尝试过用java

P.S I have a PHP background with ajax and I know the concept, but I haven't tried it with java

推荐答案

在传统的Servlet模型,它通常是1请求对应于1个线程的情况下。

In the traditional Servlet model, it's normally the case that 1 request corresponds to 1 thread.

这些线程通常来自于它是由Servlet容器管理的游泳池。因为它已经在这个池中的可用线程的Servlet容器只能作为长期处理新的请求。只要你自己的code是忙于处理请求,线程是不是免费的。

These threads typically come from a pool which is managed by the Servlet container. The Servlet container can only handle new requests as long as it has free threads in this pool. As long as your own code is busy processing the request, the thread is not free.

在某些情况下,它可能是值得的,打破这种模式。什么情况是,一个请求到达时,透过这样的Servlet容器托管线程Servlet的,和你的code,则要求异步执行。然后,您可以从servlet请求返回和容器线程将被释放。

In some situations it might be worth it to break this model. What happens is that a request arrives at the Servlet via such a Servlet container managed thread, and your code then asks for asynchronous execution. You can then return from the Servlet request and the container thread will be freed.

相反,同步请求处理,这样就不会犯任何回应,也不会关闭连接。相反,你可以用手异步上下文切换到另一个线程池,它可以把它捡起来,当一些线程可以自由地处理它,服务它,就能写入响应。

Contrary to synchronous request processing, this will not commit any response and will not close the connection. Instead, you can hand the async context off to another thread pool, which can pick it up, and when some thread is free to handle it, service it and will be able to write to the response.

一个例子:

@WebServlet(urlPatterns = "/somepath", asyncSupported = true)
public class AsyncServlet extends HttpServlet {

    @EJB
    private AsyncBean asyncBean;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AsyncContext asyncContext = request.startAsync();

        // The following line will not block and return immediately
        asyncBean.doAsyncStuff(asyncContext);

    } // Shortly after this method has ended, thread will be returned to pool
}

随着 AsyncBean 被实现为:

@Stateless
public class AsyncBean {

    @Asynchronous
    public void doAsyncStuff(AsyncContext asyncContext) throws IOException {
        asyncContext.getResponse().getWriter().write("test");
    }
}

在code以上,你从 AsyncServlet#的doGet()方法返回后不久的某个地方,Servlet的线程将被返回到池中。用于执行请求(任务) AsyncBean#doAsyncStuff()将被放置在队列中的EJB线程池回暖。

In the code above, somewhere shortly after you return from the AsyncServlet#doGet() method, the Servlet thread will be returned to the pool. A 'request' (task) for executing AsyncBean#doAsyncStuff() will have been put in the queue for the EJB thread pool to pick up.

这个问题的答案为何以及何时你会使用这不是那么简单。如果你只是想preserve线程则在上述情况下,你会被交换来自一个线程池中的线程为其他(在这种情况下,Servlet的游泳池相较于EJB异步池)和净效益就不会那么多。你也可以同样给你的servlet线程池中的一个额外的线程。

The answer to WHY and WHEN you would use this is not that straightforward. If you just want to preserve threads then in the above case you would be exchanging one a thread from one thread pool for the other (in this case the Servlet pool vs the EJB async pool) and the net benefit wouldn't be that much. You could just as well have given your Servlet thread pool an extra thread.

在更高级的场景中,你可以做但要求更精细的管理;把它们分成多个任务,并有一个线程池服务的那些任务。例如。想象一下100的下载请求,由10个线程的循环处理,给一个10MB的文件发送100KB一时间每个请求。

In more advanced scenarios, you could however do more fine-grained managing of requests; divide them into multiple tasks and have a thread pool service those tasks. E.g. imagine 100 download requests for a 10MB file handled by 10 threads that round-robin give sends 100KB a time to each request.

又一应用是需要等待数据从外部系统的请求,并且其中该外部系统能够发送可以被中继回请求者的消息。即一个数据库调用就没有意义在这里,因为你需要另一个线程等待响应反正。然后,你会再次改变一个线程另一个。但是,如果你需要等待比方说进入的电子邮件,然后一个线程可以等待任何电子邮件和中继的任何挂起的请求。

Yet another application is a request that needs to wait for data from an external system, and where this external system is capable of sending a message that can be relayed back to the requestor. I.e. a database call would not make sense here, since you would need another thread waiting for the response anyway. Then you would change one thread for another again. But if you need to wait for say an incoming email, then one thread can wait for any email and relay that to any suspended request.

这篇关于我不明白异步支持的Servlet 3.0 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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