PREVIOUS NEXT
Does not Exists -> when the bean has not been instantiated.
Enterprise Java Beans
·
Sun Microsystems introduced the J2EE application
server and the Enterprise JavaBean (EJB) specifications as a venture into the
multitier server side component architecture market.
·
It is important to note that though EJB and
JavaBeans are both component models, they are not same. EJBs are interprocess components and JavaBeans are intraprocess components.
·
EJB is a
specification for creating server side components that enables and simplifies
the task of creating distributed objects.
The Key features of EJB are as follows
Ø EJB
components are server side components written using Java
Ø EJB
components implement the business logic only. We need not write code for
system level services, such as managing transactions and security.
Ø EJB
components provides services, such as transaction and security management and can be
customized during deployment.
Ø EJB
can maintain state information across various method calls.
Enterprise JavaBeans Component Architecture
EJB Server which contains the EJB container.
EJB container, which contains enterprise beans
Enterprise
bean, which contains methods that implement the business logic.
EJB Server
EJB Server provides some low level services, such as network
connectivity to the container. It also provides the following services:
- Instance Passivation – If a container needs resource, it can decide to temporarily swap out a bean from the memory storage.]
- Instance Pooling – If an instance of the requested bean exists in the memory, the bean is reassigned to another client.
- Database Connection Pooling – When an Enterprise bean wants the access a database, it does not create a new database connection of the database connection already exists in the pool.
- Precached Instances – The EJB server maintains a cache. This cache information about the state of the enterpirse bean.
sarayoa
EJB Container
- The EJB container acts as an Interface Between and Enterprise bean and the clients. Clients communicate with the enterprise bean through the Remote and Home Interfaces provide by the container.
- The client communicating to the enterprise bean through the container enables the container to service the client’s request with great flexibility. For Instance, the container manages a pool of enterprise beans and uses them as the need arises, instead of creating a new instance of the bean for each client request.
The container also provides the following services:
- Security
- Transaction Management
- Persistance -> Permanent Storage
- Life Cycle Management
Enterprise Bean
Enterprise JavaBeans are write once, run anywhere, middle tier components that consists of methods that
implements the business rule. The enterprise
bean encapsulates the business logic. There are two types of enterprise bean.
- Entity Bean
- Session Bean
Entity Bean
Entity
Beans are enterprise beans that persist across multiple sessions and multiple
clients. There are two types of Entity bean:
- Bean Managed Persistance
- Container Managed Persistance
In a Bean
managed persistance, the programmer has to write the code for database calls.
On the other hand, in container managed persistance, the container takes care
of database calls.
Session Bean
Session
bean perform business tasks without having a
persistent storage mechanism, such as a database and can use the shared data. There are two types of
session beans:
- Stateful Session Bean
- Stateless Session Bean
A Stateful
session bean can store information in an instance
variable to be used across various method calls. Some of the application
require information to be stored across various method calls.
A Stateless session bean do not have instance variables to
store information. Hence, stateless session beans can be used in situations
where information need not to be used across method calls.
Life Cycle of a Stateless Session Bean
Method Ready -> When the container requires it.
Class.newInstance() -> Create a new instance of the
stateless session bean and allocates the required memory
SessionBean.setSessionContext(SessionContext ct) -> Sets
the bean reference to the session context. A session context enables the
enterprise bean to interact with the container. The enterprise bean can use the
session context to query the container for information, such as transactional
and security state.
ejbCreate() -> It is similar to the constructor of EJB
class. It is invoked only once in the life cycle of the stateless session bean,
when the client invokes the create() method of the home interface. The
ejbCreate() method must not take any argument,
as stateless session bean do not store any information in the instance
variable.
ejbRemove() -> ends the life cycle of the stateless
session bean. This method closes any open resource and frees the memory space.
EJB Servers
Sun Java
Server (J2EE Server)
Web
Logic (BEA)
Websphere
(IBM)
STATELESS BEAN
Home Interface
Remote Interface
Bean Class
Client Application
Remote Interface
A remote
interface defines all the business methods of the enterprise bean that the EJB
client would invoke. The remove interface does not include system level
operations, such as persistence, security and transactions.
Home Interface
The home
interface defines methods that allow EJB clients to create and find EJB
components.
Bean Class
The EJB
class implements all the business methods declared in the remote interface.
Eg:
//CalculatorHome.java
// Home Interface
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface CalculatorHome extends EJBHome
{
public Calculator
create() throws RemoteException, CreateException;
}
//Calculator.java
//Remote Interface
import javax.ejb.*;
import java.rmi.RemoteException;
public interface Calculator extends EJBObject
{
public double
dollarToRs(double dollars) throws RemoteException;
}
//CalculatorEJB.java
//Enterprise Bean
class
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class CalculatorEJB implements SessionBean
{
public double
dollarToRs(double dollars)
{
return
(dollars * 47.20);
}
public void
ejbCreate() {}
public void
ejbRemove() {}
public void
ejbActivate() {}
public void
ejbPassivate() {}
public void
setSessionContext(SessionContext sc) {}
}
client program:
//CalculatorClient.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import javax.naming.*;
import javax.ejb.*;
import java.rmi.*;
public class CalculatorClient extends JFrame implements
ActionListener
{
public static
int w = 500;
public static
int h = 95;
public static
String str = "Earnest Bank Welcomes You";
Container c;
JLabel l,result;
JTextField t;
JButton b;
public static
String value;
public static
double dbl;
public static
double amt;
public
CalculatorClient()
{
super(str);
c = getContentPane();
c.setLayout(new GridLayout(2,2,2,2));
l =
new JLabel("Enter the amount in Dollars($)");
c.add(l);
t =
new JTextField(10);
c.add(t);
b =
new JButton("Calculator");
c.add(b);
result
= new JLabel();
c.add(result);
//
value = t.getText();
b.addActionListener(this);
setSize(w,h);
show();
}
public
void actionPerformed(ActionEvent e)
{
dbl = Double.parseDouble(t.getText());
try
{
Properties p=new
Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
Context ic= new InitialContext(p);
CalculatorHome
h=(CalculatorHome)ic.lookup("rose");
Calculator r=h.create();
amt =
r.dollarToRs(dbl);
result.setText("Result(Rs.): " + String.valueOf(amt));
}catch(Exception ex)
{
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
public static
void main(String args[])
{
CalculatorClient m = new CalculatorClient();
}
}
//<applet code="CalculatorClient" height=500
width=500></applet>
[this is for j2ee server]
{
Compile
the Source files
s.bat
path=%path%;d:\jdk1.2\bin;d:\j2sdkee1.2\bin
set
classpath=d:\j2sdkee1.2\lib\j2ee.jar;d:\demo\ejb;
set
java_home=d:\jdk1.2
set
j2ee_home=d:\j2sdkee1.2
PREVIOUS NEXT
No comments:
Post a Comment