简体   繁体   English

在servlet中运行多线程

[英]Run multi threads in servlets

I have couple of processes (calling to a 3rd party service and get response, query database and get results, do some IO operations etc) to be executed in my servlets. 我有几个进程(调用第三方服务并获得响应,查询数据库并获取结果,执行一些IO操作等)以在我的servlet中执行。 Moreover, those processes are time consuming and if the 3rd party call which is the first process, response fails, the rest of processes should be terminated too. 而且,这些过程是耗时的,如果第三方调用是第一个过程,响应失败,其余的过程也应该终止。

I am to run all these processes as multi threads using Executors. 我将使用Executors将所有这些进程作为多线程运行。 As soon as possible, the response of first process is available, the servlet should send response ( redirect to a page ) . 尽快,第一个进程的响应可用,servlet应发送响应(重定向到页面)。 When the rest of all process are finished, the output should be sent to the page again (something like push to the browser), or redirect to another page, ( but then want to have the access to the already running rest of threads. 当剩下的所有进程都完成后,输出应该再次发送到页面(类似于推送到浏览器),或者重定向到另一个页面,(但是然后想要访问已经运行的其余线程)。

My first attempt was to send ajax requests from the browser and handle all these processes. 我的第一次尝试是从浏览器发送ajax请求并处理所有这些进程。 But it is less secure. 但它不太安全。 So, let me know an approach to run these threads and once first process is done, send the response and later return the rest of outputs. 所以,让我知道运行这些线程的方法,一旦完成第一个进程,发送响应然后返回其余的输出。

My recommendation in this situation is to make the entire page asynchronous. 我在这种情况下的建议是使整个页面异步。 Send request, kick off threads and return immediately. 发送请求,启动线程并立即返回。 The threads can populate a common object contained in session, write to files or a database. 线程可以填充会话中包含的公共对象,写入文件或数据库。 Once complete, set a flag to show any subsequent requests from the client (polling requests, etc) that you're finished and the page can be built. 完成后,设置一个标志以显示来自客户端的任何后续请求(轮询请求等),您已完成并可以构建页面。

You can present the interim page in various ways ("Check back" message, polling with progress bar, et. al.). 您可以通过各种方式显示临时页面(“返回”消息,使用进度条轮询,等等)。

There are two options after knowing which IE version we have to support 在知道我们必须支持哪个IE版本后,有两个选项

  1. Use an AJAX based implementation. 使用基于AJAX的实现。

Using AJAX and HTTP POST, trigger your services one by one. 使用AJAX和HTTP POST,逐个触发您的服务。 Also make sure that promises are maintained in order to keep a clean state of requests. 还要确保维护promises以保持请求的干净状态。 You can also use ajax chaining, if you have to follow order of completion as well. 如果您还必须遵循完成顺序,也可以使用ajax链接。 Please note AJAX as such has no security limitations (see here ), it is just an API which uses HTTP methods to call server, in an asynchronous way. 请注意,AJAX没有安全限制(参见此处 ),它只是一个使用HTTP方法以异步方式调用服务器的API。 So if your server has security, AJAX can/need inherit that security. 因此,如果您的服务器具有安全性,则AJAX可以/需要继承该安全性。

  1. Use a websockets based implementation 使用基于websockets的实现

Since IE8 does not support websockets natively, you can use a custom api such as this . 由于IE8不支持WebSockets的本身,你可以使用自定义的API,如 Please note this will have some compatibility issues going forward, but for now there are no visible impacts. 请注意,这将有一些兼容性问题,但目前没有明显的影响。 Call the servlet which will trigger different threads, and send back the data after completion of threads. 调用将触发不同线程的servlet,并在完成线程后发回数据。 If your server has websocket-api.jar in its server lib, then you can code the server side of it very easily. 如果您的服务器在其服务器库中有websocket-api.jar,那么您可以非常轻松地对服务器端进行编码。 See here 看到这里

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

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