Title: EPDA EJB: Topic 3
1EPDAEJB Topic 3
- Agenda
- Client and EJB Interaction
- EJB Taxonomy
- Example writing an EJB
2Architectural Overview Recap
- To implement the entity and session enterprise
beans we need to define the component
interfaces, a bean class and a primary key - Remote interface
- Defines beans business methods that can be
accessed from the outside the EJB container. - The remote interface extends javax.ejb.EJBObject,
which in turn extends java.rmi.Remote. - It is used by session and entity beans in
conjunction with the remote home interface
3Architectural Overview Recap
- Remote home interface
- Defines the beans life-cycle methods that can be
accessed from applications outside the EJB
container - i.e. methods for creating new beans, removing
beans, and finding beans. - The home interface extends
- javax.ejb.EJBHome which in turn extends
java.rmi.Remote. - It is used by session and entity beans in
conjunction with the remote interface
4Architectural Overview Recap
- EJB Local interface
- Defines the business methods that can be used by
other beans co-located in the same EJB container - The business methods a bean presents to other
beans in the same address space. - Allows beans to interact without the overhead of
distributed object protocol. - It extends javax.ejb.EjbLocalObject
- It is used by session and entity beans in
conjunction with local home interface.
5Architectural Overview Recap
- Local home interface
- Defines the beans life-cycle methods that can be
used by other beans co-located in the same EJB
container - The life cycle methods a bean presents to other
beans in the same address space. - Allows beans to interact without the overhead of
distributed object protocol. - It extends javax.ejb.EjbLocalHome
- It is used by session and entity beans in
conjunction with local interface.
6Architectural Overview Recap
- Bean class
- The session and entity bean classes actually
implement the beans business and life-cycle
methods. - It must have methods that match the signatures of
the methods defined in the remote and local
interfaces. - An entity bean must implement javax.ejb.EntityBean
- A session bean must implement javax.ejb.SessionBea
n - They both in turn extend javax.ejb.EnterpriseBean
7Architectural Overview Recap
- Primary Key
- A simple class that provides a pointer into the
database. - Only entity beans need primary key.
- This class implements java.io,Serializable
- Note
- The message-driven beans do not use any of the
component interfaces. They are never called from
other applications or beans. - Instead they contain a single method,
onMessage(), which is called by the container
when a new message arrives.
8Client and EJB Interaction
- The client interacts with the EJB in 5 steps
- The container registers the EJB with the naming
service during deployment time. - The naming service in the application server
provides the client with a Naming Context. -
- The naming context can be used by the client to
lookup the EJB name in the naming service. - The naming service then returns the reference to
the home interface -
- The client uses the create method of the home
interface. - A reference to remote interface is returned to
the client. - The client can use the remote reference to invoke
business methods.
9Client and EJB Interaction
Application Server
EJB Container
EJB Home Interface
5
Client
4
EJB Remote
3
1
Namining Service
2
Naming Context
10EJB Taxonomy
Enterprise java Beans
Message-Driven EJBs
Entity EJBs
Session EJBs
Stateful Session EJB
Container-Managed Persistence
Bean-managed persistence
Stateful Session EJB
11Entity EJBs
- They can live as long as data remains in the
database - Entity beans are Persistent
- Offers shared access to multiple users
- Represents an object view of data in the
information database. - Can take two types of persistence
- Container Managed persistence
- Bean-managed persistence.
12Entity EJB Container-managed persistence (CMP)
- Container tools will be used to make database
calls. - Beans are not tightly coupled to database()
- Can code complex relationships ()
- With CMP it is difficult to handle dynamic SQL (-)
13 Entity EJB Bean-managed persistence (BMP)
- The developer needs to write the interface to the
database. - Advantage
- Bean can be deployed in any container without the
container having to make calls to the database. - Shortcomings
- Different data needs rewriting of the bean and
- it can get very complicated!
14Session EJB
- Session beans are relatively short lived
- Do not represent shared data directly in the
database, but can update and access the data - If the container crashes the session EJB will no
longer exist. - Container has to replace it with a new session
object to continue the operation - Session beans can be stateless or stateful.
15Stateless Session EJB
- Do not retain conversational state.
- But they are easy to develop and very efficient.
- Can service many clients with the use of pooling
service. - Connection pooling is a technique used for
sharing server resources among requesting clients
- Given that entity beans are shared components,
for every client request, the database
connections are acquired and released several
times.
16Connection poolingSource javaworld.com
- Acquiring and releasing database connections via
the database manager, using JDBC 1.0, will impact
the performance on the EJB layer.
17Connection Pooling Source javaworld.com
- The JDBC 2.0 Standard Extension API specifies
that a database service provider can implement a
pooling technique that can allow multiple
connection objects from a resource pool to be
shared transparently among the requesting
clients.
18Stateful Session EJB
- Retain conversational state with a single client
- They run on behalf of the client in a 1-to-1
relationship
19Message-Driven EJB
- Similar to a session bean, except it responds to
a JMS message rather than an RMI event. - Stateless and short-lived
- Can update shared data in the database
- If the container crashes the message-driven EJB
will no longer exist. - Container has to replace it with a new
message-driven object to continue the operation
20Example Writing a HelloWorld EJB
- This example involves the following steps to
write the HelloWorld EJB - Write home Interface
- Write remote Interface
- Write beans implementation
- Compile
- Home Interface
- Remote Interface
- Beans implementation class
- Deploy the EJB
- Write the client
- Run the client
21Step1 Building the Home Interface
- The bean will be created here
- The interface must be public and
- Must have a create() method
- The home interface will be
Import java.rmi.RemoteException Import
javax.ejb. public interface HeloWorldHome
extends EJBHOME public HelloWorld create()
throws RemoteException, CreateException
Name of EJB remote interface
22Step2 Building the Remote Interface
- Here we declare all the functionality supported
by the EJB component - Declare every method that is supported by the EJB
- Methods must throw RemoteException
- The remote interface will be
Import java.rmi.RemoteException Import
javax.ejb.EJBObject public interface HeloWorld
extends EJBObject public String getHelloWorld
(String message) throws RemoteException
23Step 3 Building the EJB Implementation
- This EJB class should implement
- All of the methods defined in home interface and
remote interface - Methods that are defined in the SessionBean
interface - Any error will be detected at deployment time
24Step 3 Building the EJB Implementation Continued
- HelloWorld EJB implementation
Import javax.ejb.SessionBean Import
javax.ejb.SessionContext public class
HelloworldEJB implements SessionBean public
string getHelloworld (String message)
return Hello World from EPDA gt message
\n Have fun public HelloWorldEJB()
public void ejbCreate() public void
ejbRemove() public void ejbActivate()
//details to be followed later public void
ejbPassivate() //details to be followed later
public voidsetSessionContext (sessionContext sc)
25Step 4 Compile the Bean
- Compile using the following commands
- Javac HelloWorld.java HelloworldHome.java
HelloWorldEJB.java - Use ant (or asant)
- Strongly recommended
- Use it as your Build tool.
26Step 5 Deploy the Bean
- Deployment is the process of installing an EJB in
an EJB container - The deployment process varies between different
application servers. - But it generally requires the following items
- EJB home interface
- EJB remote interface
- Bean class
- Deployment Descriptors (an XML file)
- A range of features for a given EJB that are
provided by the container will be made available
to the developer. - The EJB compiler uses these four items to
generate JAR files needed for deployment process - Now the EJBs can be deployed
- Can use the command line or
- A deployment tool
27Step 6 Write the Client
- The client implements the following
- Obtains the naming context of the application
server - Context initial new InitialContext ()
- Looks up the EJB name
- HelloWorldHome
- home1 (HelloWorldHome) initial.lookup
(myHelloWorld) - Creates an EJB instance
- HelloWorld hw home1.create ()
- Invoke the required methods
- String msg hw.getHelloWorld ( ..thank God
its Easter Break Soon!)
28Step 6 Write the Client .
- import javax.naming.
- public class Helloworld
-
- public static void main (String args)
-
- try
-
- Context initial new InitialContext ()
- HelloWorldHome home1 (HelloWorldHome)
initial.lookup (myHelloWorld) - HelloWorld hw home1.create ()
- String msg hw.getHelloWorld ( ..thank God
its EasterBreak Soon!) - system.out.println (msg)
-
- catch (Exception ex)
-
- system.err.println (Exception!!)
- ex.printStackTrace()
-
-
29Step 7 Running the Client
- We can execute this client
- java HelloWorldClient
- The result must look like as follows
Hello World from EPDA gt thank God its Easter
Break Soon! Have fun