EJB 3 (@Session) For Absolute Beginners - 3 Minutes Bootstrap, 5 Steps

Requirements:

  1. Installled JDK 1.5 (better 1.6) 
  2. An IDE of your choice e.g. vi, emacs, netbeans 6.1 (SE or EE), Eclipse Genymede (SE or EE)
  3. @Stateless, @Local Annotations in classpath
  4. An Java EE 5 capable application server of your choice. It will work with Glassfish v1+ (better v2), JBoss 4.2+, WLS 10+ and probably Geronimo (not tried yed)

What is to do:

  1. In the IDE you will have to point to a JAR containing the two annotations. If you have the Reference Implementation installed (Glassfish), put just: glassfish\lib\javaee.jar to the classpath. IDEs with built in EE support have already everything you need. However for the very first time I would prefer to develop "from scratch" an EJB.
  2. Create an interface with a method (without a method is a little bit odd :-)):
    import javax.ejb.Remote;

    @Remote
    public interface HelloWorld {
        public void sayHello();
    }
  3. Create a class which implements this interface. You will be forced by a good IDE (probly not by vi or emacs) to implement this interface:
    import javax.ejb.Stateless;

    @Stateless
    public class HelloWorldBean implements HelloWorld {
       
         public void sayHello() {
            System.out.println("Hello!");
        }
    }
  4. Compile everything and JAR the output (in Netbeans just "build", in Eclipse "Export -> JAR")
  5. Copy the JAR into the autodeploy folder of WLS 10 (bea10\user_projects\domains\YOUR_DOMAIN\autodeploy), or glassfish\domains\domain1\autodeploy in the case of Glassfish v2, or jboss-4.2.2.GA\server\default\deploy in case of JBoss
  6. Inspect the log files, you are done :-)

What you have gained:

  1. It's threadsafe (in multicore environments as well) 
  2. Remoting: you can access the interface remotely
  3. It's transactional - transactions are started for you
  4. It's pooled - you can control the concurrency and prevent "denial of service" attacks.
  5. It's monitored: and EJB have to be visible through JMX. Application servers provide additional monitoring services as well.
  6. Dependency Injection just works - you can inject persistence, other beans, legacy pojos (I will cover this in some upcomings posts)
  7. It's portalble and so vendor-neutral. Deployment to different application servers just works
  8. There is NO XML.
  9. Its easily accessible (via DI), from Restful services, JSF, Servlets etc.
  10. Clustering and security are beneficial as well - but not the main reason to use EJBs

Comments:

You forgot Intellij Idea. It has a fantastic Java EE 5 and Glassfish support ;o)

Posted by Antonio Goncalves on July 01, 2008 at 04:05 PM CEST #

Hi Antonio,

IntelliJ is great, indeed. I forgot to mention it - but JDeveloper, RAD etc. should work as well :-)

Posted by Adam Bien on July 01, 2008 at 05:12 PM CEST #

Dear Adam,
very impressed by your persistence in promoting NetBeans and lead mainly by this promotion, I recently installed 6.1 and evaluated. Unfortunately I must inform that it's still unusable :)

I periodically check each new major release of this IDE and of course have to admit there's some progress. However, Java code editor still sucks when trying to compare it with Eclipse's (for example: search function is in completely different league; haven't seen very practical "quick fix" function; Am I right there's no bulk getters/setters auto-generation?; even icons are unreadable).

Sun can put megaliard of new enterprise creators, tools, integration supports etc. and NetBeans will still suck - because of mediocre code editor.

regards
adg

Posted by adg on July 01, 2008 at 08:11 PM CEST #

Adg,

you are considering this post as Netbeans promotion? :-)

I'm using Genymede and Netbeans 6.1 now. Both are great.

Quick fix works actually great (alt+enter) and I have no problem with searching in both IDEs.

Try in NB alt+insert -> you should be able to find the "Getter/Setter" wizzard.

Thank you for the information :-)

I get a fee for every sold Netbeans license - it's great business for me...:-)

Posted by Adam Bien on July 01, 2008 at 08:31 PM CEST #

Adg,

you remembered me to mention my favorite NB-editor functionality -> stay tuned!,

thank you for the comment,

regards,

adam

Posted by Adam Bien on July 01, 2008 at 09:48 PM CEST #

Thanks for the post !
I should try this at home. I learnt EJB 2.1 in the university and I hated it completely. So I moved to modeling and since then I am an Eclipse EMF guy :-)

Btw, Eclipse EMF is the reason why I won't use Netbeans :-)

I wish the smart guys at EMF and EJB will come together and connect these two technologies.

Posted by Thomas on July 01, 2008 at 10:44 PM CEST #

Hi Thomas,

I'm using EMF with Eclipse as well. But ...there is actually no relation between EJB and EMF. EMF is really great for creating custom metamodels and code generation - this is what I do. I'm using Netbeans in other projects for the "real work" :-). SDO could be interesting with EMF as well.

Yes - EJB 2.1 just worked - and EJB 3 are great and they work :-). Behind the scenes are both similar :-),

thank you for the post!,

adam

Posted by Adam Bien on July 02, 2008 at 01:03 AM CEST #

@Adam
"you remembered me to mention my favorite NB-editor functionality -> stay tuned!,"
Ok, I am looking forward for this and we will start some serious IDE wars here ;)

"I get a fee for every sold Netbeans license - it's great business for me...:-)"
I've never doubt it ;)

regards
adg

Posted by 77.236.1.117 on July 02, 2008 at 12:33 PM CEST #

Hi Adam,

Good stuff! Please contribute this to the NetBeans Community Docs wiki at http://wiki.netbeans.org/CommunityDocs.

Thanks,

--James Branam

Posted by james Branam on July 03, 2008 at 04:36 PM CEST #

I love NetBeans from 5.5.1 on up

Posted by Bob Ferrero on July 04, 2008 at 09:37 AM CEST #

It's great to see how much easier things are becoming. I've not used EJB for a long time but I'm getting tempted to have another look.
I've seen in other examples that an @Local interface is also created - is this overhead still really required?
Thanks.

Posted by Tom on July 04, 2008 at 12:01 PM CEST #

Great tutorial. What is the simplest way to call this bean from a small java program? Do you have a post on that?

Posted by VikasG on April 11, 2013 at 11:49 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License