EJB - Enterprise Java Bean |
||
EJB separates busines logic from transactions, security, scalability and the other technical details. It works with Application Servers and EJB containers, so main benefit is safe bean distribution (scalability). The scalability is costly in performance, so EJB is not adviced if one server is sufficient for your application. - Session : for business process and web services. Stateful or stateless. NOT Persistant. - Entity : for busines entity. Statefull. Persistant. - Message : to listen and process Java Messages. |
||
Session Bean |
||
- For business process (e.g. verifying a credit card) and web services. - At any given time, only one client has access to the bean instance. - Not persistant on DB. - May fetch from a DB read-only data. |
||
Stateless Session Bean |
||
- There is no bean property (=state). - Can support multi clients. - Can be shared among clients but not at the same time. - Does not maintain conversational state (there is no state). - Offer better rerformance than statefull beans. - Can implement web services but others not. |
||
Stateful Session Bean |
||
- Bean properties are states. - Represents a single client. - Cannot be shared among clients. - Maintains conversational state (=bean states in terms of the serialisation protocol). - When client terminates, then it is no more associated to the client. - Manages the workflow of several enterprise beans. |
||
Entity Bean |
||
- For business entity (e.g. credit card). - Typically, each entity bean has an underlying table. - Typically, each instance of the bean corresponds to a row in that table. - Persistant on DB : container managed, bean managed. - Has a primary key. - Has finder methods (at least one for the primary key). - Can participate in relationships with other entity beans. - Allows shared access. Abstract schema (container managed) - Defines the bean's persistent fields and relationships. - Distinguished from the physical schema (DB). - Specify its name in deployement descriptor. - This name is referenced by queries written in EJB QL (Query Language). - Every finder method must be implemented by EJB QL. - Peristent fields are states stored in DB. - The EJB container automatically synchronizes this state with the database. - A relationship field is like a foreign key in a DB. - A relationship field does not represent the bean's state. - The multiplicities: one-to-one, one-to-many, many-to-one, and many-to-many. - In a bidirectional relationship, each entity bean has a relationship field that refers to the other bean. - In a unidirectional relationship, only one entity bean has a relationship field that refers to the other. |
||
Message Bean |
||
- Processes messages asynchronously. - Acts as a JMS message listener. - The client has not access through interfaces. - Has only a bean class. - Does not maintain conversational state (there is no state). - All instances of a message-driven bean are equivalent. - The container can pool these instances to process messages concurrently. - Can invoke a session or entity bean to process or store it in a database. - Can be transaction aware. - Implements onMessage method to process the message. - Implements onMessage method to process the message. - Relatively short-lived. - Avoids tying-up. |
||
ejbApplicationName.ear ├ META-INF │ ├ MANIFEST.MF │ ├ application.xml │ ├ sun-application.xml │ └ sun-j2ee-ri. ├ DukesBankAC.jar ├ DukesBankEJB.jar └ DukesBank.war application.xml : <?xml version="1.0" encoding="UTF-8"?> <application version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"> <description>Application description</description><display-name>DukesBankApp</display-name> <module> <web> <web-uri>DukesBankWAR.war</web-uri> <context-root>/bank</context-root> </web> </module> <module> <java>DukesBankACJAR.jar</java> </module> <module> <ejb>DukesBankEJBJAR.jar</ejb> </module> </application> sun-application.xml : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-application PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.0 J2EE Application 1.4//EN" "http://www.sun.com/software/appserver/dtds/sun-application_1_4-0.dtd"> <sun-application><pass-by-reference>false</pass-by-reference> <security-role-mapping> <role-name>bankAdmin</role-name> <principal-name>bankadmin</principal-name> <group-name>bankAdmin</group-name> </security-role-mapping> <security-role-mapping> <role-name>bankCustomer</role-name> <principal-name>200</principal-name> <group-name>bankCustomer</group-name> </security-role-mapping> </sun-application> |
||
DukesBankEJB.jar ├ META-INF │ ├ MANIFEST.MF │ ├ ejb-jar.xml │ ├ sun-cmp-mappings.xml │ └ sun-ejb-jar.xml. ├ DukesBankAC.jar ├ DukesBankEJB.jar └ DukesBank.war application.xml : <?xml version="1.0" encoding="UTF-8"?> <application version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"> <description>Application description</description><display-name>DukesBankApp</display-name> <module> <web> <web-uri>DukesBankWAR.war</web-uri> <context-root>/bank</context-root> </web> </module> <module> <java>DukesBankACJAR.jar</java> </module> <module> <ejb>DukesBankEJBJAR.jar</ejb> </module> </application> sun-application.xml : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-application PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.0 J2EE Application 1.4//EN" "http://www.sun.com/software/appserver/dtds/sun-application_1_4-0.dtd"> <sun-application><pass-by-reference>false</pass-by-reference> <security-role-mapping> <role-name>bankAdmin</role-name> <principal-name>bankadmin</principal-name> <group-name>bankAdmin</group-name> </security-role-mapping> <security-role-mapping> <role-name>bankCustomer</role-name> <principal-name>200</principal-name> <group-name>bankCustomer</group-name> </security-role-mapping> </sun-application> sun-cmp-mappings.xml : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-cmp-mappings PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 OR Mapping//EN" "http://www.sun.com/software/appserver/dtds/sun-cmp-mapping_1_2.dtd"> <sun-cmp-mappings><sun-cmp-mapping> <schema>dukesbank</schema> <entity-mapping> <ejb-name>AccountBean</ejb-name> <table-name>ACCOUNT</table-name> <cmp-field-mapping> <field-name>accountId</field-name> <column-name>ACCOUNT.ACCOUNT_ID</column-name> <fetched-with> <default/> </fetched-with> </cmp-field-mapping> <cmp-field-mapping> <field-name>balance</field-name> <column-name>ACCOUNT.BALANCE</column-name> <fetched-with> <default/> </fetched-with> </cmp-field-mapping> <cmr-field-mapping> <cmr-field-name>customers</cmr-field-name> <column-pair> <column-name>ACCOUNT.ACCOUNT_ID</column-name> <column-name>CUSTOMER_ACCOUNT_XREF.ACCOUNT_ID</column-name> </column-pair> <column-pair> <column-name>CUSTOMER_ACCOUNT_XREF.CUSTOMER_ID</column-name> <column-name>CUSTOMER.CUSTOMER_ID</column-name> </column-pair> <fetched-with> <none/> </fetched-with> </cmr-field-mapping> </entity-mapping> <entity-mapping> <ejb-name>CustomerBean</ejb-name> <table-name>CUSTOMER</table-name> <cmp-field-mapping> <field-name>city</field-name> <column-name>CUSTOMER.CITY</column-name> <fetched-with> <default/> </fetched-with> </cmp-field-mapping> <cmp-field-mapping> <field-name>customerId</field-name> <column-name>CUSTOMER.CUSTOMER_ID</column-name> <fetched-with> <default/> </fetched-with> </cmp-field-mapping> <cmr-field-mapping> <cmr-field-name>accounts</cmr-field-name> <column-pair> <column-name>CUSTOMER.CUSTOMER_ID</column-name> <column-name>CUSTOMER_ACCOUNT_XREF.CUSTOMER_ID</column-name> </column-pair> <column-pair> <column-name>CUSTOMER_ACCOUNT_XREF.ACCOUNT_ID</column-name> <column-name>ACCOUNT.ACCOUNT_ID</column-name> </column-pair> <fetched-with> <none/> </fetched-with> </cmr-field-mapping> </entity-mapping> </sun-cmp-mapping> </sun-cmp-mappings> sun-ejb-jar.xml : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 EJB 2.1//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-1.dtd"> <sun-ejb-jar> public abstract class AccountBean implements EntityBean { private EntityContext context; // Access methods for persistent fields public abstract String getAccountId(); public abstract void setAccountId(String accountId); public abstract BigDecimal getBalance(); public abstract void setBalance(BigDecimal balance); // Access methods for relationship fields public abstract Collection getCustomers(); public abstract void setCustomers(Collection customers); // business methods public void addCustomer(LocalCustomer customer) { Debug.print("AccountBean addCustomer"); try { Collection customers = getCustomers(); customers.add(customer); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } } public void removeCustomer(LocalCustomer customer) { Debug.print("AccountBean removeCustomer"); try { Collection customers = getCustomers(); customers.remove(customer); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } } // ejb methods public String ejbCreate(String accountId, String type, String description, BigDecimal balance, BigDecimal creditLine, BigDecimal beginBalance, java.util.Date beginBalanceTimeStamp) throws CreateException { Debug.print("AccountBean ejbCreate"); setAccountId(accountId); setType(type); setDescription(description); setBalance(balance); setCreditLine(creditLine); setBeginBalance(beginBalance); setBeginBalanceTimeStamp(beginBalanceTimeStamp); return null; } public void ejbRemove() { Debug.print("AccountBean ejbRemove"); } public void setEntityContext(EntityContext context) { Debug.print("AccountBean setEntityContext"); context = context; } public void unsetEntityContext() { Debug.print("AccountBean unsetEntityContext"); context = null; } public void ejbLoad() { Debug.print("AccountBean ejbLoad"); } public void ejbStore() { Debug.print("AccountBean ejbStore"); } public void ejbActivate() { Debug.print("AccountBean ejbActivate"); } public void ejbPassivate() { Debug.print("AccountBean ejbPassivate"); } public void ejbPostCreate(String accountId, String type, String description, BigDecimal balance, BigDecimal creditLine, BigDecimal beginBalance, java.util.Date beginBalanceTimeStamp) { } } |