Thomas Dias
Thomas Dias

Reputation: 1

Java EE and background threads

I am trying to determine the "approved" or best practices approach while in a Java EE environment for doing the following: A client is on a web page and clicks a button. This starts a thread for monitoring a process, i.e. database activity, network, etc. This process will continue to run until the user clicks a button that tells the process to end. Other clients, and/or the same client then clicks a button to listen to the status being sent from that process which will continue to listen until the user clicks a button to stop listening.

I have already done the above by using a WebSocket to communicate with a servlet which gets an injected a singleton EJB that extends WebSocketApplication. But, this EJB is spawning the process thread to do the monitoring. Although it works and should continue to work since it is a singleton, it is not the "approved" way of doing it.

Some suggestions I have reviewed discuss using JMS to spawn the thread, but, unless I am misunderstanding something, this doesn't solve anything since a Message Driven Bean isn't supposed to spawn a thread either. So, what is the approved/best practices method of doing this? How does one start and stop a background process in a Java EE environment? Again, the requirements are, only one process can be spawned, it must communicate to all WebSockets that register with the servlet, it must be able to die when told to (although that doesn't mean the server closes the sockets, since it could be started back up and would still communicate to all the previously registered clients).

Thanks.

Upvotes: 0

Views: 1611

Answers (4)

mancini0
mancini0

Reputation: 4703

As of Java EE 7 you can use the ManagedExecutorService to give your EJBs access to a managed thread pool.

Upvotes: 0

Chris Ritchie
Chris Ritchie

Reputation: 4826

To create threads in an enterprise environment, you should use either ManagedThreadFactory or ManagedExecutorService.

Please see here

Upvotes: 1

Thomas Dias
Thomas Dias

Reputation: 1

The answer is an asynchronous EJB 3.1 bean. Thanks for the reply.

Upvotes: 0

Raedwald
Raedwald

Reputation: 48626

this EJB is spawning the process thread

EJBs are not permitted to start threads. Quoth the standard:

The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread’s priority or name. The enter- prise bean must not attempt to manage thread groups.

These functions are reserved for the EJB container. Allowing the enterprise bean to manage threads would decrease the container’s ability to properly manage the runtime environment.

This is by design; the specification states the following

The Enterprise JavaBeans architecture will make it easy to write applications: application developers will not have to understand low-level transaction and state management details, multi-threading, connection pooling, or other complex low-level APIs.

Have you considered using a stateful session bean? Clicking on the button causes the bean to enter the "started" state. Click on the second button causes the bean to enter the "stopped" state.

Upvotes: 1

Related Questions