java – web application running in the background

Question:

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 its processing. Depending on the process, this part can take a long time, 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 in the background on the server. And if you have a nice reference.

Answer:

Your question is made up of two parts.

Background Processing

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 user's request , and let it run in parallel, even after the response has already been returned to the user, and the original thread where the request was processed has already ended.

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

There is a recommendation to use the resources made available by the container (WebLogic, Websphere, Tomcat, etc.), including threads. To do this, use what is commonly referred to as Container managed threads . The implementation will depend, in this case, on the container chosen by you.

asynchronous notification

Asynchronous notification is done differently. Your application should ask you 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 (in English):

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

Scroll to Top