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

10,779

Solution 1

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.

Solution 2

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/

Solution 3

Quartz API can be used for your task.

Check this example Integrating Quartz in a J2EE application

Solution 4

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.

Share:
10,779
gigadot
Author by

gigadot

This is blank.

Updated on June 04, 2022

Comments

  • gigadot
    gigadot almost 2 years

    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.