1 / 34

Enterprise JavaBeans™

Enterprise JavaBeans™. Trademark of Sun Microsystems, Inc. Agenda. Review Interfaces Deployment Descriptors Using Beans Homework 5. Review. need development and runtime environment J2EE SDK development environment from tool vendor like Together, Inprise

jihan
Download Presentation

Enterprise JavaBeans™

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 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; public 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) throws RemoteException, CreateException; public findByPrimaryKey(AccountPK pk) throws RemoteException, FinderException; }

  10. Generic Enterprise Bean Class public class xxxBean implements javax.ejb.zzzBean { // … >>> business methods here <<<… public … ejbCreate(…) {…} public void ejbRemove() {} // context setting public void setzzzContext(javax.ejb.zzzContext ctx) {} // etc. }

  11. EntityBean Class public class xxxBean implements javax.ejb.EntityBean { // …>>> public instance variables from persisted storage <<<… // … >>> business methods here <<<… 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.EntityContext 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 count=0; public String id; public String name; public String ssNumber; public MoneyAmount balance; public void debit(BigDecimal amount) throws RemoteException, InsufficientFundsException { if (balance.compareTo(amount) > 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.EntityContext ctx) {} public void unsetEntityContext() {} public void ejbLoad() {} public void ejbStore() {} }

  16. SessionBean Class public class xxxBean implements javax.ejb.SessionBean { // … >>> business methods here <<<… public ... ejbCreate(...) {…; return null;} public void ejbActivate() {} public void ejbPassivate() {} public void ejbRemove() {} public void setSessionContext(javax.ejb.SessionContext ctx) {} }

  17. SessionBean Example, Part 0 from: Enterprise JavaBeans, R. Monson-Haefel, O’Reilly, 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, O’Reilly, 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.narrow( (Object) jc.lookup(“CabinHome”), CabinHome.class); Vector list = new Vector(); CabinPK pk = new CabinPK(); Cabin cabin; // continued...

  19. SessionBean Example, Part 2 for (int i=0; ; 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 String[list.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.SessionContext ctx) {} }

  21. MessageDrivenBean Class public class xxxBean implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener { public void ejbCreate(...) {…} public void ejbRemove() {} public void onMessage(javax.jms.Message message) {…} public void setMessageDrivenContext( javax.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, O’Reilly, 2000. <ejb-jar> <enterprise-beans> <entity> <description>This Cabin enterprise bean entity represents a cabin on a cruise ship.</description> <ejb-name>CabinBean</ejb-name> <home>com.titan.cabin.CabinHome</home> <remote>com.titan.cabin.Cabin</remote> <ejb-class>com.titan.cabin.CabinBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>com.titan.cabin.CabinPK</prim-key-class> <reentrant>False</reentrant> <cmp-field><field-name>id</field-name></cmp-field> <cmp-field><field-name>name</field-name></cmp-field> <cmp-field><field-name>deckLevel</field-name></cmp-field> <cmp-field><field-name>ship</field-name></cmp-field> <cmp-field><field-name>bedCount</field-name></cmp-field> </entity> </enterprise-beans>

  24. Deployment Descriptors:EntityBean Example, Part 2 <assembly-descriptor> <security-role> <description> This role represents everyone who is allowed full access to the cabin bean. </description> <role-name>everyone</role-name> </security-role> <method-permission> <role-name>everyone</role-name> <method> <ejb-name>CabinBean</ejb-name> <method-name>*</method-name> </method> </method-permission>

  25. Deployment Descriptors:EntityBean Example, Part 3 <container-transaction> <method> <ejb-name>CabinBean</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>

  26. Deployment Descriptors:SessionBean Example, Part 1 from: Enterprise JavaBeans, R. Monson-Haefel, O’Reilly, 2000. <ejb-jar> <enterprise-beans> <session> <ejb-name>TravelAgentBean</ejb-name> <home>com.titan.travelagent.TravelAgentHome</home> <remote>com.titan.travelagent.TravelAgent</remote> <ejb-class>com.titan.travelagent.TravelAgentBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> <ejb-ref> <ejb-ref-name>ejb/CabinHome</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <home>com.titan.cabin.CabinHome</home> <remote>com.titan.cabin.Cabin</remote> </ejb-ref> </session> </enterprise-beans>

  27. Deployment Descriptors:SessionBean Example, Part 2 <assembly-descriptor> <security-role> <description> This role represents everyone who is allowed full access to the cabin bean. </description> <role-name>everyone</role-name> </security-role> <method-permission> <role-name>everyone</role-name> <method> <ejb-name>TravelAgentBean</ejb-name> <method-name>*</method-name> </method> </method-permission>

  28. Deployment Descriptors:SessionBean Example, Part 3 <container-transaction> <method> <ejb-name>TravelAgentBean</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>

  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 Sun’s 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, O’Reilly, 2000. package com.titan.travelagent; import com.titan.cabin.CabinHome; import com.titan.cabin.Cabin; import com.titan.cabin.CabinPK; import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; 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 < list.length; i++){ System.out.println(list[i]); } } catch(java.rmi.RemoteException 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 (don’t create/delete table) & run • use container-managed persistence • use JNDI for databases: jndi/SavingsAccountDB and jndi/CheckingAccountDB

More Related