1 / 20

Enterprise Java Beans

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.

Download Presentation

Enterprise Java Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech

  2. 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

  3. 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

  4. Entity Bean usage

  5. 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

  6. BMP & CMP

  7. Entity bean example

  8. 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; }

  9. 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); } }

  10. 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() …

  11. 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; }

  12. 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”);} }

  13. 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.

  14. Message-Driven Bean • Definition • A special EJB component that can receive JMS messages.

  15. Message-Driven Bean • Life cycle

  16. 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

  17. 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”);}

  18. 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”);} }

  19. Transaction • Features • ACID • Types • Programmatic • Declarative • Client-initiated • Package • Javax.transaction.*

  20. 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

More Related