Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi-threading in EJB helper classes?  RSS feed

 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all.

From my testing, it appears that it is technically possible to spawn a thread from within an EJB (or, say, in an EJB-helper POJO class), I thought I had read that spawning threads in your J2EE app was a tech-design no-no.

Is it a no-no?

Ben Ethridge
SCJB 1.4
 
Siplin Ayishoto
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the idea behind EJBs is to free the programmer from such dirty
work. If you feel the urge to multithread a helper, maybe the problem lies
elsewhere. Maybe architecture of the system itself might need a major
rehaul...

-Siplin
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

I'm curious into what you might want to do with the thread there?
Are you initiating the thread from a Session Bean or an Entity Bean?
Even though it's technically possible to do so, but why would you do so?
Could you give me a sample case where initiating a thread from a Session Bean or an Entity Bean may just solve a problem?

Just out of my curiosity..

- eric
blog
 
Bruno Collet
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EJB spec says that thread should never be managed programatically in the EJB container, but it does not say what happens if you do it I think that weird things can occur, which are not obvious in ordinary testing. For example, what happens to your threads when the EJB is passivated? This may not happen when you run your 30 seconds test but will happen in production environment.
As for why should one want to use threads in EJB (comment to Eric), I met the same situation. I wanted to read data (a business feature...) from numerous URLs from the EJB container, which is not forbidden. But every URL connection can take from a few seconds to eternity (URL not reachable) to be done. So it was a good idea to start a separate thread for each URL and to implement a timeout mechanism (TimerTask) for each thread. Ultimately I moved this feature to POJO that are instantiated from a web application, but I think that the feature logically belongs to business components.
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all.

This is not something I'm designing, but rather something I've seen in a design, so I'm not exactly sure why the designers chose to use multi-threading in this case. It appears that the thread polls the database, looking for new jobs to do. This is from a Session EJB.

I could see reasons why one might want to use multi-threading in such situations, just to free the caller to move on to other work, while the spawned thread does some other "lower-priority" work asynchronously...

...but then I would think one could solve the problem with JMS, without resorting to using such "application-level" threads.

I was just curious what the Sun J2EE spec or some of the J2EE authors have to say on this, since, J2EE is apparently not disallowing it, at least not in IBM WebSphere Studio.

Ben
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I read once that some version of WebSphere was going to start throwing exceptions if you made your own threads, but that could be wrong.

Have you done MDBs? You can use JMS async messaging to start another long running process in an MDB.

Does the request come through a servlet container? You can make threads there. You might start a new thread that does a separate EJB call that can run forever.
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually expected it to throw an exception (or a pre-compiler error). When it didn't, it kind of surprised me, that threading was allowed . That's when I started wondering what Sun has said in the J2EE spec on the subject. Still curious about that. Anyone know?

Regarding MDB's: I know. Again, this is not my own design.

Ben
 
Bruno Collet
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben, if the job to be done is related to the database, I would have done it in a resource adapter (Java Connector Architecure (JCA) component). It is, among other things, intended for this situation, and it allows multithreading. A resource adapter can be called fron an EJB (for instance, a datasource is a specialization of a resource adapter).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!