Presentasi The Triad..

Download Report

Transcript Presentasi The Triad..

The Triad of Beans I
Oleh:
Dini Addiati (1203000366)
Fahrurrozi Rahman (120300042Y)
Irfan Hilmy (1203000552)
Salman Azis A (1203001001)
Aziiz Surahman (1203007026)
Titin Farida(1203007077
Session Beans

What is Session Beans?


A session beans represent work being performed for
client code that is calling it
Session Bean life time



Equivalent of a session of the client code that is calling
the session bean
Non-persistent
Could be as long as a browser window is open, as
long as your java applet is running, as long as your
java application is open,as long as another bean is
using your bean.
SESSION BEANS SUB TYPES


Stateful Session Bean
Stateless Session Bean
MonSessionBean
MonSessionBean
Atribut
method
ejbCreate(Arg arg)
stateful
method
ejbCreate()
stateless

Stateful Session Bean




a bean that is designed to service business processes that
span multiple method requests or transactions
Retaining state on behalf of an individual client
If state is changed during a method invocation, that same
state will be available to that same client upon the following
invocation
Stateless Session Bean




a bean that holds conservations that span a single method
call
Do not hold multhimethod conservations with their clients.
Can be pooled, reused, and swapped from one client to
another client on each method call
Example:
credit card verification
Characteristic of Stateful Session Beans

Passivation



Activation


Container use object serialization (or equivalent protocol ) to
convert the bean’s conversational state to bit blob and write out
state to disk
The bean instance can be reassigned to a different client, and
can hold a brand- new conversation with that new client
A serialized blob that had been written is read back into memory
and converted to in-memory bean data
What makes the whole process work??


javax.ejb.EnterpriseBean interface extends java.io.Serializable
every enterprise bean class indirectly implements this interface
 reapplied recursively on those objects
Entity Beans

Entity Beans are persistent objects that
can be stored in permanent storage.
Features of Entity Beans

Entity Beans Survive Failures. They survive critical failures such
as server crashing or even database crashing.

Entity Beans Instance Are a View into a Database. The object
and the database itself are one and the same.

This means update to the object will update the database too.

Several Entity Beans Instances May Represent the Same
Underlying Data. EJB dictates only single thread can run within a
bean instance. We could allow containers to instantiate multiple
instance of the same entity bean class.

This would allow many clients to concurrently interact with separate
instance, each represent the same entity data.
Features of Entity Beans

Entity Bean Instances Can Be Pooled.



Entity bean instances are recyclable objects and may be pooled
depending on container’s policy.
The container may pool and reuse entity bean instances to
represent different instances of the same type of data.
There Are Two Ways to Persist Entity Beans.


A bean-managed persistent entity bean is entity bean that must
be persisted by hand. The component developer must write the
code to translate the object in the database.
Another way is container-managed persistence. In this case, the
container perform the persistence.
Features of Entity Beans

Creation and Removal of Entity Beans.


Because entity beans instance and the underlying database are
one and the same , the initialization of an entity bean instance
should entail initialization of database data.
Entity Beans Can Be Found.


Because entity bean data is uniquely identified in an underlying
storage, entity beans can also be found rather than created.
We can define many ways to find an entity bean. Just list all
methods in the entity bean home interface. These are called
finder methods.
Features of Entity Beans

We Can Modify Entity Bean Data without
Using EJB.

We can directly modifying the underlying database
where the bean data is stored. For example we can
simply delete/create the entity bean data by directly
delete/add the row in the database.
Writing Bean-Managed Persistent Entity Beans:
A Bank Account







Implements javax.ejb.EntityBean
Finding existing entity beans: ejbFind()
(finder methods).
Create java files
Create deployment descriptor (XML file)
Create container-specific deployment descriptor
Setting up the database
Running the client program
javax.ejb.EntityBean interface
public interface javax.ejb.EntityBean
extends javax.ejb.EnterpriseBean {
public void setEntityContext(javax.ejb.EntityContext);
public void unsetEntityContext();
public void ejbRemove();
public void ejbActivate();
public void ejbPassivate();
public void ejbLoad();
public void ejbStore();
}
javax.ejb.EnterpriseBean interface
public interface javax.ejb.EnterpriseBean implements
java.io.Serializable{
}

ejbFind()
Finder methods : Find existing bean in storage,
load old entity bean data, not create new database.
Example other finder methods:
/** Finds the unique bank account indexed by primary key
*/
public AccountPK ejbFindByPrimaryKey(AccountPK key)
throws FinderException {...}
/** Find the all product entity beans. Return a collection of
primary keys
*/
public Collection ejbFindAllProducts()
throws FinderException {...}
Finder Methods Rules





All finder methods must begin with ejbFind
You must have at least one finder method, called
ejbFindByPrimaryKey.
You can have many different finder methods, each
with different names and different parameters.
A finder method must return either primary key for
the entity bean it finds or a collection of primary keys
if it finds more than one.
As with ejbCreate(), client do not invoke your
finder methods on the bean instance itself.
The Account Bean’s ejb-jar.xml deployment descriptor
<?xml version=“1.0”?>
<!DOCTYPE ejb-jar PUBLIC ‘-//Sun Microsystems,
Inc.//DTDEnterprise JavaBeans 2.0//EN’
’http://java.sun.com/dtd/ejb-jar_2_0.dtd’>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>Account</ejb-name>
<home>examples.AccountHome</home>
<remote>examples.Account</remote>
.
.
.
</assembly-descriptor>
</ejb-jar>
Bean-Managed Persistent Example:
A Bank Account
<<interface>>
java.rmi.Remote
Comes with Java 2 Platform
<<interface>>
java.io.Serializable
<<interface>>
javax.ejb.EnterpriseBean
<<interface>>
<<interface>>
<<interface>>
<<interface>>
javax.ejb.EJBLocalObject javax.ejb.EJBObject javax.ejb.EJBHome javax.ejb.EJBLocalHome
Comes with EJB Distribution
<<interface>>
Bank Account
Local Interface
<<interface>>
Bank Account
Remote Interface
<<interface>>
Bank Account
Home Interface
<<interface>>
javax.ejb.EntityBean
<<interface>>
Bank Account Bean Bank Account
Bank Account
Implementation Primary Key Class
Local Home Interface
Class
Supplied by Bean Provider (We Will Write)
Bank Account
EJB Local Object
Bank Account
EJB Object
Bank Account
Home Object
Bank Account
Local Home Object
Generated for Us by Container Vendor’s Tools
Figure 1. The bank account object model
The files that we must create for our entity bean
component

Account.java : is our entity bean’s remote interface-what
remote clients use to call our bean’s methods.

AccountLocal.java : is our entity bean’ local interface-what
local clients use to call our bean’s methods.

AccountHome.java : our home interface

AccountLocalHome.java : our local home interface, the
higher performing home interface used by local clients.

AccountPK.java : our entity bean’s primary key class.
AccountBean.java : our bean implementation code
divided into several sections.

Bean-managed state fields : persistable fields of our entity
bean class. Our bean instance will load and store database
data into these fields.

Business logic methods : this method perform services for
clients, such as withdrawing or depositing into an account.

EJB-required methods : these are EJB-required methods
that the container calls to manage our bean.

AccountException.java
Client.java

The lifecycle of a bean-managed persistent entity bean
Does Not Exist
1:newInstance()
1: unsetEntityContext()
2:setEntityContext()
2: JVM Will Garbage Collect
and Call finalize()
ejbFind()
ejbHome()
1: ejbCreate()
2: ejbPostCreate()
Pooled
Activate Your Bean: Passive Your Bean:
1: ejbActivate()
1: ejbStore()
2: ejbLoad()
ejbLoad()
ejbRemove()
2: ejbPassivate()
Ready
ejbStore()
Business Method
Figure 2. The lifecycle of a bean-managed persistent entity bean