Abhinav Walia
Abhinav Walia

Reputation: 21

Can i use ExecutorService in an ejb?

I have a scenario in which the results of various students are generated from within one ejb call by looping over the student list. I was thinking of creating threads for processing each student using executorService within a ejb call. Currently i just look up my ejb once.

Upvotes: 2

Views: 1947

Answers (2)

Brett Kail
Brett Kail

Reputation: 33936

In EE 7+ servers, you should just use JSR 236, which let's your application have access to executors/pools that are managed by the application server.

Otherwise, in theory, the EJB spec does not allow EJBs to create their own ExecutorService, which would create/manage its own threads:

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 enterprise 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.

In practice, it might work if you have complete control over the server running your application (you know which other applications are running and how many threads/pools they're creating in order to avoid overloading the system), and you limit the actions taken in those threads (for example, java:comp lookups won't work, transactional behavior might be limited, etc.).

Upvotes: 1

André R.
André R.

Reputation: 1647

i think this post should answer your question

EJB's and Threading

in general an EJB should not spawn new threads or do 'handcrafted' asynchronous execution.

Upvotes: 2

Related Questions