How to monitor asynchronous/background thread status and get notifications in a JSF component

Solution 1:

I created few threads in a web application

I wholeheartedly hope that you did it the right way and thus it don't end up in an epic disaster.


I would like to update the table automatically to show the current status of thread. Is there any prebuild component I can use in this scenario?

You're basically looking for a technique called "polling" or "pushing". The basic concept of polling is to use JavaScript's setInterval() or setTimeout() to fire a (ajax) HTTP request to the server at intervals and let it process the results accordingly. The basic concept of pushing is to open a persistent HTTP connection and have the server send small messages over it, this can be done with among others WebSocket in JavaScript and JSR356 API in Java EE 7.

The standard JSF implementation doesn't offer any ready-to-use poll or push component out the box. For the upcoming JSF 2.3, a new <f:websocket> tag is currently in progress (see also issue 1396).

Your best bet would be a hidden form with a hidden command link/button with <f:ajax> and then invoke it by setInterval(). Component libraries like PrimeFaces however offer ready-to-use components out the box, see also <p:poll> showcase and <p:push> showcase for some concrete examples. JSF utility library OmniFaces has in its <o:commandScript> showcase a poll example and in <o:socket> showcase several push examples (the new standard JSF <f:websocket> will be largely based on <o:socket>).

See also:

  • How do I ajax-update a component at an interval of 1 second?
  • Real time updates from database using JSF/Java EE
  • How can server push asynchronous changes to a HTML page created by JSF?