A quick look at EJB 3.0 Stateless Session Beans

I have been with EJBs for a fairly long time but I have been very much part of the “reluctant EJB developers” community. Give me an option to EJBs and in all probability I will take it. The primary reason was that I did not find EJBs worth the effort. The develop and deploy cycles were unacceptably long, the development seemed unnecessarily confusing, machines crawled once you had a EJB server + IDE running and ofcourse finding skilled EJB developers was a very difficult task.

So I wasn’t very excited or optimistic about EJB3 when I first heard about it somewhere around JavaOne 2004. Every EJB3 session at JavaOne 04 was full and I remember wondering if all these people were there because they have used and found EJB fun or because they were hoping that finally with EJB3 they will understand what’s happening and why their managers were forcing them to use EJBs.

I did do a few experiments with EJB3, but I was essentially waiting for tools to catch on. Today, all major J2EE vendors have at least a EJB3 preview release of their server and IDE. So I think it’s time to learn EJB3.

NetBeans and JDeveloper are my preferred tools. I don’t think NetBeans supports EJB3 as yet, so I decided to try out EJB3 using the recent preview release of JDeveloper 10g. I wrote a book on JDeveloper 10g in early 2004 titled Oracle JDeveloper 10g: Empowering J2EE Development, but that version of JDeveloper supported EJB2.1. JDeveloper’s EJB 3.0 preview release is still called 10g, but I suppose Oracle will rename it to 11z or something like that when the final release is out.

Lets look at a simple stateless session EJB and try to understand what’s new in EJB3. With the JDeveloper wizards, it took me less than aminute to create this app.

Listing 1: DateEJBBean Class

package example.model;
import javax.ejb.Stateless;

@Stateless(name=”DateEJB”)
public class DateEJBBean implements DateEJB, DateEJBLocal {

public DateEJBBean() {

}

public String displayDate() {

return “”+new java.util.Date();

}

}

In the DateEJBBean class in Listing 1, note the following:

  1. It’s a Plain Old Java Object (POJO).
  2. It’s not abstract.
  3. It does not force me to implement any interface/s apart from the ones that I have created.
  4. The Java annotation that states that this is a stateless session bean.

Listing 2: DateEJBLocal Interface

package example.model;
import javax.ejb.Local;

@Local
public interface DateEJBLocal {

public String displayDate();

}

Listing 3: DateEJB Remote Interface

package example.model;
import javax.ejb.Remote;

@Remote
public interface DateEJB {

public String displayDate();

}

The local and remote interface for the EJB are shown in Listing 2 and 3. These are Plain Old Java Interfaces (POJIs) that declare a business method and use an annotation. Exceptions are conspicuous by their absence. In EJB3, you do not have to throw exceptions, because the container will do that for you.

That’s it. No deployment descriptor, no abstract classes, no empty implementations of lifecycle methods and no home interface. You don’t even have to implement a business interface as the container can generate one for you. You just need to add an @Remote annotation in the bean class itself.

Default Values: In an EJB 2.1 deployment descriptor, you had to specify a lot of tags and attributes that you never intended to use or modify. These tags although did not interfere with your work, made EJB a lot scarier than it really was, especially for beginners. With EJB3, there are default values for everything. So you only specify something if you do not wish to use the default value. I think this is the primary reason for EJB3 to appear simpler.

Drawbacks: As more and more Java software makes use of annotations, it’s very likely that annotations will soon get rather irritating. Also remembering all the annotations and their attributes isn’t much fun. With EJB3 you also have to be aware of the default values as unlike earlier versions of EJB, you can’t just open up your deployment descriptor and check the configuration.

EJB3 sure is easy to use. We will next look at Stateful Session Beans, Entity Beans and dependency injection features of EJB3.

Related:
>> Migrating a J2EE application from EJB to JDO
>> With EJB 3.0, any Java developer is an EJB developer
>> Oracle Releases Enterprise JavaBeans 3.0 Preview
>> The main goal for EJB 3.0 is to simplify life for developers
>> Checking EJB 3.0 performance

Harshad Oak

Harshad Oak is the founder of Rightrix Solutions & IndicThreads. He is the author of 3 books and several articles on Java technology. For his contributions to technology and the community, he has been recognized as an Oracle ACE Director and a Sun Java Champion. Contact - harshad aT rightrix doT com & @HarshadOak

0 thoughts on “A quick look at EJB 3.0 Stateless Session Beans

  • January 18, 2006 at 12:51 am
    Permalink

    Congratulation !!!! And best of luck for your future work…. 🙂

Leave a Reply