Java Champion / JavaOne Rockstar Adam Bien (adam-bien.com) is a self-employed consultant, lecturer, software architect, developer, and author in the enterprise Java sector. He is also the author of several books and articles on Java and Java EE technology, as well as distributed Java programming. adam has posted 59 posts at DZone. View Full User Profile

Legally Starting Threads/Synchronizing EJBs - Hell Or Heaven

03.16.2010
| 4394 views |
  • submit to reddit

EJB spec does not allow starting and managing threads ...in general. With @Singleton you can do whatever you want even:

With Bean Managed Concurrency demarcation, the container allows full concurrent access to the Singleton bean instance. It is the responsibility of the bean developer to guard its state as necessary against synchronization errors due to concurrent access. The bean developer is permitted to use the Java language level synchronization primitives such as synchronized and volatile for this purpose. [EJB 3.1 spec, page 111]

You only need to apply an additional annotation on a @Singleton and do whatever you want - at least synchronization-wise:

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class InefficientHelloWorld {
//AtomicInteger would be a lot better.
private static volatile int counter = 0;
//synchronized just for demo purposes, o.k with CMT.Bean.
public synchronized void sayHello(){
new Thread(new Runnable() { //still not allowed - but it works
public void run() {
//out.println is not a best practice either...
System.out.println("Hello World: " + counter++);

}
}).start();
}
}

In the majority of all cases it is better to use just @Stateless beans and just let the container manage threads and synchronization for you.
@Asynchronous is far better and easier to use , than the example above.
The "working" example was pushed into http://kenai.com/projects/javaee-patterns/. The name of the project is "BeanManagedConcurrency". Starting and managing threads in the application code is hard to implement, monitor and debug - it is by no means a best practice. [See Lightweight Asynchronous Facade pattern, page 65 in "Real World Java EE Patterns Rethinking Best Practices" book for more in-depth discussion] 

From http://www.adam-bien.com

Published at DZone with permission of its author, adam bien.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Wujek Srujek replied on Tue, 2010/03/16 - 3:35am

A person like you (Java Evangelist and other stuff) should never, ever post code like this and even think of saying that starting a thread in EJB is allowed. So what that it works? It IS AGAINST the specs. It might work for your simple GF v3 tests (BTW, it did work on JBoss 5 and GF v2.1 as well, so it's nothing new actually), but may fail miserably on other servers, for example those whose security policy prevents starting new threads.

You write that it is not a best practice - it is a NO NO, I would argue. But, novices will come read this post and think that it is possible and not a big deal (maybe not, but the specs is for something, right?).

I am really sorry to say that, but over the last period you created so much crap that I continue to be astound that DZone even considers your posts.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.