Transcript home object
Middleware Technology (J2EE/EJB)
EJB Fundamentals
EJB Fundamentals
• Enterprise Bean
A server-side software component which can be deployed in a distributed multitier environment.
EJB specification requires that your beans expose a few required methods that
allow the EJB container to manage beans uniformly, regardless of which
container your beans is running in.
2
Types of Enterprise Bean
• Session Bean
business process, actions such as adding numbers、calling other enterprise
beans (CMP、BMP).
Stateless
Stateful
ejbActivate ejbPassivate
• Entity Bean
business data,session beans harness entity beans to achieve business goals.
BMP (Bean Management Persistence) ejbLoad ejbStore
CMP (Container Management Persistence)
• Message-Driven Bean
Asynchronous communication, JMS
3
Table 2.1 Session Beans Calling Entity Beans
Session Beans
Entity Beans
Bank teller
Bank account
Credit card authorizer
Credit card
DNA sequencer
DNA strand
Order entry system
Order, Line item
Catalog engine
Product
Auction broker
Bid, Item
Purchase order approval router
Purchase order
4
Figure 2.1 EJB Clients
5
Distributed Objects
• EJB components are based on distributed objects. A distributed object can be
called from an in-process client, an out-of-process client, or a client located
elsewhere on the network.
• Three different technologies
OMG’s CORBA (Common Object Request Broker Architecture)
Microsoft’s DCOM
Sun’s Java RMI-IIOP
6
Figure 2.2 Distributed Object
7
Client
Server
network
Stub
Skeleton
Servant
Object
Reference
Client and server are collocated in different address space.
8
Client and Server
Proxy
Object
Reference
Servant
Client and server are collocated in the same address space.
Location Transparency
No changes to the source code are necessary in either client or
server.
9
Distributed Objects and Middleware
• Explicit Middleware
(transaction, security, etc.)
Difficult to write
Difficult to maintain
Difficult to support
10
Explicit Middleware (gained through API)
11
Distributed Objects and Middleware
• Implicit Middleware (Recommended)
JTS
Instead of writing any code to middleware APIs, you declare what you need in a
simple text file. The request interceptor provide the middleware logic for you
transparently
12
Implicit Middleware (gained through declaration)
13
Enterprise Bean Constitution
• Enterprise Bean Class
An enterprise bean class contains implementation details of your component
and conforms to a well-defined interface and certain rules which are necessary
for your beans to run in any EJB container.
Session Beans: business-process-related logic
javax.ejb.SessionBean
Entity Beans: data-related logic
javax.ejb.EntityBean
message-oriented logic
javax.ejb.MessageDrivenBean
public interface javax.ejb.EnterpriseBean extends java.io.Serializable {
...
}
14
Enterprise Bean Constitution
•
The EJB Object
The client never invokes the
methods directly on an actual bean
instance. Rather, the invocation is
intercepted by the EJB container,
and then delegated to the bean
instance.
The EJB container automatically
performs the implicit middleware.
Services
(See also : page 39)
15
Enterprise Bean Constitution
EJB objects clone and expose every business method that the bean
itself expose, and it acts as a surrogate object between the client and
the bean.
We could think of EJB objects as physical parts of the container, all
EJB objects have container-specific code inside of them. So container
vendor generates the class file for your EJB Object automatically.
The Remote Interface
This interface duplicate all the business methods that the
corresponding bean class expose.
16
Enterprise Bean Constitution
Java RMI-IIOP and EJB Objects
Every javax.ejb.EJBObject extends java.rmi.Remote which is
part of Java Remote Method Invocation over Internet InterORB Protocol (RMI-IIOP).
(1) Any object that implements javax.rmi.Remote interface
is a remote object and is callable from a different JVM.
(2) Any method that is part of a remote object must throw a special
remote exception. The parameters you pass in method must be
valid types for RMI-IIOP, such as primitives, serializable objects and
RMI-IIOP remote objects
17
Figure 2.5 EJB Objects
18
EJBObject interface source
public interface javax.ejb.EJBObject extends java.rmi.Remote {
public javax.ejb.EJBHome getEJBHome()
throws java.rmi.RemoteException;
public java.lang.Object getPrimaryKey()
throws java.rmi.RemoteException;
public void remove()
throws java.rmi.RemoteException, javax.ejb.RemoveException;
public javax.ejb.Handle getHandle()
throws java.rmi.RemoteException;
}
public boolean isIdentical(javax.ejb.EJBObject)
throws java.rmi.RemoteException;
19
Enterprise Bean Constitution
• The Home Object
EJB promotes location transparency, so clients should never be aware of
exactly where an EJB object resides. To acquire a reference to an EJB object,
your client code asks for an EJB object from an EJB object factory which is
called a home object.
Chief responsibilities of home objects:
create EJB objects
find existing EJB objects
remove EJB objects
Home objects are physically part of the container and are automatically
generated by the container vendor’s tool.
20
Enterprise Bean Constitution
The Home Interface
The container’s home object implements your home interface.
Home interface simply defines methods for creating,
destroying, and finding EJB objects.
Your home interface must extend javax.ejb.EJBHome
interface.
21
Figure 2.6 Home Object
22
EJBHome interface source
public interface javax.ejb.EJBHome extends java.rmi.Remote {
public EJBMetaData getEJBMetaData()
throws java.rmi.RemoteException;
public javax.ejb.HomeHandle getHomeHandle()
throws java.rmi.RemoteException;
public void remove(javax.ejb.Handle handle)
throws java.rmi.RemoteException, javax.ejb.RemoveException;
}
public void remove(Object primaryKey)
throws java.rmi.RemoteException, javax.ejb.RemoveException;
23
7 steps in remote invocation
•
(1) The client calls a local stub.
•
(2) The stub marshals parameters into a form suitable for the network.
•
(3) The stub goes over a network connection to the skeleton.
•
(4) The skeleton demarshals parameters into a form suitable for Java.
•
(5) The skeleton calls the EJB object.
•
(6) The EJB object performs needed middleware, such as connection pooling,
transactions, security, and lifecycle services.
•
(7 )Once the EJB object calls the enterprise bean instance, and the bean does
its work, each of the preceding steps must be repeated for the return
trip home.
24
Enterprise Bean Constitution
• The Local Interfaces
EJB 2.0 specification provides a local interface rather than a remote
interface, and local home interface rather than home interface to avoid
the steps of the stub, skeleton, network and marshaling/unmarshaling
of parameters.
You can use the local interface when you call beans in the same
process.
25
Enterprise Bean Constitution
local object ---- local interface ---- javax.ejb.EJBLocalObject
local home object ---- local home interface ---- javax.ejb.EJBLocalHome
pass-by-reference
EJB object ---- remote interface ---- javax.ejb.EJBObject
Home object ---- home interface ---- javax.ejb.EJBHome
pass-by-value
26
Enterprise Bean Constitution
• Deployment Descriptors
Deployment descriptors declare how your beans should use middleware, rather
than you writing code to use middleware (implicit middleware).
In EJB 2.0, a deployment descriptor is an XML file.
Bean management and lifecycle requirements
Persistence requirements (Entity beans only)
Transaction requirements
Security requirements
27
Three key components of an EJB bean
• EJBHome (extends javax.ejb.EJBHome)
using the factory design pattern, defining the create, find and remove methods.
• EJBObject (extends javax.ejb.EJBObject)
using the proxy design pattern, defining the business logic implemented in bean.
• Bean Implementation Class (implements
javax.ejb.EntityBean/SessionBean/MessageDrivenBean)
implementing the business logic
28