2

On most of applications servers, J2EE Ejb specification forbids creating threads "by hand", since these resources should be managed by the server.

But is there any way to get threads from Tomcat, Glassfish, Jboss etc.; thus access their ThreadPool?

csviri
  • 1,159
  • 3
  • 16
  • 31
  • 1
    Great question! I've asked [this question](https://forums.oracle.com/forums/thread.jspa?threadID=2260116) in the OTN Forums before as well with no responses. As @Robin [answered](http://stackoverflow.com/questions/7321825/getting-thread-from-container#answer-7323431), CommonJ is one potential option (WAS also provides Work Managers, but this isn't as portable). In addition, I'd recommend watching [JSR-236](http://jcp.org/en/jsr/detail?id=236), which is supposed to be in the works for [Java EE 7](http://jcp.org/en/jsr/detail?id=342). – shelley Sep 06 '11 at 18:58
  • An alternative to using the thread directly may be to use an asynchronous EJB call. It's pretty new though, I think it was added in EJB 3.1. – craigforster Sep 09 '11 at 14:10

3 Answers3

3

You can use the commonj WorkManager. It was a proposal by IBM and BEA to provide a standard means to accomplish this task (access to container managed threads).

Although it was not included in the actual specification, there are implementations available for most containers.

Robin
  • 24,062
  • 5
  • 49
  • 58
1

The legal way to get threads from container is using JCA (Java Connector Architecture). The component you implement using this technology is called "resource adapter" and is packaged as rar file.

The implementation is pretty verbose but not too complicated in simple cases. So, good luck.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

I've seen at least one utility class for getting ahold of Tomcat's threadpool, but it's not wise to go this route. Those threads are created to service your EJB or Servlet's requests, not for you to support the EJB or Servlet. Each one you take up is just another thread that won't be available to service requests to the container.

You could probably just throw in a static ThreadPool and use a static initializer to get around the EJB spec on this one, but you'd obviously have to make sure the thread code works well otherwise it could really bork your EJB.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83