Thursday, June 7, 2007

I love EJB3!!!!

So for the last 4 years or so a lot of the development I've done has revolved around J2EE. This means using things like EJB, JMS, MBeans, and usually other goodies offered buy JBoss. I've always liked EJBs, especially the way they remove concerns from the developer like threading, locking, synchronization and clustering. However, I have never liked how hard they are to develop and deploy. Things are so much easier in EJB3. Here is a comparison.

In EJB2.X

To deploy a stateless EJB in 2.1 you need to write the bean and make sure you extend SessionBean. Then implement a home, make sure you write the correct method! The create your local and remove interfaces and place the correct methods you want to expose in both EXACTLY as they are in the bean definition. Then you need to write 2 very complex xml documents telling the application server how to deploy and manage your bean. Then package it up into a jar, place it in an ear and deploy.

Now to access the bean you need to create a InitialContext. Then do a lookup on the bean and get the home method. Now use the home method to create an instance of the been itself. Now you can use it.

In EJB3

Write a pojo bean with the business methods you want. Have it extend a local interface and have it override methods in that.

Place the @Stateless annotation on your bean telling the app server this is a stateless bean.
Package it up in an ear and deploy.

To access a bean, if you are in another ejb3 just annotate the member variable with:

@EJB
MySessionBean bean;

And when that bean is created it will also create an instance of your bean and inject it!

Now you get the power of EJB with the simplicity of writing plain old java! I love it! For more information you can go to JBoss or get this really great book.


Happy Coding!