简体   繁体   English

来自Java servlet的异步任务

[英]Asynchronous task from Java servlet

I need to perform an asynchronous task when a RESTful web service endpoint is called. 我需要在调用RESTful Web服务端点时执行异步任务。 Effectively, the endpoint is asked to perform a body of work with a POST operation. 实际上,要求端点通过POST操作执行一系列工作。 It should immediately return a 200 OK to the caller, spawn a thread and perform it's resource intensive task. 它应该立即向调用者返回200 OK,生成一个线程并执行它的资源密集型任务。 On completion, the thread would then POST to a corresponding endpoint on the caller (another REST server) indicating success (passing a token that represents the initial transaction request). 完成后,线程将POST到调用者(另一个REST服务器)上的相应端点,指示成功(传递表示初始事务请求的令牌)。

What are the best practice approaches for performing asynchronous actions inside a servlet that I should be aware of? 在我应该注意的servlet中执行异步操作的最佳实践方法是什么?

Servlet 3.0 has support for asynchronous operations . Servlet 3.0支持异步操作 Tomcat 7.0 is already stable, so you can get it and try the new features. Tomcat 7.0已经稳定,因此您可以获得它并尝试新功能。

If you don't need to output data continously, but to simply start a background process, then you can use any asynchronous mechanism available: 如果您不需要连续输出数据,而只需启动后台进程,那么您可以使用任何可用的异步机制:

Aside from the complexities of async coding in Java, another "best practice" in RESTful web services is to use HTTP status codes to describe your server's response as accurately as possible. 除了Java中异步编码的复杂性之外,RESTful Web服务中的另一个“最佳实践”是使用HTTP状态代码尽可能准确地描述服务器的响应。 Unless you have a compelling reason to stick with 200 (ie a client which you can't change expects this), you should return HTTP 202 : 除非你有令人信服的理由坚持200(即你无法改变的客户端期望这个),否则你应该返回HTTP 202

202 Accepted 202接受

The request has been accepted for processing, but the processing has not been completed. 该请求已被接受处理,但处理尚未完成。

The only advice for your scenario would be to use thread pool rather than creating a new thread per request. 您的方案的唯一建议是使用线程池而不是每个请求创建一个新线程。 In Java it is very easy, just create pool once during application startup (look at Executors class) and submit new tasks to it each time you need to perform some asynchronous operation. 在Java中,它非常简单,只需在应用程序启动期间创建一次池(查看Executors类),并在每次需要执行某些异步操作时向其提交新任务。 For your scenario this tasks will perform resource intensive operations and second REST call from within a different thread, long after the original request was served with 200. 对于您的场景,此任务将在原始请求与200一起提供服务之后很长时间内执行资源密集型操作和第二次REST调用。

In a Java EE container, the recommended way is to use the WorkManager API (JSR-237). 在Java EE容器中,推荐的方法是使用WorkManager API(JSR-237)。 Check this article for an overview. 查看此文章以获取概述。

A J2EE container can serve multiple users at the same time (in parallel) by managing a pool of threads, but for various reasons opening independent threads within a single J2EE container is not recommended. J2EE容器可以通过管理线程池同时(并行)为多个用户提供服务,但由于各种原因,不建议在单个J2EE容器中打开独立的线程。 Some containers' security managers will not allow user programs to open threads. 某些容器的安全管理器不允许用户程序打开线程。 Moreover, if some containers allowed opening threads, then they would not manage those threads and therefore no container-managed services would be available for the threads. 此外,如果某些容器允许打开线程,那么它们就不会管理这些线程,因此线程不会有容器管理的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM