开发者

Create a worker thread and run from a servlet or Spring controller

I would like to execute some task after a user request in background. My initial idea is to create a work thread and execute it from a servlet. However, I do not want too many threads to be running at the same time so I will need something like a thread pool.

Since I am already using Spring with my web开发者_运维百科 application, I am wondering if there is anything in Spring or other libraries I can use to handle this problem without having to implement my own codes.


Use @Async to spawn asynchronous threads; Spring can manage those, like every other service it provides. Example blog post: http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/


creating your own threads is often not advisable within a container as this can screw up thread pooling, i.e. the container unexpectantly runs out of threads. There is good quartz support in spring which may do exactly what you need, e.g. you can configure that the background job runs synchronously or asynchronously.


Quartz API can be used for your task.

Check this example Integrating Quartz in a J2EE application


Manually creating threads doesn't allways work well in a Java EE environment (and sometimes is prohibited by the container). Creating a background task in Java EE is often better done using a servlet that responds to messages (JMS). Then you can send a message to let the servlet do some work.


Please look at this post and Bozho answer

Open new thread in Tomcat

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜