0

Is there a way to run multiple threads inside a single EJB execution?

What I'm trying to do is essentially an ETL process: the onMessage method of an MDB (@MessageDriven) will run a query, then kick off multiple threads to insert rows into some target table (all native JDBC, not JPA.)

It could just as easily be a @Stateless session bean as well.

The question is - Can you use Executors.newFixedThreadPool inside an EJB? Even if it's not strictly legal according to spec, can you get away with it if the background threads are not accessing any Java EE / container-managed resources?

If not, is there a better way to do this? I know about @Async but would lose control over the number of allowed threads per job.

wrschneider
  • 17,913
  • 16
  • 96
  • 176

1 Answers1

0

In practice, yes, you can use Executors.newFixedThreadPool inside an EJB. The EJB spec puts limits on what an EJB can do because it can cause problems for either Java EE services (non-issue for your situation) and for the resources consumed by the server itself. You'll need to be careful with the latter by somehow ensuring that if you get a "storm" of MDB activity that you don't end up creating more threads than the JVM can handle.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90