?
Solved

Start Thread in EJB

Posted on 2003-03-27
4
Medium Priority
?
1,098 Views
Last Modified: 2007-12-19
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
Comment
Question by:aperdon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:eelgueta
ID: 8247895
- 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
 

Accepted Solution

by:
gordonss earned 500 total points
ID: 8284119
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
 
LVL 1

Author Comment

by:aperdon
ID: 8291044
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
 

Expert Comment

by:gordonss
ID: 8291352
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert websphere application server default chained Certificates from 1024 to 2048 keysize or higher size and also you can change signatureAlgorithm . Please make sure Websphere Application Server fixpack 7.0.0.23 or Above. The following steps a…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question