Enterprise Java Bean(EJB)


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 provide the core package and compile for EJB files.
  2. Application server: - This provides the   runtime environment for EJB. It provides 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 domain
Start->program->web logic platform->user projects->domain name->start server

Finding the default page in 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 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 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 first time then it calls create () method of 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 reference of remote interface from create() method of home interface. This interface can be created by inheriting javax.ejb.EJBOBJECT.

Bean Class

This class contains logic for the methods present in home and remote interfaces. Whenever the clients call any method of home and remote interface, then the EJB container calls the corresponding method available 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 state of the user for a period or session. If the state can be available for only one transaction then it is called as stateless session bean. If the state can be available for more than one transaction then it is called as 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. Persistance logic means creation and maintenance of data in Dbms.This bean can contain state of the user forever. If the persistency logic is being implemented by the bean class explicitly then it is called as bean managed persistency. If the persistency 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 an 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 home interface

    a. Create an interface by inheriting javax.ejb.EJBHOME.

    b. Provide the required create() method to accept initial state of the user. Return type of this method must be 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 accepts or returns only serialized type of data.This method must throws javax.rmi.Remote Exception.
     
  3. Create bean class

    a. Create a class by inheriting 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 to 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
 
//Home interface
package account;
import javax.ejb.*;
import java.rmi.*;
public interface accHome extends EJBHome
{
   public accRemote create()throws RemoteException,CreateException;

 
accRemote.java
 
//Remote interface
package account;
import javax.ejb.*;
import java.rmi.*;
public interface accRemote extends EJBObject
{
   public String validate(int acno,int pno)throws RemoteException;
   public int getBalance(int acno)throws RemoteException;

 
accBean.java
 
//Bean Class
package account;
import javax.ejb.*;
import java.sql.*;
public class accBean implements SessionBean
{
SessionContext sc;
int bal;
public void ejbCreate(){}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(SessionContext s)
{
sc=s;
}
public String validate(int ano,int pno)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from account where accno="+ano+" and pno="+pno);
if(rs.next())
{
bal=rs.getInt("balance");
return "Valid";
}
}
catch(Exception e){System.out.println(e);
}
return "Invalid";
}
public int getBalance(int ano)
{
return bal;
}
}


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 class path for EJB files. The setWLSEnv.cmd file can be available from installation folder of web logic/bin folder. Compile all three files of EJB simultaneously since the home interface file has used 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 a 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 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 server)
    Deploy the EJB (Tool-Deploy module)

Up Next
    Ebook Download
    View all
    Learn
    View all