Tuesday, February 10, 2009

EJB Basics

Enterprise Java Beans:
EJB is both a set of specifications (often called contract) and API. Any application server which is EJB conformant should follow this specification and provide implementation to the API.

What is the use of EJB?
Suppose you have to develop a huge distributed application having complex business logics. While developing such an application you need to take care of several factors like security, transaction management, Remote method invocation, Threading, system management etc to name a few. Thanks to application servers which take care of all these issues. This is possible because these application servers follow certain specifications. If this specification is EJB specification then they are called EJB conformant application servers (e.g. BEA Weblogic, IBM websphere). Further they also support EJB API and all you need to do is to call or override the required functions. Thus you can see that EJB simplifies the development of business component. However if your application is very small having very less business logic then using EJB is not a good idea. The purpose of using EJB is to simplify the development of complex distributed business components and not to increase the complexity of simple business components!!

Types of EJB:
1) Session Bean
a) Stateful
b) Stateless
2) Entity Bean
a) Container managed persistence
b) Bean managed persistence
3) Message Driven Bean

Session Beans:
You write business logic in session bean.
Non persistent
Short lived – life terminates with the termination of client
It is non sharable i.e. it can have only one client at a time.

Stateless Session Bean:
Stateless session beans, as the name suggests, don’t maintain state information.
Because these beans don’t contain state information, same been can be used by more than one client (but one client at a time)
Stateless session beans can be pooled by the container
Since stateless session beans can support multiple clients the offer better scalability
Since stateless session beans are never written to secondary storage, they give better performance.
They can implement web service.

Writing Stateless Session bean:
Step 1:
Write the Remote interface:

ShakilRemote.java (Remote interface)

import javax.ejb.*;
import java.rmi.RemoteException;
import java.rmi.Remote;

public interface ShakilRemote extends EJBObject{

public String hello()throws RemoteException;
}

Step 2:
Write Home interface:

HelloHome.java (Home interface)
import javax.ejb.*;
import java.rmi.RemoteException;

public interface ShakilHome extends EJBHome{
Hello create() throws RemoteException,CreateException;
}

Step 3:
Write the stateless Session bean


ShakilBean.java (stateless session bean)

import javax.ejb.*;
public class ShakilBean implements SessionBean
{
private SessionContext ctx;
public void ejbCreate()
{
System.out.println("inside ejbCreate");
}
public void ejbRemove(){
System.out.println("inside ejbRemove");
}
public void ejbActivate(){
System.out.println("inside ejbActivate");
}
public void ejbPassivate(){
System.out.println("inside ejbPassivate");
}
public void setSessionContext(SessionContext ctx){
System.out.println("inside setSessionContext()");
}
public String hello()
{
System.out.println("Hello world");
}

}

Step 4:
Create ejb-jar.xml

No comments:

Post a Comment