Web application running in background

7

I'm working on a Java web application, and I'm creating and planning some specs. In some specific modules I need some executions running in the background on the server.

For example, the user sends a file to the server with a request for processing the same. 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 do the application by running " background " on the server. And if you have any nice references.

    
asked by anonymous 15.08.2014 / 21:31

2 answers

7

Your question is made up of two parts.

Processing in background

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

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

When the thread running in background terminates its workload the application can be notified. This status can later be obtained, and a completion message displayed to the user.

There is a recommendation to use the resources provided by the container (WebLogic, Websphere, Tomcat, etc.), including threads. To do this, use what is generally referenced by Container managed threads . The implementation will depend, in this case, on the container you choose.

Asynchronous Notification

Asynchronous notification already occurs differently. Your application should ask from time to time if there are any notifications to be displayed ( pooling ) or implement some server-side notification technology ( server push ), such as WebSockets or similar.

References:

JEE6 tutorial: invoking asynchronous methods
JBoss Developer: How to use Container managed threads
Why is initializing threads in Java EE containers not recommended? Working with threads in a Java web application
Oracle: Java API for WebSocket

    
15.08.2014 / 21:53
6

Dude, you would have to use two different technologies to get this to work as you described it.

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

1st - Use a scheduler. A "scheduler" is a service that is fired at regular intervals inside a java server and ee. You can program it to thirty to thirty seconds to perform a certain task. An example of this would be a scheduler that every thirty seconds searches the webserver for a certain stock quote or the exchange for a particular currency (if you were to use it to make stock purchases, obviously the time interval to get the quote should be much smaller). Using this strategy you should queue the action that must be performed so that the scheduler goes in that queue and seeing that there is something to do, trigger the processing. Take a look at this reference for more information on using scheduler: link I do not recommend this strategy.

2º - Use a servlet with asynchronous processing. It is this strategy that I recommend to you. In this case you will fire the action but the servlet will not return anything as a response to your page, and you also can not guarantee how long it will take for the task to be terminated. Within the action of the servlet you should queue some type of message, so that it is triggered to the user afterwards, indicating the end of the processing. Take a look at link for further clarification.

On 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 primefaces.

    
15.08.2014 / 22:01