EJB In Java

EJB Projects

EJB Project 1

EJB Examples

EJB EXAMPLE

adplus-dvertising
Enterprises Java Beans

An enterprise bean is a server-side component that encapsulates the business logic of an application. It written in java so, it is called enterprise java beans. EJB is a managed, server-side component architecture for modular construction of enterprise applications.

The EJB technology provides a distributed component architecture which integrates many enterprise-level requirements like distribution, persistence, transactions, messaging, security, and connectivity to mainframes . EJB technology provides us with different types of components for business logic, persistence, and enterprise messages. They are not only platform independent but also implementation independent. That is EJBs can run in any application server that implements the EJB specifications.

EJB Tutorials and Interview Question
EJB Component Architecture

An enterprise java bean is a non-visual component of a distributed, transaction -oriented application. enterprise beans are deployed in EJB containers and run on EJB servers. there are mainly three types of EJB session beans,entity beans ,message driven beans.

EJB Server

An EJB server provide environment for the support and execution of application developed using EJB components. An EJB server is a high-level process or application server that gives a run-time environment to support the execution of server applications which are used enterprise beans. An EJB server provide one or more containers.

EJB Container

An EJB container manages the EJB contain inside it. For each enterprise bean, the container is responsible for registering the object, providing a remote interface for the object, creating and destroying object instances, checking security for the object, managing the active state for the object, and coordinating distributed transactions. Optionally, the container can also manage all persistent data within the object.

Enterprise Beans

An enterprise bean is a server-side component that encapsulates the business logic of an application. written in the Java programming language.The business logic is the code that fulfill the purpose of application. In an application enterprise beans add the business logic.

Benifits Of Enterprises Beans

Enterprise beans simplify the development of large, distributed applications. because the EJB container provides system-level services to enterprise beans, the bean developer can concentrate on solving business problems. EJB container is responsible for system-level services such as transaction management and security authorization

The EJB architecture provides a simple, elegant component container model. Java server components can be developed once and deployed in any EJB-compliant server.

The EJB architecture can be used for small-scale or large-scale business transactions. As processing requirements grow the enterprise beans can be migrated to more powerful operating environments

Types Of Enterprises Beans

There are tree types of enterprise beans

  • Session beans - Session bean perform task for a client A session bean represents a single client and is not shared across clients.
  • There are two types of session beans: statefull and stateless.

    Statefull session bean

    A stateful session bean maintains a conversational state with one client for the duration of a single session.

    Stateless session bean

    A stateless session bean does not maintain a conversational state for each individual client

    Stateless session beans are not persisted to secondary storage by the EJB container,therefore a programmer must recognize that all data is transient between invocations for each client
  • Entity bean - Represent a business entity object that persistent in the storage. entity bean can be restored to its original state by the EJB container. Entity beans allow shared access – they may be shared by multiple clients ,Entity beans have primary keys – primary-key classes exist to identify an instance of an entity bean. and Entity beans can participate in transactions
  • Message -driven bean - Acts as a listener for the Java Message Service API, processing messages asynchronouslyAn instance of an MDB is instantiated by an EJB container to act as a message listener. Messages received by an MDB may be sent by any JMS-compatible message transmitter, such as a JMS-enabled publish/subscribe topic.
Creating an Enterprises Beans

The creation of enterprise java bean requires to code remote interface, home interface, and EJB class To understand EJB here we take the example of stateless session bean.

Coding the remote interface

A remote interface defines the business methods that a client may call. The business methods are implemented in the enterprise bean code.

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;

public interface Converter extends EJBObject
{
   public BigDecimal dollarToRupee(BigDecimal dollars) 
      throws RemoteException;
   public BigDecimal RupeeToEuro(BigDecimal Rupee) 
      throws RemoteException;
}

Coding home interface

A home interface defines the methods that allow a client to create, find, or remove an enterprise bean. The Home interface contains a single create method, which returns an object of the remote interface type .
import java.io.Serializable;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;



public interface ConverterHome extends EJBHome

	{

   Converter create() throws RemoteException, CreateException;

}

Coding Enterprise bean class

The enterprise bean class for this example is called ConverterEJBBean. This class implements the two business methods, dollarToRupee and RupeeToEuro, that the Converter remote interface defines.

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import java.math.*;



public class ConverterEJBBean implements SessionBean {



   BigDecimal RupeeRate = new BigDecimal("45.4104");

   BigDecimal euroRate = new BigDecimal("0.016523");



   public BigDecimal dollarToRupee(BigDecimal dollars) 

	   {

      BigDecimal result = dollars.multiply(RupeeRate);
      return result.setScale(2,BigDecimal.ROUND_UP);
   }
   public BigDecimal RupeeToEuro(BigDecimal yen) 
	   {
      BigDecimal result = Rupee.multiply(euroRate);
      return result.setScale(2,BigDecimal.ROUND_UP);
   }
   public ConverterEJBBean() {}

   public void ejbCreate() {}

   public void ejbRemove() {}

   public void ejbActivate() {}

   public void ejbPassivate() {}

   public void setSessionContext(SessionContext sc) {}

}