Enterprise JavaBeans - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Enterprise JavaBeans

Description:

entity beans model real-world objects. persistent in nature, can be externally manipulated ... { public Account create(String name, String ssNumber, MoneyAmount ... – PowerPoint PPT presentation

Number of Views:200
Avg rating:3.0/5.0
Slides: 35
Provided by: facultyWa8
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Enterprise JavaBeans


1
Enterprise JavaBeans
Trademark of Sun Microsystems, Inc.
2
Agenda
  • Review
  • Interfaces
  • Deployment Descriptors
  • Using Beans
  • Homework 5

3
Review
  • need development and runtime environment
  • J2EE SDK
  • development environment from tool vendor like
    Together, Inprise
  • runtime environment from Sun, IBM, BEA, Inprise,
    etc.
  • beans are server-side components
  • entity beans model real-world objects
  • persistent in nature, can be externally
    manipulated
  • session beans manage processes or tasks
  • transient in nature, though have side effects
    that perist
  • message-driven beans increase reliability and
    efficiency
  • transient in nature, process messages sent to
    message server

4
Interface Review
  • remote interface defines business methods
    EJBObject extension
  • home interface defines life cycle methods
    EJBHome extension
  • bean class implements business methods
  • implements EntityBean or SessionBean
  • MessageDrivenBean is a different story, left
    untold
  • primary key for entity beans DB reference

5
Clients and Containers Review
  • client uses home and remote interfaces
  • not bean class itself
  • bean and server/infrastructure interactions
    managed by "container"
  • container creates instances, manages storage,
    etc.
  • tools support container, doing things like
  • mapping between entity beans and DB records
  • generating code from interfaces
  • etc.

6
Remote Interface
  • just the business methods
  • not used by message-driven beans
  • import java.rmi.RemoteException
  • public interface xxx extends javax.ejb.EJBObject
  • public ... throws RemoteException
  • By convention, xxx is name of business object or
    process
  • means any method signature

7
Remote Interface Example
package account import java.rmi.RemoteException
public interface Account extends
javax.ejb.EJBObject public void
debit(MoneyAmount amount) throws
RemoteException, InsufficientFundsException publ
ic void credit(MoneyAmount amount) throws
RemoteException

8
Home Interface
  • life cycle and bean look-up methods
  • not used by message-driven beans
  • import java.rmi.RemoteException
  • import java.ejb.CreateException
  • import java.ejb.FinderException // BMP entity
    bean only
  • public interface xxxHome extends
    javax.ejb.EJBHome
  • public xxx create(...) throws RemoteException,
  • CreateException
  • // BMP entity bean only below
  • public xxxPK findByPrimaryKey(...) throws
  • RemoteException, FinderException

9
Home Interface Example
package account import java.rmi.RemoteException
import javax.ejb.CreateException import
javax.ejb.FinderException public interface
AccountHome extends javax.ejb.EJBHome public
Account create(String name, String
ssNumber, MoneyAmount initialBalance) throw
s RemoteException, CreateException public
findByPrimaryKey(AccountPK pk) throws
RemoteException, FinderException
10
Generic Enterprise Bean Class
public class xxxBean implements
javax.ejb.zzzBean // gtgtgt business methods
here ltltlt public ejbCreate() public
void ejbRemove() // context
setting public void setzzzContext(javax.ejb.zzzC
ontext ctx) // etc.
11
EntityBean Class
public class xxxBean implements
javax.ejb.EntityBean // gtgtgt public instance
variables from persisted storage ltltlt // gtgtgt
business methods here ltltlt public xxxPK
ejbCreate(some key...) return
null public void ejbPostCreate(some key...)
public void ejbActivate() public void
ejbPassivate() public void ejbRemove()
public void setEntityContext(javax.ejb.Entit
yContext ctx) public void unsetEntityContext(
) public void ejbLoad() public void
ejbStore()
12
EntityBean Example, Part 0
// AccountPK.java package account public class
AccountPK implements java.io.Serializable
public String id public int hashCode( )
return // integer hash of id
public boolean equals(Object obj) if(obj
instanceof AccountPK) return (id
((AccountPK)obj).id) return
false public String toString()
return id
13
EntityBean Example, Part 1
// AccountBean.java package account public
class AccountBean implements javax.ejb.EntityBean
private static int count0 public String
id public String name public String
ssNumber public MoneyAmount balance public
void debit(BigDecimal amount) throws
RemoteException, InsufficientFundsException
if (balance.compareTo(amount) gt 0)
balance.Decrement(amount) else raise
InsufficientFundsException //continued
14
EntityBean Example, Part 2
public void credit(MoneyAmount amount) throws
RemoteException balance.Increment(amount)
public AccountPK ejbCreate(String name, String
ssNumber, MoneyAmount initialBalance)
this.name name // check for
errors??? this.ssNumber ssNumber balance
new MoneyAmount(initialBalance) count 1 //
??? id 123456- count // ??? return
null public void ejbPostCreate(String name,
String ssNumber, MoneyAmount initialBalance)
//continued
15
EntityBean Example, Part 3
public void ejbActivate() public void
ejbPassivate() public void ejbRemove()
public void setEntityContext(javax.ejb.Entity
Context ctx) public void unsetEntityContext()
public void ejbLoad() public void
ejbStore()
16
SessionBean Class
public class xxxBean implements
javax.ejb.SessionBean // gtgtgt business
methods here ltltlt public ... ejbCreate(...)
return null public void ejbActivate()
public void ejbPassivate() public void
ejbRemove() public void setSessionContext(ja
vax.ejb.SessionContext ctx)
17
SessionBean Example, Part 0
from Enterprise JavaBeans, R. Monson-Haefel,
OReilly, 2000. // TravelAgent.java //
imports... public interface TravelAgent extends
javax.ejb.EJBObject public String
listCabins(int shipID, int bedCount) throws
RemoteException // TravelAgent???.java //
imports... public interface TravelAgent???
extends javax.ejb.EJB??? public TravelAgent
create() throws RemoteException,
CreateException
18
SessionBean Example, Part 1
from Enterprise JavaBeans, R. Monson-Haefel,
OReilly, 2000. // TravelAgentBean.java //
imports... public class TravelAgentBean
implements javax.ejb.SessionBean public String
listCabins(int shipID, int bedCount)
try javax.naming.Context jc new
InitialContext() CabinHome home
(CabinHome) javax.rmi.PortbleRemoteObject.narro
w( (Object) jc.lookup(CabinHome),
CabinHome.class) Vector list new
Vector() CabinPK pk new CabinPK() Cabin
cabin // continued...
19
SessionBean Example, Part 2
for (int i0 i) pk.id
i try cabin home.findByPrimaryKey(pk)
catch (javax.ejb.FinderException fe)
break if (cabin.getShip() shipID
cabin.getBedCount() bedCount)
list.addElement((String) i,
cabin.getName() ,
cabin.getDeckLevel()) String
strings new Stringlist.size() list.copyInto
(strings) return strings catch (Exception
e) throw new EJBException(e) //
continued...
20
SessionBean Example, Part 3
public TravelAgent ejbCreate() return
null public void ejbActivate() public void
ejbPassivate() public void ejbRemove()
public void setSessionContext(javax.ejb.Sessi
onContext ctx)
21
MessageDrivenBean Class
public class xxxBean implements
javax.ejb.MessageDrivenBean, javax.jms.Messa
geListener public void ejbCreate(...)
public void ejbRemove() public void
onMessage(javax.jms.Message message)
public void setMessageDrivenContext( jav
ax.ejb.MessageDrivenContext mdc)
22
Deployment Descriptors
  • as of EJB 1.1, tags in XML files (ejb-jar.xml)
  • part of a jar file, which defines a bean
  • declares behavior externally vs. inside code
  • allows deployer to change way code works by
    changing environment, including naming
  • lists dependencies
  • groups beans

23
Deployment Descriptors EntityBean Example, Part
1
from Enterprise JavaBeans, R. Monson-Haefel,
OReilly, 2000. ltejb-jargt ltenterprise-beansgt
ltentitygt ltdescriptiongtThis Cabin enterprise
bean entity represents a cabin on a cruise
ship.lt/descriptiongt ltejb-namegtCabinBeanlt/ejb
-namegt lthomegtcom.titan.cabin.CabinHomelt/home
gt ltremotegtcom.titan.cabin.Cabinlt/remotegt
ltejb-classgtcom.titan.cabin.CabinBeanlt/ejb-class
gt ltpersistence-typegtContainerlt/persistence-t
ypegt ltprim-key-classgtcom.titan.cabin.CabinPK
lt/prim-key-classgt ltreentrantgtFalselt/reentran
tgt ltcmp-fieldgtltfield-namegtidlt/field-namegtlt/
cmp-fieldgt ltcmp-fieldgtltfield-namegtnamelt/fiel
d-namegtlt/cmp-fieldgt ltcmp-fieldgtltfield-namegtd
eckLevellt/field-namegtlt/cmp-fieldgt
ltcmp-fieldgtltfield-namegtshiplt/field-namegtlt/cmp-fiel
dgt ltcmp-fieldgtltfield-namegtbedCountlt/field-na
megtlt/cmp-fieldgt lt/entitygt lt/enterprise-beansgt
24
Deployment Descriptors EntityBean Example, Part
2
ltassembly-descriptorgt ltsecurity-rolegt
ltdescriptiongt This role represents
everyone who is allowed full access to
the cabin bean. lt/descriptiongt
ltrole-namegteveryonelt/role-namegt
lt/security-rolegt ltmethod-permissiongt
ltrole-namegteveryonelt/role-namegt ltmethodgt
ltejb-namegtCabinBeanlt/ejb-namegt
ltmethod-namegtlt/method-namegt lt/methodgt
lt/method-permissiongt
25
Deployment Descriptors EntityBean Example, Part
3
ltcontainer-transactiongt ltmethodgt
ltejb-namegtCabinBeanlt/ejb-namegt
ltmethod-namegtlt/method-namegt lt/methodgt
lttrans-attributegtRequiredlt/trans-attributegt
lt/container-transactiongt lt/assembly-descriptorgt lt
/ejb-jargt
26
Deployment Descriptors SessionBean Example, Part
1
from Enterprise JavaBeans, R. Monson-Haefel,
OReilly, 2000. ltejb-jargt ltenterprise-beansgt
ltsessiongt ltejb-namegtTravelAgentBeanlt/ejb-namegt
lthomegtcom.titan.travelagent.TravelAgentHomelt/h
omegt ltremotegtcom.titan.travelagent.TravelAgentlt
/remotegt ltejb-classgtcom.titan.travelagent.Trave
lAgentBeanlt/ejb-classgt ltsession-typegtStatelesslt
/session-typegt lttransaction-typegtContainerlt/tra
nsaction-typegt ltejb-refgt
ltejb-ref-namegtejb/CabinHomelt/ejb-ref-namegt
ltejb-ref-typegtEntitylt/ejb-ref-typegt
lthomegtcom.titan.cabin.CabinHomelt/homegt
ltremotegtcom.titan.cabin.Cabinlt/remotegt
lt/ejb-refgt lt/sessiongt lt/enterprise-beansgt
27
Deployment Descriptors SessionBean Example, Part
2
ltassembly-descriptorgt ltsecurity-rolegt
ltdescriptiongt This role represents
everyone who is allowed full access to
the cabin bean. lt/descriptiongt
ltrole-namegteveryonelt/role-namegt
lt/security-rolegt ltmethod-permissiongt
ltrole-namegteveryonelt/role-namegt ltmethodgt
ltejb-namegtTravelAgentBeanlt/ejb-namegt
ltmethod-namegtlt/method-namegt lt/methodgt
lt/method-permissiongt
28
Deployment Descriptors SessionBean Example, Part
3
ltcontainer-transactiongt ltmethodgt
ltejb-namegtTravelAgentBeanlt/ejb-namegt
ltmethod-namegtlt/method-namegt lt/methodgt
lttrans-attributegtRequiredlt/trans-attributegt
lt/container-transactiongt lt/assembly-descriptorgt lt
/ejb-jargt
29
Deployment
  • put all class files and bean descriptors in a jar
    file
  • make a folder called META-INF at root of source
    tree
  • copy ejb-jar.xml to META-INF
  • then
  • jar cf com\titan\com\travelagent\.class
    META-INF\ejb-jar.xml
  • Use the deployment tool provided by the EJB
    server vendor to configure the environment
  • For Suns J2EE Reference Implementation
  • deploytool generates .ear file which includes
    server-specific descriptor (sun-j2ee-ri.xml) and
    application-specific descriptor
  • Fill in the fields with information pertinent to
    the bean
  • deploy to your EJB server host

30
Client Application
  • uses the bean(s)
  • via the remote and home interfaces
  • never via the bean class
  • steps
  • find the naming service
  • find the bean(s)
  • use home and business methods as desired

31
Client Application Example, Part 1
from Enterprise JavaBeans, R. Monson-Haefel,
OReilly, 2000. package com.titan.travelagent
import com.titan.cabin.CabinHome import
com.titan.cabin.Cabin import com.titan.cabin.Cabi
nPK import javax.naming.InitialContext import
javax.naming.Context import javax.naming.NamingEx
ception import javax.ejb.CreateException import
java.rmi.RemoteException import
java.util.Properties // continued...
32
Client Application Example, Part 2
public class Client_1 public static int
SHIP_ID 1 public static int BED_COUNT
3 public static void main(String args)
try Context jndiContext
getInitialContext() Object obj
jndiContext.lookup("ejb/TravelAgentHome")
TravelAgentHome home (TravelAgentHome)
javax.rmi.PortableRemoteObject.narrow(obj,
TravelAgentHome.class) TravelAgent
reserve home.create() // continued...
33
Client Application Example, Part 3
// Get a list of all cabins on ship 1 with a bed
count of 3. String list
reserve.listCabins(SHIP_ID,BED_COUNT)
for(int i 0 i lt list.length i)
System.out.println(listi)
catch(java.rmi.RemoteExcepti
on re)re.printStackTrace()
catch(Throwable t)t.printStackTrace()
static public Context getInitialContext() throws
Exception Properties p new Properties()
// ... Specify the JNDI properties specific to
the vendor. return new InitialContext()
34
Homework 5
  • Programming assignment 2
  • tasks
  • implement Account beans for savings and checking
    accounts use student id for account number
    prefix
  • implement transferMoney bean
  • implement client to transfer money from one
    account to another
  • deploy (dont create/delete table) run
  • use container-managed persistence
  • use JNDI for databases jndi/SavingsAccountDB and
    jndi/CheckingAccountDB
About PowerShow.com