Enterprise Java Bean(EJB)

Introduction 

 
Enterprise java bean is a technology to create deployable business components. These components implement business logic in the application layer of a distributed architecture. If a component can be available in an application server for any type of client then it is called a deployable component.
 

Software requirement of EJB

  1. JDK - This provides the core package and compile for EJB files.
  2. Application server - This provides the runtime environment for EJB. It provides an EJB container and web container. This can be WebLogic, WebSphere or JBoss.

Using Web logic

 
Domain creation in web logic
 
Domain - This is a name to refer to a folder of the file system. A domain can be used to start the server. The server present in a domain can be used to deploy web applications and EJB.
 

In web logic 7.0

 
Start-> program->web logic platform->configuration wizard->Choose WLS domain+ provide domain name.
 
->A single server->provide server name and port number (keep default)->provide username + password->window service (no)-> start menus (yes)->create->End configuration wizard->next->finish
 
Starting server in the domain
 
Start->program->web logic platform->user projects->domain name->start server
 
Finding the default page in a browser
 
Use http://localhost:7001 in any web browser after starting the server.
 
Context creation in WebLogic 7.0 for servlet and JSP
  1. Create the context folder inside the domain folder with WEB-INF classes and web.xml in their predefined locations. Open administration console in a web browser by using http://localhost:7001/console 
  2. Click on the web application node in the applet window. Configure a new web application. Click on the select link available besides the context folder. Move available server to target server. Provide application name as the context name. Configure and deploy (wait for the status to become true)
     
  3. To get the index of the context in the web browser, click configuration tab->click file tab->Choose index directory->apply.
     
  4. Restart the server and access the context.

In web logic 10

 
Domain creation
 
Start->program->Bea product->Tools->Configuration wizard->Create a new web logic domain->next->next
Provide username + password ->next->next->next
Provide domain name and location (keep default)
 
Creation of context in web logic 10
  1. Access the administration console in the web browser by using http://localhost:7001/console provide userid + password. Click on deployment ->Click on lock and edit->Click on install->Select the folder of the context->Next->Next->Choose no –Finish
  2. Click on activate changes->Select the application->start->servicing all request->yes

Architecture of EJB

 
EJB architecture 
 
Home Interface
 
This interface contains methods for creating instances of EJB. Whenever the client application wants to communicate with EJB for the first time then it calls create () method of the home interface. This interface may contain more than one create() method. This interface can be created by inheriting javax.ejb.EJBHOME interface.
 
Remote Interface
 
This interface contains methods to implements business logic. The client application calls this method after getting a reference to a remote interface from create() method of the home interface. This interface can be created by inheriting javax.ejb.EJBOBJECT.
 
Bean Class
 
This class contains logic for the methods of present in-home and remote interfaces. Whenever the clients call any method of home and remote interface, then the EJB the container calls the corresponding method available in bean class. The bean class can be three types, they are below
  1. Session bean
  2. Entity bean
  3. Message-driven bean
Session bean
 
This type of bean implements business logic in the application layer. This bean can perform all types of operations or calculation required for an application. This bean can contain the state of the user for a period or session. If the state can be available for only one transaction then it is called a stateless session bean. If the state can be available for more than one transaction then it is called a state full session bean. A session bean can be created by inheriting javax.ejb.SessionBean interface.
 
Entity Bean
 
This bean can be created to implement persistence logic. Persistence logic means the creation and maintenance of data in Dbms. This bean can contain the state of the user forever. If the persistence logic is being implemented by the bean class explicitly then it is called as bean-managed persistency. If the persistence logic gets implemented by the EJB container then it is called as container-managed persistency. An entity bean can be created by inheriting javax.ejb.EntityBean interface.
 
Message-driven bean
 
This bean can be used to implements any type of logic or acceptance of a message from a jms client. Whenever a Jms client sends a message to the MOM then this bean receives the message to implements the required logic. This bean does not need home and remote interface. This bean can be created by inheriting javax.ejb.MessageDrivenBean.
 

Creation process of EJB

  1. Create a home interface 
    a. Create an interface by inheriting javax.ejb.EJBHOME.  
    b. Provide the required create() method to accept the initial state of the user. The return type of this method must be a remote interface type. This method can accept only serialized type of parameter. This method must throw javax .rmi.Remote exception and javax.ejb.CreateException.  
  2. Create remote interface  
    a. Create an interface by inheriting javax.ejb.EJBObject.  
    b. Provide the required methods to implements business logic.  
    c. The business methods can accept or returns only serialized type of data. This method must throw javax.rmi.Remote Exception.  
  3. Create bean class  
    a. Create a class by inheriting an appropriate interface  
    b. Provide ejbCreate() method to represent create() method of home interface. The parameter list of create() method and ejbCreate() method must match. This method need not throw any exception.  
    c. Override all methods present in remote interface with the logic.  
    d. Override all method present in the super interface.
Example
 
accHome.java
  1. //Home interface  
  2. package account;  
  3. import javax.ejb.*;  
  4. import java.rmi.*;  
  5. public interface accHome extends EJBHome  
  6. {  
  7.    public accRemote create()throws RemoteException,CreateException;  
  8. }   
accRemote.java
  1. //Remote interface  
  2. package account;  
  3. import javax.ejb.*;  
  4. import java.rmi.*;  
  5. public interface accRemote extends EJBObject  
  6. {  
  7.    public String validate(int acno,int pno)throws RemoteException;  
  8.    public int getBalance(int acno)throws RemoteException;  
  9. }   
accBean.java
  1. //Bean Class  
  2. package account;  
  3. import javax.ejb.*;  
  4. import java.sql.*;  
  5. public class accBean implements SessionBean {  
  6.     SessionContext sc;  
  7.     int bal;  
  8.     public void ejbCreate() {}  
  9.     public void ejbRemove() {}  
  10.     public void ejbActivate() {}  
  11.     public void ejbPassivate() {}  
  12.     public void setSessionContext(SessionContext s) {  
  13.         sc = s;  
  14.     }  
  15.     public String validate(int ano, int pno) {  
  16.         try {  
  17.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  18.             Connection con = DriverManager.getConnection("jdbc:odbc:dsn1""system""pintu");  
  19.             Statement stmt = con.createStatement();  
  20.             ResultSet rs = stmt.executeQuery("select * from account where accno=" + ano + " and pno=" + pno);  
  21.             if (rs.next()) {  
  22.                 bal = rs.getInt("balance");  
  23.                 return "Valid";  
  24.             }  
  25.         } catch (Exception e) {  
  26.             System.out.println(e);  
  27.         }  
  28.         return "Invalid";  
  29.     }  
  30.     public int getBalance(int ano) {  
  31.         return bal;  
  32.     }  
  33. }  

Storing compilation and deployment of EJB

  1. Store the EJB files inside the specified package folder. The package folder can be available in any folder of the file system.  
  2. Compilation - Execute setWLSEnv.cmd file to set the path and classpath for EJB files. The setWLSEnv.cmd file can be available from the installation folder of web logic/bin folder. Compile all three files of EJB simultaneously since the home interface file has used the remote interface name as  
    --- \ejb\account>c:\bea\weblogic700\server\bin\setWLSEnv 
    --- \ejb\account>javac *.java  
  3. Deployment  
    a. Create a deployment descriptor. This is an XML file to contain information about the EJB. This file must be named, as EJB-jar.xml.This file must be available in the META-INF folder. If this file is absent the WebLogic creates this file during deployment.  
    b. Create a jar file  
    A jar file required to be created to contain all. Class files of EJB with their package folder. Use jar command in command prompt to create the jar file. This command must be used in the parent folder of the package folder to include the package folder inside it.  
    ---\ejb\account\cd.. 
    ---\ejb>jar-cvf e.jar account\*.class  
    c. For web logic 7.0  
    Start the server  
    Start->program->weblogic platform->user project->domain name->start server  
    --Open->program->weblogic platform->weblogic server->weblogic builder  
    --Open the jar file of EJB in WebLogic builder.File->open(click on yes to create deployment descriptor)save the changes file->save 
    Remember on changes the JNDI name given to EJB connect to server (Tools-connect to the server) 
    Deploy the EJB (Tool-Deploy module)