从 Java servlet 运行长进程的最佳方法是什么? [英] What is the best approach to run a long process from a java servlet?

查看:34
本文介绍了从 Java servlet 运行长进程的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下从 java servlet 运行长进程的最佳方法是什么.我有一个 webapp,当客户端发出请求时,它运行一个 servlet.这个 servlet 应该从请求中获取一些参数,然后运行一个进程.这个过程可能需要很长时间,所以我需要单独运行它.当这个过程执行完成时,它会发送一封包含结果的电子邮件.

I would like to ask what is the best approach to run a long process from a java servlet. I have a webapp and when the client do a request it runs a servlet. This servlet should get some parameters from the request and then runs a process. This process may take a long time so I need to run it separately. When this process executed finish, it send an email with the results.

提前致谢.

推荐答案

使用线程池.每次收到请求,创建一个任务并提交到线程池.这将确保过多的请求不会使服务器瘫痪,因为您可以控制可以拥有多少并发线程,以及可以在线程池的等待任务队列中等待多少任务.

Use a thread pool. Each time you receive a request, create a task and submit it to the thread pool. This will ensure too many requests don't bring the server to its knees, because you'e in control of how many concurrent threads you can have, and how many tasks can wait in the thread pool's queue of waiting tasks.

请参阅 Executors 的 javadocThreadPoolExecutor.

这篇关于从 Java servlet 运行长进程的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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