Asynchronous Tasks in Java Web Application

Asynchronous tasks, such as downloading results from a website, or sending emails after completing some important tasks. When we load some evaluations, we have to wait on the current page to get the response page or download the file. Is there a chance that I can click on the number of downloads and this happens in the background so that I can navigate to other parts of the website and check the status of the job on average. Or schedule some task later in the future and receive the results of its execution by email.

We are struts 2 web applications with Hibernate 3.5 ORM. After looking at some of the java scheduling libraries, you got information about Quartz. But is Quartz the right library for the above requirements or any other library I can try? Please point me in the right direction.

+1


source to share


2 answers


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

  • - this library is very comprehensive and allows you to plan all kinds of work. If you only want to use it to schedule jobs in the background and run them immediately, it might be overkill

  • use thread pool, see Executors

    class

  • the queue can listen for requests and process them asynchronously in s

  • Finally, you can use the support @Async

    / @Asynchronous

    in or



Then you restore the results somehow. Explanation of whether you want to deliver them directly in the browser or via email:

  • every time you show the page, check to see if any completed / in-progress jobs are running. If there are some completed tasks, show an additional link on the page somewhere (kind of notification). If the job is running, run query and query every second or use long poll /to get the result immediately.

  • if you want to send the results by email, just send it after the work is completed. Much simpler, but less friendly IMHO.

+7


source


Quartz is definitely one way to do this - and works well if you want to schedule a job to run at a specific time or frequency.

If you just want to drop something in the background in response to a user action and check its status, there are several other ways to do this that might be better suited for this pattern:



  • package java.util.concurrent: you can set up ThreadPoolExecutor and send it jobs that implement Callable

    . You return an object Future<T>

    that you can check for completion ( isDone

    ) and get its result on completion ( get

    ).

  • with EJB or Spring, there is also the concept of a (session) bean being @Async

    or @Asynchronous

    , which also return Future<T>

    and behave like above. Basically it just abstracts the creation and management of the thread pool from your code and moves it into a container or framework.

+1


source







All Articles