• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1102
  • Last Modified:

Start Thread in EJB

Hi,

Is it allowed to start threads within an EJB (for example in an Sheduler-MDB in JBoss) and if so, can starting threads within EJB cause problems like memory leaks?
I was told once (a long while ago) not to start threads within an EJB.

Alexander.
0
aperdon
Asked:
aperdon
  • 2
1 Solution
 
eelguetaCommented:
- You can start threads.
- If u don't eventually kill them, they'll eat up your memory.
- Keep track of them, use them in a Session bean and kill them at Session expiration.
0
 
gordonssCommented:
Hmm...well according to the spec, you can't. From the EJB 1.1 spec:

"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."
(Section 18.1.2)

EJB 2.0 spec is similar:
"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."
(section 24.1.2)

But it's true that many app servers _do_ let you at least start new threads. Check your environment...
  Si.
0
 
aperdonAuthor Commented:
Indeed JBoss lets u start threads inside an EJB. I remember I was not able in WebSphere 3.5x to start threads from within an EJB.

The problem I encouter now in Jboss 3.03 and 3.06 is when I start threads from a scheduler-MDB. At the moment of scheduling I want to split some work over threads. I think there is a problem with JBoss in case a new scheduler-MDB starts when the last one was not completely finished yet.
0
 
gordonssCommented:
Think you've hit the nail on the head.

Since the MDB is stateless, when a new MDB is created JBoss is (probably) reusing the same MDB instance for the incoming message: your MDB's Thread member is getting (re)set and the old member is becoming <null> and getting garbage collected at some point.

This is the kind of thing that the specs try to avoid happening. Because you've gone outside what is allowed, the behaviour is at best 'undefined'.

Can't think of a better alternative without knowing more of what you're trying to achieve. But since the MDB can be swapped in/out of memory at the whim of the app server EJB container, it would be great to avoid letting the MDB 'own' these threads you create.

Post a message using RMI to a remote object?
Post a new JMS message for each bit of 'work' that needs its own thread?
?

 Si.


0

Featured Post

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now