Sunday, February 26, 2012

EJB3 session Bean - simplified Approach




EJB3









In this post, we are going to learn about EJB3.0 session bean and how to deploy it in Jboss 5 application server.

EJB3.0 is quite simple and easy from programmer's point of view. EJB3.0 eliminates the home and component interface declaration and allows Java metadata annotations as an alternative to deployment descriptor.
EJB3.0 specification provides backward compatibility, that means your old EJB2.x code will still work in EJB3 environment.










The creation of a session EJB consists of two steps:
1. Create a simple interface (called business interface) annotated with Local or Remote annotation
2. Create a simple class (called session bean) annotated with a session bean annotation

We need only two files to create a stateless session bean in EJB3.0 as mentioned below:



Business Interface : Unlike EJB2.x , EJB3.0 business interface of a session bean is plain Java interface which is not an EJBObject or EJBLocalObject.

Session Bean : Session bean will implement business interface and must be annotated with stateless annotation  or denoted in deployment descriptor as stateless session bean.

Session bean need not implement the javax.ejb.SessionBean interface.






HelloWorldIx.java
package com.sarf.ejb3.bean;
import javax.ejb.Local;
@Local
public interface HelloWorldIx {
      public String sayHello();
}



@Local annotation is applied to session bean or local business interface to designate a local interface of the bean.




HelloWorldSessionBean.java
package com.sarf.ejb3.bean;
import javax.ejb.Local;
import javax.ejb.Stateless;

@Stateless(name="HelloWorldSessionBean")
@Local(HelloWorldIx.class)
public class HelloWorldSessionBean implements HelloWorldIx{

      @Override
      public String sayHello() {
            return "Hello, EJB3 ! Message from stateless session bean";
      }
}




@Stateless annotation specify that enterprise bean is stateless session bean. @Local annotation specify the name of the business interface.




We need a client to access the session bean. We will write index.jsp page to acess the session bean.

we will use JNDI lookup methodology to acess the EJB.  JNDI lookup approach is similar to our ejb2.x approach  as described below:
- Obtain an InitialContext object
- Use the context to look up the EJB
      Context initialContext = new InitialContext();
      HelloWorldIx helloWorld = (HelloWorldIx)
       initialContext.lookup("MyEJB3Example/HelloWorldSessionBean/local");

By default, when the application is deployed in a jar, session beans will bind to JNDI in the form ejbName/remote for remote interfaces and ejbName/local in the case of local interfaces. When the EJBs are deployed in an .ear file, the default jndi binding will be prepended by the name of the .ear file

In our case, the JNDI binding will look like this
MyEJB3Example/HelloWorldSessionBean/local



index.jsp




web.xml




application.xml




Here is the eclipse project structure









To deploy our EJB3.0 application, we need to bundle all the above mentioned file in a ear file in a specific hierarchy mentioned below: