enterprise java beans ye zhou cs6704 presentation virginia tech
DESCRIPTION
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 IdentifierTRANSCRIPT
![Page 1: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/1.jpg)
Enterprise Java BeansYe Zhou
CS6704 PresentationVirginia Tech
![Page 2: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/2.jpg)
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
![Page 3: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/3.jpg)
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
![Page 4: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/4.jpg)
Entity Bean usage
![Page 5: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/5.jpg)
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
![Page 6: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/6.jpg)
BMP & CMP
![Page 7: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/7.jpg)
Entity bean example
![Page 8: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/8.jpg)
Entity bean exampleimport 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;
}
![Page 9: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/9.jpg)
Entity bean exampleimport 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);}
}
![Page 10: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/10.jpg)
Entity bean examplepublic class AccountBean implements EntityBean{
protected EntityBeanContext ctx;
private String accountID; //primary keyprivate String ownerName;private double balance;
//getter & setter on fieldspublic setXXX(String value)public String getXXX()…
![Page 11: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/11.jpg)
Entity bean example//Business Logic methodspublic 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;
}
![Page 12: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/12.jpg)
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”);}}
![Page 13: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/13.jpg)
Message-Driven Bean Motivation
Performance: RMI-IIOP is a blocking protocol
Reliability: 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.
![Page 14: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/14.jpg)
Message-Driven Bean Definition
A special EJB component that can receive JMS messages.
![Page 15: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/15.jpg)
Message-Driven Bean Life cycle
![Page 16: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/16.jpg)
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
![Page 17: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/17.jpg)
Message-Driven Bean exampleimport 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”);}
![Page 18: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/18.jpg)
Message-Driven Bean examplepublic 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”);}
}
![Page 19: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/19.jpg)
Transaction Features
ACID Types
Programmatic Declarative Client-initiated
Package Javax.transaction.*
![Page 20: Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech](https://reader035.vdocuments.mx/reader035/viewer/2022062401/5a4d1b4f7f8b9ab0599a6f3f/html5/thumbnails/20.jpg)
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