Ye Zhou - Virginia Tech
Download
Report
Transcript Ye Zhou - Virginia Tech
Enterprise Java Beans
Ye Zhou
CS6704 Presentation
Virginia Tech
Entity Beans
An in-memory Java representation of
persistent data -- a view into a database
Long-lived – as long as data lives
Shared access from multiple users
Transactional
Can survive from the server crash
Entity Bean Model
Entity Bean class
ejbLoad() – load state from underlying DB
ejbStore() – synchronize the state regularly
ejbRemove() – remove object when client calls
ejbActivate() – bean instance out of pool
ejbPassivate() – bean instance disassociated
setEntityContext() – called by container on creation
unsetEntityContext() – called by container before being
removed
Primary Key class
Unique Identifier
Entity Bean usage
BMP & CMP
BMP – Bean Managed Persistence
an entity bean that synchronizes its state with the
database manually
CMP – Container Managed Persistence
EJB container will do it instead
BMP & CMP
Entity bean example
Entity bean example
import javax.ejb.*;
import java.rmi.remoteException;
public interface AccountHome extends EJBHome{
Account create(String accountID, String ownerName) throws
CreateException, AccountException;
public Account findByPrimaryKey(AccountPK key) throws FinderException,
RemoteException;
public Collection findByOwnerName(String name) throws FinderException,
RemoteException;
public double getTotalBankValue() throws RemoteException,
AccountException;
}
Entity bean example
import java.io.Serializable;
public class AccountPK implements java.io.Serializable{
public String accountID;
public AccountPK(String id){
this.accountID = id;
}
public String toString() {
return accountID:
}
public boolean equals(Object account){
return ((AccountPK)account).accountID.equals(accountID);
}
}
Entity bean example
public class AccountBean implements EntityBean{
protected EntityBeanContext ctx;
private String accountID; //primary key
private String ownerName;
private double balance;
//getter & setter on fields
public setXXX(String value)
public String getXXX()
…
Entity bean example
//Business Logic methods
public void Deposit(double amt) throws AccountException {
balance+=amt;
}
public void WithDraw(double amt) throws AccountException {
if(amt > balance) {
throw new AccountException(accountID+“Limit reached”);
}
else balance-=amt;
}
Entity bean example
//EJB required methods
public void ejbLoad() {
AccountPK account = (AccountPK) ctx.getPrimaryKey();
String id = account.toString();
PreparedStatement ps =null; Connection con=null;
try{
con=getConnection();
ps = con.preparedStatement(“select owner, balance from
accounts where id = ?”);
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
rs.next();
ownerName = rs.getString(1);
balance = rs.getDouble(2);
}catch(Exception e){throw new EJBException(“Fail to load from DB”);}
}
Message-Driven Bean
Motivation
Performance:
Reliability:
RMI-IIOP is a blocking protocol
RMI-IIOP can’t survive from the server failure
Multiple senders and receivers:
RMI-IIOP is connection-oriented, which is not possible
for clients to broadcast the events to servers.
Message-Driven Bean
Definition
A special EJB component that can receive JMS
messages.
Message-Driven Bean
Life cycle
Message-Driven Bean
Characteristics
No home interface, local home interface and
remote interface
It has a single, weakly-couple typed business
method “OnMessage”
No return values
Stateless
Do not send Exception back to clients
Subscription can be durable or non-durable
Message-Driven Bean example
import javax.ejb.*;
import javax.jms.*;
public sampleBean implements MessageDrivenBean, MessageListener
{
protected MessageDrivenContext ctx;
public void setMessageDrivenContext(MessageDrivenContext ctx)
{this.ctx=ctx;}
public void ejbCreate()
{System.err.println(“bean initialized”);}
Message-Driven Bean example
public void onMessage(Message msg)
{
if(msg instanceOf TextMessage) {
TextMessage tm=(TextMessage)msg;
try{
String text = tm.getText();
System.err.println(“Text is”+text);
}catch(JMSException e) {
e.printStackTrace(); }
}
}
public void ejbRemove() {
System.err.println(“bean removed”);}
}
Transaction
Features
Types
ACID
Programmatic
Declarative
Client-initiated
Package
Javax.transaction.*
Reference
Ed Roman, “Mastering Enterprise
Javabeans ”, John Wiley & Sons Inc., 2002
Linda DeMichiel, “Enterprise Javabeans
Spec V2.0”, Sun Microsystem, 2001
Gopalan Raj,http://www.execpc.com/~gopalan/java/ejb.html