Wednesday, 15 April 2015

EJB 3.0 Features in Detail


EJB 3.0 Features


We have already discussed features in EJB 3.0 in the previous post (EJB Features) , so in this post we will discuss features in details so we can understand them easily.

1. ( Deployment descriptor - optional )


Now we can create our EJB application without deployment descriptor XML file by using java metadata annotations for EJB. Annotation were introduced in J2SE 5.0 , are used to simplify the Enterprise Java Beans.


In EJB 3.0 , beans are simple POJO (plain old java object) with annotation, annotation processor creates the deployment descriptor at runtime. The developer doesn't have to maintain multiple files for one bean. For example, the following code shows how to define a simple stateless session bean:

/* Example class for Stateless session */
@Stateless
public class MySessionBean implements MyBean{
    public String getMessage(){
        return "Test Message";
    }
}

/* Example class for Business Interface */
@Remote
public interface MyBean{
    public String getMessage();
}

@Stateless annotation mark the MySessionBean as stateless session bean.

@Remote  annotation mark the MyBean as business interface.

2. Remote Interface are not mandatory to write


Note : Its is not mandatory to write Remote interface in EJB 3.0 we can use @Remote in the bean class itself to let the container generate the business interface,
as in the following example:
@Stateless
@Remote
public class MessageStoreBean{
public String getMessage(){
     return "Test Message";
   }

}

In the above example, the bean class doesn't implement the business interface, and uses @Remote instead. This makes development much easier than in previous versions. EJB 3.0 provides annotations for every type of metadata previously addressed by deployment descriptors, so no XML descriptor is needed and you can deploy your beans simply by deploying a plain old .jar
into your application server.


3. Callback Methods and Listener Classes


In EJB 3.0 , bean developer does not need to implement callback methods which were there in EJB 2.1 like ejbCreate(), ejbPassivate(), ejbActivate(). 
Now we have freedom to make any method as callback using some callback annotations in the bean class itself.
/* Callback method defined inside a bean class */ 
@Stateful 
public class TestBean {
     private int var;
     public int method(){}

     @PreDestroy testMethod(){}
 }

Listener classes: We can create listener classes instead of creating callback methods in the bean, only the difference is in the method signature A callback method defined in a listenerclass must take a Object as a parameter, which is not needed when the callback is in the bean itself.
Here's an example

/* Callback method defined inside a Listener class*/
 public class CustomListener{
     @PrePassivate public testMethod(Object obj){
         // Statements
     } }
 /* Adds callback listener to bean class */
 @CallbackListener
 CustomListener
 @stateful
 public class TestBean{
     private int var;
     public void getPrice(){}
 }

No comments:

Post a Comment