Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Tasks in Java Web app

Asynchronous jobs such as download scores from the website, or send emails after completion of some critical tasks. Rightnow we when we download some scores, we have to wait on the current page to get the response page or to get file downloaded. Is there a possibility that i can click on download scores and it happens in the background so that i can navigate to other parts of the website, and in the mean-time check the status of the job. Or Schedule some job later in the future and get its execution results via email.

Ours is a struts 2 webapplication with Hibernate 3.5 ORM. After browsing into some java scheduling libraries, got some info on Quartz. But is Quartz the right library for the above requirements or any other library that i can try for? Please guide me in the right direction.

like image 735
Sangram Anand Avatar asked Aug 29 '26 01:08

Sangram Anand


2 Answers

You will need some sort of asynchronous processing support. You can use:

  • quartz-scheduler - this library is very comprehensive and allows you to schedule all sorts of jobs. If you want to use it only for the purpose of scheduling jobs in the background and run them immediately, might be an overkill

  • use thread pool, see Executors class

  • jms queue can listen on requests and process them asynchronously in mdbs

  • Finally you can take advantage of @Async/@Asynchronous support in spring or ejb

Then you mut somehow restore the results. Depening on whether you want to deliver them directly in the browser or via e-mail:

  • every time you are rendering a page, check whether there aren't any completed/in progress jobs. If there are some completed jobs, display an extra link on the page somewhere (sort of notification). If the job is in progress, start an ajax request and ask every other second or use long-polling/comet to receive the result immediately

  • if you want to send results by e-mail, just send it after the job finishes. Much simpler but less user-friendly IMHO.

like image 106
Tomasz Nurkiewicz Avatar answered Aug 30 '26 15:08

Tomasz Nurkiewicz


Quartz is certainly one way to do that - and works well if you want to schedule a job to run at a particular time or with a particular frequency.

If you just want to kick something off in the background in response to a user action, and check its status, there are a few other ways to do it which may be better suited to this pattern:

  • the java.util.concurrent package: you can set up a ThreadPoolExecutor and submit tasks to it that implement Callable. You get back a Future<T> object that you can check for completion (isDone) and get its result when complete (get).

  • with EJB or Spring, there is also a concept of a (session) bean method being @Async or @Asynchronous, which return a Future<T> as well and behave as above. Basically this just abstracts away the thread-pool creation and management from your code, and moves it into the container or framework.

like image 20
wrschneider Avatar answered Aug 30 '26 15:08

wrschneider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!