Académique Documents
Professionnel Documents
Culture Documents
EJB Overview
In this session, we are going to talk about basic and high level concept and
architecture of EJB. We still have 2 or possibly 3 more sessions for talking
about session beans, entity beans, message driven beans, security,
transaction, and persistence of EJB technology in detail later on.
1
06/01/2003
Sang Shin
sang.shin@sun.com
www.javapassion.com/j2ee
Technology Evangelist
Sun Microsystems, Inc.
2
2
06/01/2003
3
06/01/2003
Revision History
? 02/07/2003: version 1, created (Sang Shin)
? 05/11/2003: version 2, Speaker Notes are revised. (Daryl Luk luk@ptc.com)
? To be done
– speaker notes still need to be refined
4
06/01/2003
Agenda
? What is and Why EJB?
? EJB Architecture
? Component and Container Architecture
? Types of Beans
– Session beans, Entity beans, Message-Driven Beans
? Roles
? Anatomy of a EJB module
? How client invokes methods of EJB
? RMI communication model
? Deployment Descriptor & Packaging
5
So this is the agenda for this presentation. First, we will talk about what is EJB and then why
and when you want to use EJB for the development and deployment of enterprise applications.
Then we will spend some time talking about EJB architecture especially from the standpoint of
component and container architecture. This component and container architecture is a rehash
of what we already talked about in “J2EE overview”.
We will spend sometime talking about types of beans a bit. There are three types of beans
currently: session beans, entity beans, and message driven beans. Each of these bean type
addresses specific area of writing enterprise application.
In building and deploying EJB application, there is a clear separation of roles. For example, the
role that is played by bean developer is different from the role that is played by the deployer.
And we will talk about these logical roles.
We will look into the anatomy of EJB module. That is, when you build EJB module, as a bean
developer, the question of “what do you have to include in that module?” will be addressed
here.
We will talk about a bit on the RMI communication model between the client and EJB server.
Along with it, we will talk about the difference between local client view and remote client view.
Again, we will talk about these issues in rather high-level today and later on in this class, these
issues will be addressed again in more detail. 5
06/01/2003
What is EJB?
6
06/01/2003
This is the definition of EJB architecture from EJB specification. The key
point of EJB architecture is to provide server side component architecture
for building scalable, transactional, and multi-user secure enterprise
applications.
7
06/01/2003
So this is a just a repeat of the previous slide. This is a bit short description of EJB.
8
06/01/2003
Now let's talk a little bit on EJB design principles. (These design principles are
quoted from Kevin Boone's Applied Enterprise Beans Technology” Book,
chapter 1.)
9
06/01/2003
This is discussed at [EJB2.0 specification, chapter 5.4]. The EJB Specification was
designed from the outset to support the integration of components from different
vendors. For example, EJBs refer to other components and services to which they have
access by arbitrary names. These names can be mapped onto real names without
modifying program code. This allows EJBs to be authored without a detailed knowledge
of the environment in which they will be used, or of the other components with
which they will interact. In traditional software engineering, this principle is called
‘loose coupling’. A loosely-coupled system is easier to test and maintain, and its
components are easier to be re-used.
10
06/01/2003
The clients of EJBs do not, in principle, need access to the EJB implementation
code itself, either at compile time or run time. An EJB’s interaction with its
clients is specified entirely in terms of Java interfaces. These interfaces expose the
methods that clients can call, and thereby set out a ‘contract’ between the client
and the EJB. Developers who work with systems that do not enforce this strict
separation between ‘interface’ and ‘implementation’ sometimes find the use of
interfaces a burden in EJB development; nevertheless, it increases portability and
modularity, and makes it easier to support integration with non-Java components
(in which interfaces may be specified using IDL, for example).
11
06/01/2003
12
06/01/2003
13
06/01/2003
EJB in a
Big Picture of J2EE
14
Now let's take a look at where EJB fits in in the big picture of J2EE.
14
06/01/2003
Where is EJB?
15
This is the same picture we have seen during the J2EE overview session. As you
can see, the Servlet and JSP are web tier components that are running within web-
tier container. The role that web components play are basically receiving client
requests that are coming in the form of HTTP requests and then perform dynamic
contents generation or performing business logic by themselves or delegating it to
the EJB tier components.
15
06/01/2003
EJB clients
talking to EJB server
Web
Browser Components EJBs
DB & EIS
Resources
Stand-alone
Application
Now EJB clients are anything that invokes methods of EJB beans. EJB beans reside
in EJB server. The EJB clients can be standalone applications or web-tier
components. The EJB clients can be even other EJB beans.
16
06/01/2003
Why EJB?
17
17
06/01/2003
Why EJB? By providing a standard component model on the server side, it leverages all the
benefits that are inherent in component technology, for example, reusability of components,
availability of 3rd party components that you can assemble together to create applications,
availability of tools, and so on.
Another key benefit of EJB is the separation of business logic from system code. That is, the
EJB server platform provides all the system services such as resource management,
transaction coordination and persistence management. Since the system services are provided
by the server platform itself, you, as a developer, can now focus your development effort to
building business logic components rather than system code.
Because EJB is built around industry-standard component framework, and because it is based
on Java, it allows portability of your components. That is, the business logic components you
build will be portable across different vendors’ server platforms as well as different
operational environments without any change in your code or without even recompiling. It is
truly binary portability we are talking about.
Now you might want to ask? How do these components adapt themselves to the different
operational environments? That is, different operational environments have different
requirements on security policy, they have different databases in place, different transactional
model maybe have to be used. How do you instruct your business components to a different
behavior without actual change of code? It is done by what is called deployment descriptor.
The deployment descriptor is essentially an XML file that declaratively specifies the runtime
behavioral characteristics of your business component. And it gets constructed or changed at
the time of deployment, not at the time of code development.
18
06/01/2003
Enterprise JavaBeans
? Defined as business logic ONLY
? No low-level plumbing
? Reusable across multiple EJB Servers
? Implement interfaces that allow
container to manage them
19
19
06/01/2003
Many people are asking if they need to use EJB technology. I would say
that in some cases it makes sense to write a web-only application
20
06/01/2003
EJB Architecture
21
21
06/01/2003
EJB Architecture
22
This picture shows a somewhat simplified architecture of EJB. We will talk about
the concept of containers and components later on. But a key architectural
concept of EJB is that there is a separation of business logic components from the
hosting environment in which those components are running. The business logic
components under EJB architecture are represented as EJB beans while the hosting
environment is represented by container or EJB server.
As a business component developer, you have to write three Java files. First you
have to write EJB home interface which defines the methods that will be used by
clients in order to create and locate your bean. Second, you have to write EJB
remote interface which defines the business methods of your bean. Finally, you
will build your bean.
Now container, at the time of deployment of your beans, will create two internal
objects, EJB home object and EJB remote object. These objects are
implementation of home and remote interface you have defined. So when the
client wants to invoke some business methods of the EJB bean you created, it is
actually talking to these two intermediary objects instead. Why this indirection?
This is to allow the container to intercept the calls so that it can provide system
services such as security, transaction, persistence, resource management, life cycle
management, and so on.
22
06/01/2003
23
23
06/01/2003
24
The client of EJB can be anything that invokes methods of EJB bean. So
the client can be web tier components such as servlet and JSP, which is
typically the case. The client can be standalone application or applet
running within a browser. It can be even another EJB. In fact, as we will
talk about it later on, session beans and entity beans are used together in so
called facade pattern in which session beans receive client requests and then
talk to entity beans for data access logic. So in this case, the session beans
are playing the client role to the entity beans.
EJB 2.1 specification also talks about how business functions of EJB can
be exposed as web services. So in this case, the client can be web services
client invoking the methods of EJB bean by sending and receiving SOAP
messages.
So basically the client model provides development model for clients using
EJB services, that is, how to invoke business methods of EJB bean.
24
06/01/2003
25
06/01/2003
26
Component contract basically specifies what the container is required to do for components.
For example, the container enables the EJB method invocation from clients. As we will talk
about this concept several times today, when a client makes a method invocation, the container
in fact intercepts the call first and do something and then delegate the call eventually to your
bean instance.
Container manages the life-cycle of EJB bean instances. That is, bean developer does not have
worry about creating an instance of the bean, because container handles it. And since the
container knows the best in terms of system resources, it also knows when is the good time to
create a pool of bean instances and when to remove or passivate them.
It is the container that actually implements the home and remote interfaces. And in fact, the
implementation of these interfaces are called EJB home object and EJB object. Again we will
talk about this several times.
Container handles the persistence for CMP entity beans. CMP stands for Container managed
persistence. For CMP entity beans, bean developer does not handle the database access logic
himself. Instead, it will be dealt with by the container.
Container also creates a context when it first creates a bean instance and passes that context to
the bean. Container also provides other system services such as transaction, security, and
exceptions. Container also invokes methods that are defined in the bean.
26
06/01/2003
EJB Contracts
Client
Client view contract Enterprise
bean instances
EJB Component
Container contract
EJB server
27
So this picture shows the two contracts we just talk about. Again, the client
view contract is between client and EJB container and the component
contract is between bean and its container.
27
06/01/2003
Fundamentals of
EJB Architecture
(source: Applied Enterprise JavaBeans written
by Kevin Boone)
28
However, there are some distinctive features of EJBs which make EJB
development rather different from (and easier than) other techniques for
developing distributed applications. We will talk about these distinctive
features in this segment of the presentation. (source: Applied Enterprise
JavaBeans)
28
06/01/2003
Now let's talk about fundamentals of EJB architecture. Some of these are rehash of what
we just talked in the “EJB architecture” segment of the presentation. But I would like to
emphasize these points again to make sure you get clear conceptual idea.
29
06/01/2003
30
06/01/2003
Clients can call only those methods exposed by the EJB’s interfaces. In the EJB Specification,
the interfaces are collectively referred to as the client view.
Each EJB publishes ‘factory’ interfaces and ‘business method’ interfaces. The factory interfaces
expose methods that clients can use to create, locate and remove EJBs of that type. The business
method interfaces define all the methods that clients can call on a specific EJB after it has been
located or created through the factory interface. The term ‘business method’ is a generic term
used to describe any method that a client can call on a specific EJB, and does not have to be
related to ‘business’ in the dictionary sense.
It is important for the developer to understand that anything that makes method calls on an EJB is
a client of that EJB, and interacts with it via its factory and business method interfaces. This
applies even in the case where multiple EJBs interact within the same JVM. Enforcing this model
allows the EJB container – of which, more later – to provide important services transparently. If
we are sure that the EJBs must be in the same JVM, then we can use local home and local
interfaces, rather than remote home and
remote interfaces.
31
06/01/2003
Although EJB clients make method calls as if they were directly on the EJB, in fact the
method calls are actually on the EJB container, which delegates to the EJB itself.
The client never calls EJB methods directly, even if the client and the EJB are actually on
the same server, and even in the same JVM. This strategy allows powerful features like
distributed transaction management to be provided transparently, and provides for
pooling of EJB instances to increase efficiency.
The EJB container, and therefore the EJB, is accessible to clients via proxy objects. We
will have much more to say about these objects later. Message-driven EJBs are not called
directly by clients at all, and don’t have container proxies in the same sense. Instead they
are called directly by the container when it receives a message for a queue or topic in
which the EJB has registered an interest.
The container encapsulates the EJB and acts as its security manager. It also provides
general services to the EJB, as we shall see. The notion of the container and its proxies is
illustrated in figure 3.2. It is a moot point whether the proxies are considered to be part of
the container or separate from it; it makes no practical difference to the EJB developer.
Because the methods on the EJB proxies will delegate to methods on the EJB itself, the
proxies must be generated dynamically to match the EJB. The vendor of the EJB server
vendor will provide tools to support this; normally this generation will take place when
the EJB is deployed to the server.
32
06/01/2003
The developer of an EJB should not have to be concerned about concurrency and
thread management; in fact, it contravenes the EJB Specification [EJB2.0 24.1.2] for the
developer to include such code to handle these issues. The container ensures that the
EJB is never called re-entrantly, so the developer can code the EJB as if it were used in a
single-threaded environment.
33
06/01/2003
It does this even when the transaction spans multiple servers. There is a mechanism for
specifying the transactional behavior of EJBs outside of program code, although limited
programmatic control of transactions is permissible as well.
34
06/01/2003
Security attributes specify which methods on which EJBs are accessible to which groups
of users. No coding is required, although limited programmatic intervention is allowed.
The EJB server must provide a mechanism for end users to identify themselves to the
application (e.g., a ‘log in’ dialog box). The EJB developer should never have to code
authentication procedures.
35
06/01/2003
There is a well-defined way for the client to create new EJBs, or to find existing ones. It
does this on the factory interface, which it finds by doing a JNDI lookup on the EJB
name. Having found the interface, the client can call its create() or find() methods to
create or locate new EJB instances. When it does this, it gets a reference to a proxy, not
to the the EJB itself. The client manipulates the proxy exactly as if it were the real EJB,
but the proxy carries out other functions (transaction management, for example) as well
as delegating method calls to the EJB.
36
06/01/2003
When a client creates an EJB, it is really creating a ‘conceptual EJB’, not an instance of a
Java class. The EJB container may pool instances of implementation class, and give the
client a reference to an existing instance when the client asks to create an EJB. Allowing
the container to pool instances can lead to enormous gains in efficiency. Again, this is
transparent to the client, but the developer must be aware of the circumstances in which
pooling is permitted.
37
06/01/2003
EJBs get access to external resources – such as databases and messaging systems –
through the container. The container can pool, share and manage these resources on
behalf of the EJBs, and in a way which is transparent to the EJB developer. In this way
we get all the benefits or a resource sharing scheme, with few of the disadvantages.
38
06/01/2003
There is a specified, standard way for the EJB to be packaged and transferred to the
server. This process is called deployment. Any EJB server that complies with the EJB
Specification [EJB2.0 23] must be able to accept an EJB packaged in the correct way,
regardless of the platform on which it was developed and tested.
39
06/01/2003
J2EE
Component & Container
Architecture
40
40
06/01/2003
J2SE
RMI/IIOP
RMI/IIOP
JavaMail JavaMail
JDBC
JTA
JDBC
JMS
JNDI
JNDI
JTA
JMS
App Client JAF
Container JAF
App HTTP/ J2SE
Client HTTPS
RMI
RMI/IIOP
JDBC
JNDI
JMS
J2SE J2SE
Database
41
This is what I call the J2EE Container & components diagram. As this diagram illustrates,
containers and components are the key concepts of J2EE.
As for the relationship between components and containers, as you might have guessed it, the
components are running within containers. That is, the containers provides the host execution
environments for the components. In this picture, the components are colored in green and the
containers are in purple. The types of components within J2EE environment are
•Client components that are running on client either as a stand-alone or applet
• JSP or servlet as web components running inside web container
•EJB beans as business components running inside EJB container
And you developers are responsible for the implementation of these components. On the other
hand, the containers are provided by the platform vendors. The containers provide runtime
system services such as transaction coordination, persistence management, resource pooling
The containers are also responsible for providing the enterprise APIs, shown in gold, and the
distributed communication protocols, shown in brown.
In a typical multi-tier, thin client enterprise application, most of the development effort will be
focused on building web-tier components at web-tier and enterprise Java beans at EJB tier.
41
06/01/2003
We touched upon the roles of container and components a bit in the previous slide. Now let’s compare the
tasks that are being performed by containers and the ones performed by components side by side. As we
talked about in the previous slide, the platform vendors provide containers while you as application developers
develop your applications in the form of components and deploy them over the containers. As you probably
will notice, many of the tasks that the containers perform are system services that a typical enterprise
application would need.
First., container handles concurrency. That is, it handles concurrent access from multiple clients to your
business component so that you don’t have to deal with it. However, each platform vendor might use different
synchronization schemes to support concurrency. Second, containers provide built-in security framework so
that implementing secure applications can be a matter of configuring some options on authentication and access
control at the time of deployment rather than at the time of code development. Next, availability and scalability.
We mentioned already that platform vendors compete in their implementations especially in the area of
availability and scalability. For example, one J2EE container vendor might provide high availability by
maintaining session state on a persistent storage. Another vendor might choose to implement it in a different
way.
Persistence and transaction can also be handled by the container if you choose to do so. Or you might want to
implement them on your own in your code if more customized behavior is desired. Life-cycle management.
Containers handle the creation and destruction of your component instances according to its own
implementation scheme. Finally management and administration, some vendors might provide better
management tool for managing and administering various resources in the container.
So what do you have to do as developers? Very little really. You handle only presentation and focus majority
of your development effort on building business components.
42
06/01/2003
43
(to be added)
43
06/01/2003
Terminology
44
Now we have used various terms so far and this is a source of some
confusion in terms of understanding the EJB architecture. So let's make
sure we use consistent terms in referring to specific things.
44
06/01/2003
Terminology Confusion
? The term “EJB” is used in many different
context
– Technology, Architecture, Specification,
Implementation, Product, Server, Container
– Bean class
– Bean instance
– EJB module
– EJB application
– EJB proxy objects
45
In many EJB articles, the term EJB is used to refer to many things.
At other times, the term EJB is used to refer to a bit more concrete artifacts
such as bean class, bean instance, EJB module, or EJB application.
45
06/01/2003
(to be added)
46
06/01/2003
(to be added)
47
06/01/2003
Types of Beans
48
48
06/01/2003
Types of Beans
? Session Beans
– Stateful session beans
– Stateless session beans
? Entity Beans
– Bean Managed Persistence (BMP)
– Container Managed Persistence (CMP)
? Message Driven Beans
– JMS
– JAXM
49
(to be added)
49
06/01/2003
Session Beans
? Does work on behalf of a single client
? Is not persistent and hence relatively short
lived
– Is removed when the EJB™ server crashes
? Does not represent data in data store,
although can access/update such data
? Bean class implements
javax.ejb.SessionBean interface
50
The client normally creates an EJB, calls methods on it, and then removes it.
If the client fails to remove it, the EJB container will remove it after a certain
period of inactivity.
50
06/01/2003
(to be added)
51
06/01/2003
52
52
06/01/2003
– Interest calculator
? No client specific state needs to be preserved
53
(to be added)
53
06/01/2003
Entity Beans
? Provides object view of data in data store
– Its lifetime not related to the duration of
interaction with clients
– Lives as long as data exists in database i.e. Long
lived
– In most cases, synchronized with relational
databases
? Shared access among clients
? Bean class implements
javax.ejb.EntityBean interface
54
Entity EJBs represent persistent objects; their lifetimes are not related to the
duration of interaction with clients. In nearly all cases, entity EJBs are
synchronized with relational databases; this is how persistence is achieved.
Entity EJBs are always shared amongst clients: a client cannot get an entity
EJB to itself. Thus entity EJBs are nearly always used as a scheme for
mapping relational databases into object-oriented applications.
54
06/01/2003
Entity Beans
? Clients normally look up (find) an existing
entity EJB
– Creation means adding a row to a database table
– Finding means finding a row in a existing database
table
– Removing means removing a row from a database
table
? Entity bean instance has unique identifier
called primary key
– Primary key can be any class
55
Whereas a client normally creates a session EJB and removes it after use,
clients normally look up an existing entity EJB. Creation of an entity EJB
corresponds to adding new data items to the application (e.g., adding rows
to database tables).
An important feature of entity EJBs is that they have identity, that is, one can
be distinguished from another. This is implemented by assigning a primary
key to each instance of the EJB, where ‘primary key’ has the same meaning
as it does for database management. Primary keys that identify EJBs can be
of any type, including programmer-defined classes.
55
06/01/2003
56
56
06/01/2003
57
57
06/01/2003
58
58
06/01/2003
59
59
06/01/2003
60
The EJB Specification has relatively little about the intended applications of each of the
EJB types. It is possible, and may be sensible, to build an application entirely of
session EJBs, for example. However, the properties of the EJBs types themselves
strongly favor a ‘tiered’ application model, where entity EJBs separate the session EJBs
from the data sources. In such a model entity EJBs act primarily to provide an object-
oriented view of the data, to simplify the development of the session EJBs which will
then contain the bulk of the application logic. This model has a number of advantages,
which are briefly summarized below.
Instances of the implementation classes of both session EJBs and entity EJBs are held
in a pool by the server, but for different reasons. Session EJBs are pooled to support
large client loads; entity EJBs are pooled to support large databases.
It simplifies the transaction management model; because session EJB methods can
encompass multiple method calls on multiple entity EJBs, the transaction boundaries
can be defined to be equivalent to session EJB method call boundaries. The EJB
architecture has built-in support for this notion of transaction management. We will
have much more to say about this subject later.
60
06/01/2003
61
61
06/01/2003
Message-driven EJBs are the only type that do not follow a strict request-response
interaction with clients.
62
06/01/2003
Roles
63
S.
63
06/01/2003
Roles
? Distinct roles in the application
development and deployment life cycle
– EJB Bean Provider (Component provider)
– Application Assembler
– Deployer
– EJB Server Provider
– Container Provider
– System Administrator
– Tool provider
64
The first two roles involve purchasing and installing the J2EE product and
tools. Once software is purchased and installed, J2EE components can be
developed by application component providers, assembled by application
assemblers, and deployed by application deployers. In a large organization,
each of these roles might be executed by different individuals or teams. This
division of labor works because each of the earlier roles outputs a portable
file that is the input for a subsequent role. For example, in the application
component development phase, an enterprise bean software developer
delivers EJB JAR files. In the application assembly role, another developer
combines these EJB JAR files into a J2EE application and saves it in an
EAR file. In the application deployment role, a system administrator at the
customer site uses the EAR file to install the J2EE application into a J2EE
server.
The different roles are not always executed by different people. If you work
for a small company, for example, or if you are prototyping a sample
application, you might perform the tasks in every phase.
64
06/01/2003
Lifecycle Illustration
Creation Assembly Deployment
Assembled
Created by J2EE Modules and Augmented J2EE APP
Component Processed
by Application by Deployer
Developer
Assembler
Deploy
J2EE Container
Enterprise
Components
65
- The first step is to create all the necessary business and web components. For
example, developers build business logic components such as EJB beans. They might
also get involved in building Java beans or custom tags of JSP pages. UI designers
also create web pages at this stage. One important point here is that you don’t have to
build everything yourself. That is, you might want to purchase 3rd-party, off-the-shelf
business components instead of building your own. After all, that is the whole point of
component technology. Again, this is possible because of the open and standard
based component technologies such as EJB and J2EE.
- Next step is to assemble these business and web components into an application.
This step is performed by an application assembler. His role is basically to create a
package that contains all the necessary components and descriptors that describe
those components.
65
06/01/2003
Payroll
Deployment
Deployment
Descriptor
Descriptor
66
66
06/01/2003
Employee
GUI Self
Service
Deployment
Deployment
Descriptor
Descriptor
67
67
06/01/2003
Self
GUI Service
Payroll
Database
Payroll
Deployment
Deployment
Descriptor
Descriptor
68
68
06/01/2003
69
69
06/01/2003
? Remote interface
70
06/01/2003
71
71
06/01/2003
Home Interface
? Defines methods for creating, finding and
removing beans
– Factory interface
? Implemented by Container
– EJB home object
? Client gets “reference to stub object of the
EJB home object” via JNDI
? Can be remote or local
72
72
06/01/2003
// (Remote) home interface for the `Interest' EJB (c)2002 Kevin Boone
public interface InterestHome extends EJBHome{
73
73
06/01/2003
// (Local) home interface for the `Interest' EJB (c)2002 Kevin Boone
public interface InterestLocalHome extends EJBLocalHome {
74
74
06/01/2003
75
75
06/01/2003
Remote Interface
? Defines business methods
– Business methods are methods that deals with
application specific business logic
? Implemented by Container
– EJB object
? Client gets “reference to stub object of the
EJB object” through create() or find()
method of EJB Home interface
? Can be remote or local
76
76
06/01/2003
77
77
06/01/2003
78
78
06/01/2003
79
79
06/01/2003
80
06/01/2003
81
Now let's go over the steps that client takes in order to invoke a business
method of a bean.
81
06/01/2003
To find the EJB’s home object the client first instantiates a JNDI naming context,
using the default constructor. This will work if the defaults used by the naming
service are appropriate for your system. For the J2EE Reference Implementation
the defaults will be correct if the client can contact the server using the hostname
‘localhost’ and the naming service is listening on port 1050. If the client and server
are running on the same machine, this is likely to be the case. Your server vendor’s
documentation should tell you whether the naming context needs additional
information. If the naming service is not running on the same machine as the client
(as is likely in practice), the client needs to supply some more information to JNDI .
The client calls remove() on the remote object when it has finished with the EJB. As
we shall see, however, the server does not remove the bean instance, or call
ejbRemove() on it at this point.
82
06/01/2003
// Test client class for the `Interest' EJB (c)2002 Kevin Boone
public class InterestTestClient {
public static void main (String[] args) throws Exception {
// Step 1 and Step 2 are captured in a subroutine called getInterest()
Interest interest = getInterest();
double principal = 10000.0;double rate = 10.0;int terms = 10;
System.out.println ("Principal = $" + principal);
System.out.println ("Rate(%) = " + rate);
System.out.println ("Terms = " + terms);
// Step 3: Invoke business methods
System.out.println ("Interest = $" +
interest.getInterestOnPrincipal (principal, rate, terms));
System.out.println ("Total = $" +
interest.getTotalRepayment( principal, rate, terms));
// Step 4: Clean up, remove instance of stateless session bean
interest.remove(); 83
}
83
06/01/2003
84
84
06/01/2003
RMI Communication
Model
85
Now I would like to talk a little bit on RMI communication and how it is
related to EJB.
85
06/01/2003
Distributed Objects
? Collaborating objects reside in different
computers (different VMs)
– client object invokes methods of server object
? There have to be some mechanisms
– passing method signature from client to server
– marshalling parameters from client to server
– unmarsalling received parameters at the server
– marshalling return values from server to client
– unmarshalling received results at the client
86
86
06/01/2003
Remote
Caller Object
Remote Interface
Stub Skeleton
87
So in a typical RMI communication model, in order for the caller and the
remote object to communicate, there has to be something like stub and
skeleton. The stub receives method calls from the caller and then marshals
the parameters and then passes the information on the method and its
parameters to the server side. On the server side, it is the skeleton who
receives the method and parameters. It then unmarshals the parameters and
then passes it to the remote object. Remote object then performs the
business functions of the business method and then return the result. The
skeleton then receives the result and then marshals it and sends it to the
client.
87
06/01/2003
88
88
06/01/2003
89
89
06/01/2003
90
90
06/01/2003
91
06/01/2003
stubs
92
06/01/2003
create, find
EJBHome
Client
glue bean
EJBObject
business
methods
container
server
93
(to be added)
93
06/01/2003
94
A Client makes a request on a remote object using a client-side stub. The stub
marshalls the method arguments into serialized form and forwards the method
request to the Object Request Broker. An Object Request Broker is an
intermediary between distributed objects enabling them to communicate. ORBs
are responsible for finding objects to service methods calls, handling parameter
passing, and returning results. The ORB converts the client request into low-
level transport requests ( IIOP), then the request is sent over the network to the
remote object.
On the Server side the ORB receives the transport level request and converts it
into a request for the server skeleton for the remote object. The skeleton
converts the remote request into the appropriate method call un-marshalling the
method arguments and passing them to the remote object implementation.
94
06/01/2003
95
06/01/2003
96
96
06/01/2003
97
06/01/2003
Local Client
? (talk about Local client has to be session
bean talking to local entity beans)
98
98
06/01/2003
Local Interface
? Advantages
– More efficient access due to co-location due to no
RMI over IIOP overhead
– Ability to share data between client and bean
through call by reference
? Disadvantages
– Tight coupling of client and bean
– Less flexibility in distribution
? Use local interface beans (as opposed to
remote beans) for fine-grained operations
99
The disadvantage is that the bean is now tightly coupled with its client, thus
there is no flexibility in distribution. That is, the local bean has to be co-
located with its client. So the recommendation is that you can use fine-
grained interfaces for local beans because the cost is very minimum.
99
06/01/2003
100
100
06/01/2003
Recommendations
? Use local interface whenever possible
– Create islands of local components (local entity
beans and their dependent objects)
? Use facade pattern in which a remote
interface session bean (for synchronous
operations) or message driven bean (for
asynchronous calls) invokes local entity
beans
? Use remote interface for loose coupling
101
So how do you design your application? In most of the cases, you want to
use local interface whenever possible. That is, you can create island of
local beans.
101
06/01/2003
Line Item
Remote Entity
Session Card
1:m
Bean Entity
Facade Purchase
Order Entity
Address
Entity
Relationship
Reference
102
102
06/01/2003
Local
EntityBean
Relationship
Reference 103
This is the case where message driven beans are used to receive
messages asynchronously and then invoke other beans for business
logic processing.
103
06/01/2003
EJB-JAR Deployment
Descriptor
104
104
06/01/2003
105
06/01/2003
106
06/01/2003
107
06/01/2003
108
06/01/2003
<ejb-jar>
<display-name>Interest_ejb</display-name>
<enterprise-beans>
<session>
<display-name>InterestBean</display-name>
<ejb-name>InterestBean</ejb-name>
<home>com.kevinboone.ejb_book.interest.InterestHome</home>
<remote>com.kevinboone.ejb_book.interest.Interest</remote>
<local-home>com.kevinboone.ejb_book.interest.InterestLocalHome</local-home>
<local>com.kevinboone.ejb_book.interest.InterestLocal</local>
<ejb-class>com.kevinboone.ejb_book.interest.InterestBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
<security-identity>
<description></description>
<use-caller-identity></use-caller-identity>
</security-identity>
</session>
109
</enterprise-beans>
structural information
109
06/01/2003
110
assembly information
110
06/01/2003
EJB Packaging
111
111
06/01/2003
Package Files
? J2EE Application (sometimes called EJB
application)
– *.EAR file
– Can contain Web tier modules (*.WAR files) and
EJB-JAR files
? EJB-JAR (EJB Module)
– *.jar file
– Some container allows direct deployment of EJB-
JAR files
? EJB Client Jar
112
112
06/01/2003
*.EAR file
? Contains both Web-tier modules and EJB
Modules (EJB-JAR files)
– It can contain multiple EJB-JAR files
? Has its own deployment descriptor
– application.xml
? For deploying EJB application over J2EE RI, you
have to create *.EAR file even if you have only
one EJB-JAR file and no Web modules
– Some container allows direct deployment of EJB-
JAR
113
113
06/01/2003
<application>
<display-name>interest</display-name>
<description>Application description</description>
<module>
<ejb>ejb-jar-ic.jar</ejb>
</module>
</application>
114
114
06/01/2003
?
InterestLocalHome.class
? InterestBean.class
?
InterestHome.class
? Interest.class
115
115
06/01/2003
EJB-JAR file
? Standard format for packaging enterprise
beans
? Used to package un-assembled and assembled
enterprise beans
? Must contain deployment descriptor
? For each enterprise bean, ejb-jar file must
contain following class files
– Enterprise bean class
– Enterprise bean home and remote interface
– Primary key class if the bean is entity bean
116
EJB-JAR file is a standard format for packaging enterprise beans. You can
package both unassembled and assembled enterprise beans.
116
06/01/2003
117
06/01/2003
Deployment tools
home
interface
bean
class
remote
interface
Deployment
descriptor
118
118
06/01/2003
Deployment tools
119
119
06/01/2003
Deployment tools
EJBHome EJBObject
Enterprise Beans
Container
Client stubs
context
meta attributes
client
Database
120
120
06/01/2003
Naming Conventions
For EJB Development
121
Now, since there are many pieces involved in typical EJB development, and
also typically you have more than a few EJB to build a relatively
sophisticated EJB applications, it is a good habit to use consistent names.
So here are the conventions.
121
06/01/2003
Naming Conventions
? Home Interface
– CatalogHome (remote), CatalogLocalHome (local)
? Remote Interface
– Catalog (remote), CatalogLoca (local)
? Bean class
– CatalogBean
? EJB-JAR Deployment Descriptor
– ejb-jar.xml (Same for all Beans)
122
Naming conventions for interfaces are pretty much well-agreed upon. For example,
for home interface, you name it with Home postfix. For example, you use
CatalogHome or CatalogLocalHome. For remote interface, use the name itself.
For example, Catalog or CatalogLocal will do. Bean class is typically named as
name with Bean postfix. An example is CatalogBean.
As for the deployment descriptor, same name ejb-jar.xml is used for all EJBs.
Since this xml file is under bean specific directory having same deployment
descriptor is not a problem.
122
06/01/2003
123
06/01/2003
124
I have separate EJB 2.0 section here to introduce new features introduced in
EJB 2.0.
124
06/01/2003
125
06/01/2003
126
126
06/01/2003
JMS Support
? By adding new enterprise bean type called
"MessageDrivenBean"
– Stateless bean without home and remote
interface
– Activated upon message arrival
– Bean implements javax.jms.MessageListener
interface
– onMessage() method implementation should
contain business logic
– Configured as listener for queue or topic
127
127
06/01/2003
128
128
06/01/2003
Home Methods
129
129
06/01/2003
Home Methods
? Additional methods on Home interface for
entity beans with both bean and container
managed persistence
– Business methods that are not specific to an entity
bean instance
– Implementation provided by Bean Provider
– Useful for bulk updates or other aggregate
operations
130
130
06/01/2003
Resources
131
131
06/01/2003
Resources Used
? Applied Enterprise JavaBeans Technology written by Kevin
Boone, Prentice Hall [1]
?
J2EE Tutorial in java.sun.com [2]
132
132
06/01/2003
133
133