the triad of beans i oleh: dini addiati (1203000366) fahrurrozi rahman (120300042y) irfan hilmy...

20
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

Upload: marshall-copeland

Post on 29-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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

Page 2: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 3: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

SESSION BEANS SUB TYPES Stateful Session Bean Stateless Session Bean

stateful stateless

MonSessionBean

Atribut

methodejbCreate(Arg arg)

MonSessionBean

methodejbCreate()

Page 4: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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

Page 5: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

Characteristic of Stateful Session Beans Passivation

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

Activation 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

Page 6: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman
Page 7: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

Entity Beans

Entity Beans are persistent objects that can be stored in permanent storage.

Page 8: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 9: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 10: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 11: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 12: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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

Page 13: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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{

}

Page 14: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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 {...}

Page 15: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 16: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

<?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>

The Account Bean’s ejb-jar.xml deployment descriptor

Page 17: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

Bean-Managed Persistent Example: A Bank Account

<<interface>>java.rmi.Remote

<<interface>>javax.ejb.EJBLocalObject

<<interface>>javax.ejb.EJBObject

<<interface>>javax.ejb.EJBHome

<<interface>>javax.ejb.EJBLocalHome

<<interface>>Bank Account

Local Interface

<<interface>>Bank Account

Remote Interface

<<interface>>Bank Account

Home Interface

<<interface>>Bank Account

Local Home Interface

Bank AccountEJB Local Object

Bank AccountEJB Object

Bank AccountHome Object

Bank AccountLocal Home Object

<<interface>>javax.ejb.EnterpriseBean

<<interface>>javax.ejb.EntityBean

Bank Account BeanImplementation

Class

Bank AccountPrimary Key Class

<<interface>>java.io.Serializable

Comes with Java 2 Platform

Comes with EJB Distribution

Supplied by Bean Provider (We Will Write)

Generated for Us by Container Vendor’s Tools Figure 1. The bank account

object model

Page 18: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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.

Page 19: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

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

Page 20: The Triad of Beans I Oleh: Dini Addiati (1203000366) Fahrurrozi Rahman (120300042Y) Irfan Hilmy (1203000552) Salman Azis A (1203001001) Aziiz Surahman

ejbRemove()

1: ejbCreate()

2: ejbPostCreate()

Does Not Exist

Pooled

1: unsetEntityContext()

2: JVM Will Garbage Collect and Call finalize()

1:newInstance()

2:setEntityContext()

Passive Your Bean:

1: ejbStore()

2: ejbPassivate()

Activate Your Bean:

1: ejbActivate()

2: ejbLoad()

Ready

Business Method

ejbStore()

ejbLoad()

ejbFind()

ejbHome()

The lifecycle of a bean-managed persistent entity bean

Figure 2. The lifecycle of a bean-managed persistent entity bean