Download Enterprise JavaBeans (EJB) - A Comprehensive Guide - Prof. William Pugh and more Study notes Programming Languages in PDF only on Docsity!
1
EJB
William Pugh 2
Recommended books
3
What are EJB?
- Some RMI
- Some software component technology/terminology
- Ability to intercept calls and add system- level functionality
- Functionality that can be added
- transactions, security, activation/passivation
- … 4
Kinds of beans
- Entity beans
- represents thing in persistent store
- Message-driven beans
- Session beans
Entity Beans
- Represents a thing in the persistent store
- Cached in memory
- not all persistent entities are materialized as entity beans
- You can search for entity beans
- Deleting a bean deletes the entity from the persistent store 6
Message-driven
- Clients can’t get references to message driven beans
- Attached to messaging service
- Listen for certain kinds of messages
- Provides asynchronous message invocation
7
Stateless Session Beans
- Handles one request from a client
- Then handles another request from a different client
- Client can’t come back to the same stateless session bean
- Typically, represent verbs
- verify credit card
- perform purchase 8
Stateful Session Beans
- A client gets a persistent reference to a stateful session bean
- Can retain state across several method calls
- Can’t search for a stateful session bean
- Not durable: after timeout, or machine crash, they just go away 9
Bean clients
- Clients can only get (indirect) references to session and entity beans
- Can create messages that are processed by message driven beans 10
Home sweet home
- Beans have a home interface
- Home interface allows beans to be created, located and removed
- Factory design pattern
- I would have preferred factory to home as a naming convention
- But we are stuck with home
- User specifies interface only, no code 11 Client VM
EJB Architecture
Server VM Bean Bean EJBObject EJBObject Home Com pon ent Int erfa EJBObject ce stub EJBObject stub Home stub Com pon ent Inte rfac e Ho me Inte rfac e Interception happens here happens here^ Interception 12
Assumptions
- Initially, we’ll assume that all calls to EJB’s are from remote machines - at least, via RMI - assumption relaxed in EJB 2.0, we’ll talk about it later
- Clients never get to talk directly to a bean
- would allow circumvention of security and transaction checks
19
Hello world as an EJB
- Stateless Session Bean that gives Advice 20
Component Interface
import javax.ejb.*; import java.rmi.RemoteException; public interface Advice extends EJBObject { public String getMessage() throws RemoteException; } 21
Home Interface
import javax.ejb.*; import java.rmi.RemoteException; public interface AdviceHome extends EJBHome { public Advice create() throws CreateException, RemoteException; } 22
Bean Implementation
public class AdviceBean implements SessionBean { private String[] adviceStrings = {"test", "test1", "test2", "test3"}; public String getMessage() { System.out.println("in get advice"); int random = (int) (Math.random() * adviceStrings.length); return adviceStrings[random]; } public void ejbCreate() { System.out.println("in ejb create"); } 23
XML deployment descriptor
Ejb1 AdviceBean headfirst.AdviceHome headfirst.Advice headfirst.AdviceBean Stateless Bean 24
EJBObject interface
EJBHome getEJBHome() Handle getHandle() Object getPrimaryKey() boolean isIdentical(EJBObject obj) void remove()
25
EJBHome interface
EJBMetaData getEJBMetaData() HomeHandle getHomeHandle() void remove(Handle handle) void remove(Object primaryKey) 26
Handles
- Handles are persistent references to EJBObjects
- Can be serialized, passed between machines - stored in Servlet sessions 27
isIdentical
- Used to determine whether two EJBObject stubs refer to the same bean
- Stateless session beans
- true if they come from the same home
- Stateful session beans
- false for any two distinct stubs
- Entity beans
- true if entities have same primary key 28
Session/Entity Bean Creation
- Home interface must have create(…) methods
- EJB must have matching ejbCreate(…) interfaces
- Stateless session beans should have only no-argument create methods 29
Removal
- Removing an entity bean deletes the corresponding info from persistent store
- Removing a stateful session bean says that you are done with it - removing a stateless session bean is a no-op (or an error?) 30
Stateful Session Bean Lifecycle
garbage method ready passivated create method in home invoked bean removed or times out bean inactive call to bean timeout exception thrown business method nowhere
37
Transactions
- You can use container managed transactions or bean managed transactions - container managed transactions are specificied declaratively - bmt are done by starting and ending transactions