Web application running in background

Asked

Viewed 822 times

7

I am working on a Java web application, and I am creating and planning some specifications. In some specific modules I need some runs running in the background on the server.

For example, the user sends a file to the server with a request for processing it. Depending on the process this part can be time consuming, and when the request is completed a notification is displayed (like on facebook, when we receive a message, for example).

I want to know what would be the best way to make the application running on "background" on the server. And if you have any nice references.

  • 2

    I don’t think this is too broad. See the answers. @Kyllopardiun et al., do not want to reconsider?

  • 2

    I agree with @bfavaretto, so much so that the current answers effectively answer the question and go the same way I would answer, which (for me at least) shows that it is not so wide.

  • I agree, and I really believe that the question is not that wide, forgive me if I did not know how to express myself correctly. And really both answers were more than efficient to help me. Thank you.

2 answers

7


Your question is composed of two parts.

Processing in background

This is one of the cases where threading can be an excellent tool for firing asynchronous methods.

The idea is to start a thread during the request of the user, and let it run in parallel, even after the response has already been returned to the user, and the thread original where the request was processed has already been completed.

When the thread running in background Finish your workload the application can be notified. This 'status' can be later obtained, and a checkout message displayed to the user.

There is a recommendation to use the resources provided by the container (Weblogic, Websphere, Tomcat, etc.), including threads. For this, use what is usually referenced by Container Managed threads. The implementation will depend, in this case, on the container chosen by you.

Asynchronous notification

Asynchronous notification occurs differently. Your application should ask from time to time if there is any notification to display (pooling) or implement some server-side notification technology (push server), as Websockets or similar.

References (in English):

JEE6 tutorial: invoking asynchronous methods
Jboss Developer: How to use Container Managed threads
Why Booting Threads in Java EE Containers Is Not Recommended?
Working with threads in a Java web application
Oracle: Java API for Websocket

  • thanks for the answer, with all your explanation and reference I believe I will be able to plan and execute my task correctly. I was lost in the thread issue because I had never worked with it before, the asynchronous notification already had an idea of how to do it, but you helped me even more. Thank you.

6

Dude, you’d have to use two different technologies to make it work like you described.

For you to achieve asynchronous processing you can follow one of the following two approaches:

1º - Use a Scheduler. A "Scheduler" is a service that is fired at regular intervals within a java ee server. You can program it to every thirty seconds perform a certain task. An example of this would be a Scheduler that every thirty seconds searches on some webserver the quotation of a particular stock or the exchange for a particular currency (if you were to use this to make stock purchases obviously the time interval to fetch the quote should be much shorter). Using this strategy you should line up the action that must be executed, so that the Scheduler goes in this queue and seeing that there is something to be done, trigger the processing. Take a look at this reference for more information on using Scheduler: http://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html I do not recommend this strategy.

2º - Use a Servlet with asynchronous processing. This is the strategy I recommend. In this case you will trigger the action but Servlet will not return anything as a response to your page, and you also cannot guarantee how long it will take for the task to be completed. Within the action of Servlet you must queue some kind of message, so that it is fired to the user later, indicating the end of the processing. Take a look at http://docs.oracle.com/javaee/7/tutorial/doc/servlets012.htm for further clarification.

About strategies to inform the user that the processing is over I would recommend that you use something like "push" technology, which allows the server, on its own, to trigger some message to a web client. I recommend you take a look at the documentation of the first faces.

  • thanks for the help, that you have indicated to me will be very helpful. Your answer is great. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.