+ All Categories
Home > Technology > Session 6 Tp6

Session 6 Tp6

Date post: 18-Dec-2014
Category:
Upload: phanleson
View: 720 times
Download: 1 times
Share this document with a friend
Description:
 
23
ACCP2005 /EJB 2.0 /Session 6 /1 of 23 Bean-Managed Persistent Entity Bean Session 6
Transcript
Page 1: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /1 of 23

Bean-Managed Persistent Entity Bean

Session 6

Page 2: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /2 of 23

Session Objectives

Define a Bean-Managed Persistent Entity Bean (BMP EJB)

Describe the implementation guidelines needed to use BMP EJBs

Describe deployment descriptors for BMP EJBs

Page 3: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /3 of 23

Review (1)

There are three main ways to make objects persistent: Java Object Serialization Object-Relational Mapping Object Database Management System

Object serialization is a method by which the current state of Java objects can be captured and saved permanently.

There are two ways of mapping the objects to the relational data:

Manual mapping: This is done using a database access API, which can be JDBC or SQL/J.

Use an object-relational mapping product. It can be Sun’s JavaBlend or Object people’s TOP link.

Two kinds of components are deployed in a multi-tier deployment:

Application logic components Persistent data components

Page 4: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /4 of 23

Review (2)

The entity bean comprises the following files: The entity bean class The remote interface The Local Interface The home interface The local home interface The primary key class The deployment descriptors

ejbActivate(): When a bean has to be transitioned out of an instance pool, the ejbActivate() callback method is used.

ejbPassivate(): When the bean is being sent into the instance pool, this method is called.

Enterprise beans have a context object that identifies the bean’s environment information such as transaction and security information

Page 5: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /5 of 23

IntroductionImplementing BMP EJB’s

All the BMP EJBs have to implement the javax.ejb.EntityBean interface

The callback methods invoked on the bean by the container are defined by this interface.

Bean‑managed Persistent Beans require the developer to provide data access logic.

The developer has to provide the implementation to map the bean instances from and to storage.

Page 6: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /6 of 23

Guidelines for Implementation of BMP EJBs

(1)

Method Explanation Implementation

setEntityContext() This method is called when the container has to increase the pool size by instantiating a new entity bean.

This is used to access the context and to acquire the environment information. The context has to be in a member variable.

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

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.

Page 7: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /7 of 23

Guidelines for Implementation of BMP EJBs

(2)

Method Explanation Implementation

ejbCreate(<…>) This method is responsible for creating new data in the database. It also initializes the bean.

The parameters have to be initialized and this initialization has to be valid. Database representations have to be created explicitly.

ejbPostCreate(<…>)

ejbPostCreate()

Every ejbCreate() is followed by an ejbPostCreate().

Once the bean instance is associated with the EJB Object, the ejbPostCreate() is called.

Page 8: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /8 of 23

Guidelines for Implementation of BMP EJBs

(3)

Method Explanation Implementation

ejbActivate() When the container has to take a bean from the pool and then change it to a ready state, the ejbActivate() method is called.

For this method, socket connections are important. This is because the bean has to service particular clients.

ejbLoad() This method is called to load data into the database.

The getPrimaryKey() method has to be called. This will tell the bean what data has to be loaded.

ejbStore() This method is called to update the database data with the new values from the in‑memory fields.

The data in the database is explicitly updated through a storage API.

Page 9: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /9 of 23

Guidelines for Implementation of BMP EJBs

(4)

Method Explanation Implementation

ejbPassivate() The container calls this method to return the bean to the pool.

All resources have to be released. Resources could be socket connections allocated during ejbActivate().

ejbRemove() This will destroy the data in the database.

The getPrimaryKey() is used to destroy the data from the database.

unsetEntityContext()

It is called just before the entity bean instance is destroyed. This method dissociates the bean from its environment.

All resources allocated during the setEntityContext() have to be released.

Page 10: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /10 of 23

An Object Model Example of BMP

java.rmi.Remote

java.ejb.EJBLocalObject

javax.ejb.EJBLocalHome

javax.ejb.EJBHomejava.ejb.EJBObject

Local Home InterfaceHome InterfaceRemote Interface Local Interface

Home ObjectEJB ObjectEJB Local ObjectLocal Home Object

Supplied by bean provider

Comes with EJB distribution

Comes with Java2 Platform

Page 11: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /11 of 23

Remote Interface (1)

java.io.serializable

javax.ejb.EntityBean

Bean Abstract ClassPrimary Key

Class

javax.ejb.EnterpriseBean

Comes with Java2 Platform

Comes with EJB distribution

Supplied by bean provider

Page 12: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /12 of 23

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

All Remote Interfaces extend the javax.ejb.EJBObject

Define the business methods of the bean i.e. the getter/setter methods.

All methods throw the RemoteException.

Page 13: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /13 of 23

Local Interface

The local interface is used more frequently so that the overhead involved in remote calls is reduced significantly

Contains the business methods of the bean.

Extends javax.ejb.EJBLocalObject.

Page 14: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /14 of 23

Home Interface

Extends javax.ejb.EJBHome class.

Contains create methods, finder methods and remove methods.

Throws RemoteException and CreateException

Used by clients to call methods on the bean.

Page 15: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /15 of 23

Local Home Interface

Similar to the home interface in the respect of usage by the local clients.

Does not throw remote exceptions

Extends EJBLocalHome class which generates all local objects.

Contains the create and finder methods; must contain a single findByPrimaryKey() method.

Page 16: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /16 of 23

The Bean Class

Implements the EntityBean class

Contains corresponding ejbCreate(), ejbFind(), ejbActivate(), ejbLoad() methods etc.

The Bean Class consists of three parts: Bean-managed State Fields: persistent fields, which load and store data in the database.

Business Logic Methods: methods that serve the client

EJB-required methods: methods used by the container to manage the bean

Page 17: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /17 of 23

The Client Class

Performs a JNDI lookup to get the Initial Context and to locate the Home Interface of the bean

After locating the Home interface of the bean the client instantiates the bean. Calls the create, finder and business methods of the bean.

Destroys the instance of the bean using the remove method.

Page 18: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /18 of 23

The Deployment Descriptor

A lot of information about runtime management of beans is not described by the interfaces and classes.

This information deals with beans security, transactions naming and other services common to the distributed object environments.

The deployment descriptors used for the bean-managed persistent entity bean are: The ejb-jar.xml descriptor The jaws.xml descriptor The JBoss.xml descriptor

Page 19: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /19 of 23

ejb-jar.xml descriptor

The ejb-jar.xml file lists details of the classes and interfaces of the bean, and the type and name of the primary key

The various declarations in an XML document are: <!DOCTYPE> <ejb-jar> <enterprise-beans> <entity> <ejb-name> <home> <remote> EJB 2.0: <local-home> EJB 2.0: <local> <ejb-class> <prim-key-class> <persistence-type>, <reentrant>

Page 20: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /20 of 23

jaws.xml descriptor

The JAWS file is the object-relational mapping utility used by JBoss.

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.

It also defines the name and type of the persistent fields of the bean.

Contains tags used for specifying the type of data-source and the type of mapping for the tables to be created.

Contains tags for the definition of the data types of the table cells.

(specified by the sql-type and the jdbc-type tags).

Page 21: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /21 of 23

JBoss.xml descriptor

Is the Object-Relational mapping Utility used by the JBoss server.

Provides information about the JNDI mapping names, persistence information, database mapping and advanced container information.

When bean is deployed JAWS will look for this file and then configure the names of the database tables.

<jboss> <enterprise-beans>

<entity>

<ejb-name>StudentBean</ejb-name>

<jndi-name>StudentBean</jndi-name>

</entity> </enterprise-beans></jboss>

Page 22: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /22 of 23

Creating a jar file and Running the Client

Create the jar file using the relevant command in the command prompt

Copy the .jar file into the deploy directory on the server

Run the client in the command prompt after starting the server

Page 23: Session 6 Tp6

ACCP2005 /EJB 2.0 /Session 6 /23 of 23

Summary

All the BMPs have to implement the javax.ejb.EntityBean interface.

The setEntityContext() method is called when the container has to increase the pool size. It is accomplished by instantiating a new entity bean.

The unsetEntityContext() method dissociates the bean from its environment. It is called just before the instance of the entity bean is destroyed.

The bean-managed state fields are persistent fields, which load and store data in the database.

The ejb-jar.xml file and the jaws.xml files are packaged into a directory called META-INF.

The bean instance can service any number of finder methods while in the pool.


Recommended