Session 6 Tp6

878 views
804 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
878
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
71
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Session 6 Tp6

  1. 1. Bean-Managed Persistent Entity Bean Session 6
  2. 2. Session Objectives <ul><li>Define a Bean-Managed Persistent Entity Bean (BMP EJB) </li></ul><ul><li>Describe the implementation guidelines needed to use BMP EJBs </li></ul><ul><li>Describe deployment descriptors for BMP EJBs </li></ul>
  3. 3. Review (1) <ul><li>There are three main ways to make objects persistent: </li></ul><ul><ul><li>Java Object Serialization </li></ul></ul><ul><ul><li>Object-Relational Mapping </li></ul></ul><ul><ul><li>Object Database Management System </li></ul></ul><ul><li>Object serialization is a method by which the current state of Java objects can be captured and saved permanently. </li></ul><ul><li>There are two ways of mapping the objects to the relational data: </li></ul><ul><ul><li>Manual mapping: This is done using a database access API, which can be JDBC or SQL/J. </li></ul></ul><ul><ul><li>Use an object-relational mapping product. It can be Sun’s JavaBlend or Object people’s TOP link. </li></ul></ul><ul><li>Two kinds of components are deployed in a multi-tier deployment: </li></ul><ul><ul><li>Application logic components </li></ul></ul><ul><ul><li>Persistent data components </li></ul></ul>
  4. 4. Review (2) <ul><li>The entity bean comprises the following files: </li></ul><ul><ul><li>The entity bean class </li></ul></ul><ul><ul><li>The remote interface </li></ul></ul><ul><ul><li>The Local Interface </li></ul></ul><ul><ul><li>The home interface </li></ul></ul><ul><ul><li>The local home interface </li></ul></ul><ul><ul><li>The primary key class </li></ul></ul><ul><ul><li>The deployment descriptors </li></ul></ul><ul><li>ejbActivate(): When a bean has to be transitioned out of an instance pool, the ejbActivate() callback method is used. </li></ul><ul><li>ejbPassivate(): When the bean is being sent into the instance pool, this method is called. </li></ul><ul><li>Enterprise beans have a context object that identifies the bean’s environment information such as transaction and security information </li></ul>
  5. 5. Introduction Implementing BMP EJB’s <ul><li>All the BMP EJBs have to implement the javax.ejb.EntityBean interface </li></ul><ul><li>The callback methods invoked on the bean by the container are defined by this interface. </li></ul><ul><li>Bean‑managed Persistent Beans require the developer to provide data access logic. </li></ul><ul><li>The developer has to provide the implementation to map the bean instances from and to storage. </li></ul>
  6. 6. Guidelines for Implementation of BMP EJBs (1) Once data is found, the primary keys for that data are sent back to the container. The container will then create EJB objects and associate bean instances with the objects. These methods (also known as Finder methods) will locate the existing entity bean data instances from within the persistent storage. For instance ejbFindByPrimaryKey() , ejbFindAll() and others. ejbFind<…>(<…>) This is used to access the context and to acquire the environment information. The context has to be in a member variable. This method is called when the container has to increase the pool size by instantiating a new entity bean. setEntityContext() Implementation Explanation Method
  7. 7. Guidelines for Implementation of BMP EJBs (2) Once the bean instance is associated with the EJB Object, the ejbPostCreate() is called. Every ejbCreate() is followed by an ejbPostCreate() . ejbPostCreate(<…>) ejbPostCreate() The parameters have to be initialized and this initialization has to be valid. Database representations have to be created explicitly. This method is responsible for creating new data in the database. It also initializes the bean. ejbCreate(<…>) Implementation Explanation Method
  8. 8. Guidelines for Implementation of BMP EJBs (3) The data in the database is explicitly updated through a storage API. This method is called to update the database data with the new values from the in‑memory fields. ejbStore() The getPrimaryKey() method has to be called. This will tell the bean what data has to be loaded. This method is called to load data into the database. ejbLoad() For this method, socket connections are important. This is because the bean has to service particular clients. When the container has to take a bean from the pool and then change it to a ready state, the ejbActivate() method is called. ejbActivate() Implementation Explanation Method
  9. 9. Guidelines for Implementation of BMP EJBs (4) All resources have to be released. Resources could be socket connections allocated during ejbActivate() . The container calls this method to return the bean to the pool. ejbPassivate() All resources allocated during the setEntityContext() have to be released. It is called just before the entity bean instance is destroyed. This method dissociates the bean from its environment. unsetEntityContext() The getPrimaryKey() is used to destroy the data from the database. This will destroy the data in the database. ejbRemove() Implementation Explanation Method
  10. 10. An Object Model Example of BMP java.rmi.Remote java.ejb.EJBLocalObject javax.ejb.EJBLocalHome javax.ejb.EJBHome java.ejb.EJBObject Local Home Interface Home Interface Remote Interface Local Interface Home Object EJB Object EJB Local Object Local Home Object Supplied by bean provider Comes with EJB distribution Comes with Java2 Platform
  11. 11. Remote Interface (1) java.io.serializable javax.ejb.EntityBean Bean Abstract Class Primary Key Class javax.ejb.EnterpriseBean Comes with Java2 Platform Comes with EJB distribution Supplied by bean provider
  12. 12. Remote Interface (2) public interface Student extends EJBObject { public String getName()throws RemoteException; public String getId()throws RemoteException; public void setName(String name) throws RemoteException; } <ul><li>All Remote Interfaces extend the javax.ejb.EJBObject </li></ul><ul><li>Define the business methods of the bean i.e. the getter/setter methods. </li></ul><ul><li>All methods throw the RemoteException. </li></ul>
  13. 13. Local Interface <ul><li>The local interface is used more frequently so that the overhead involved in remote calls is reduced significantly </li></ul><ul><li>Contains the business methods of the bean. </li></ul><ul><li>Extends javax.ejb.EJBLocalObject. </li></ul>
  14. 14. Home Interface <ul><li>Extends javax.ejb.EJBHome class . </li></ul><ul><li>Contains create methods, finder methods and remove methods. </li></ul><ul><li>Throws RemoteException and CreateException </li></ul><ul><li>Used by clients to call methods on the bean. </li></ul>
  15. 15. Local Home Interface <ul><li>Similar to the home interface in the respect of usage by the local clients. </li></ul><ul><li>Does not throw remote exceptions </li></ul><ul><li>Extends EJBLocalHome class which generates all local objects. </li></ul><ul><li>Contains the create and finder methods; must contain a single findByPrimaryKey() method. </li></ul>
  16. 16. The Bean Class <ul><li>Implements the EntityBean class </li></ul><ul><li>Contains corresponding ejbCreate(), ejbFind(), ejbActivate(), ejbLoad() methods etc. </li></ul><ul><li>The Bean Class consists of three parts: </li></ul><ul><ul><li>Bean-managed State Fields : persistent fields, which load and store data in the database. </li></ul></ul><ul><ul><li>Business Logic Methods: methods that serve the client </li></ul></ul><ul><ul><li>EJB-required methods: methods used by the container to manage the bean </li></ul></ul>
  17. 17. The Client Class <ul><li>Performs a JNDI lookup to get the Initial Context and to locate the Home Interface of the bean </li></ul><ul><li>After locating the Home interface of the bean the client instantiates the bean. </li></ul><ul><li>Calls the create, finder and business methods of the bean. </li></ul><ul><li>Destroys the instance of the bean using the remove method. </li></ul>
  18. 18. The Deployment Descriptor <ul><li>A lot of information about runtime management of beans is not described by the interfaces and classes. </li></ul><ul><li>This information deals with beans security, transactions naming and other services common to the distributed object environments. </li></ul><ul><li>The deployment descriptors used for the bean-managed persistent entity bean are: </li></ul><ul><ul><li>The ejb-jar.xml descriptor </li></ul></ul><ul><ul><li>The jaws.xml descriptor </li></ul></ul><ul><ul><li>The JBoss.xml descriptor </li></ul></ul>
  19. 19. ejb-jar.xml descriptor <ul><li>The ejb-jar.xml file lists details of the classes and interfaces of the bean, and the type and name of the primary key </li></ul><ul><li>The various declarations in an XML document are: </li></ul><ul><ul><li><!DOCTYPE> </li></ul></ul><ul><ul><li><ejb-jar> </li></ul></ul><ul><ul><li><enterprise-beans> </li></ul></ul><ul><ul><li><entity> </li></ul></ul><ul><ul><li><ejb-name> </li></ul></ul><ul><ul><li><home> </li></ul></ul><ul><ul><li><remote> </li></ul></ul><ul><ul><li>EJB 2.0: <local-home> </li></ul></ul><ul><ul><li>EJB 2.0: <local> </li></ul></ul><ul><ul><li><ejb-class> </li></ul></ul><ul><ul><li><prim-key-class> </li></ul></ul><ul><ul><li><persistence-type>, <reentrant> </li></ul></ul>
  20. 20. jaws.xml descriptor <ul><li>The JAWS file is the object-relational mapping utility used by JBoss. </li></ul><ul><li>When the bean is deployed, JAWS will look for the jaws.xml file, and on locating it, will read it, and configure the names of the database tables. </li></ul><ul><li>It also defines the name and type of the persistent fields of the bean. </li></ul><ul><li>Contains tags used for specifying the type of data-source and the type of mapping for the tables to be created. </li></ul><ul><li>Contains tags for the definition of the data types of the table cells. </li></ul><ul><li>(specified by the sql-type and the jdbc-type tags). </li></ul>
  21. 21. JBoss.xml descriptor <ul><li>Is the Object-Relational mapping Utility used by the JBoss server. </li></ul><ul><li>Provides information about the JNDI mapping names, persistence information, database mapping and advanced container information. </li></ul><ul><li>When bean is deployed JAWS will look for this file and then configure the names of the database tables. </li></ul><jboss> <enterprise-beans> <entity> <ejb-name>StudentBean</ejb-name> <jndi-name>StudentBean</jndi-name> </entity> </enterprise-beans> </jboss>
  22. 22. Creating a jar file and Running the Client <ul><li>Create the jar file using the relevant command in the command prompt </li></ul><ul><li>Copy the .jar file into the deploy directory on the server </li></ul><ul><li>Run the client in the command prompt after starting the server </li></ul>
  23. 23. Summary <ul><li>All the BMPs have to implement the javax.ejb.EntityBean interface. </li></ul><ul><li>The setEntityContext() method is called when the container has to increase the pool size. It is accomplished by instantiating a new entity bean. </li></ul><ul><li>The unsetEntityContext() method dissociates the bean from its environment. It is called just before the instance of the entity bean is destroyed. </li></ul><ul><li>The bean-managed state fields are persistent fields, which load and store data in the database. </li></ul><ul><li>The ejb-jar.xml file and the jaws.xml files are packaged into a directory called META-INF. </li></ul><ul><li>The bean instance can service any number of finder methods while in the pool. </li></ul>

×