Java Beans Notes
Java Beans Notes
Java Beans Notes
20
Enterprise Beans
743
JavaEETutorial.book Page 744 Thursday, February 1, 2007 4:36 PM
problems. The EJB container—and not the bean developer—is responsible for
system-level services such as transaction management and security authoriza-
tion.
Second, because the beans—and not the clients—contain the application’s busi-
ness logic, the client developer can focus on the presentation of the client. The
client developer does not have to code the routines that implement business rules
or access databases. As a result, the clients are thinner, a benefit that is particu-
larly important for clients that run on small devices.
Third, because enterprise beans are portable components, the application assem-
bler can build new applications from existing beans. These applications can run
on any compliant Java EE server provided that they use the standard APIs.
Note: Entity beans have been replaced by Java Persistence API entities. For infor-
mation about entities, see Chapter 24, Introduction to the Java Persistence API.
Stateful session beans are appropriate if any of the following conditions are true:
• The bean’s state represents the interaction between the bean and a specific
client.
• The bean needs to hold information about the client across method invoca-
tions.
• The bean mediates between the client and the other components of the
application, presenting a simplified view to the client.
• Behind the scenes, the bean manages the work flow of several enterprise
beans. For an example, see the AccountControllerBean session bean in
Chapter 37.
To improve performance, you might choose a stateless session bean if it has any
of these traits:
• The bean’s state has no data for a specific client.
• In a single method invocation, the bean performs a generic task for all cli-
ents. For example, you might use a stateless session bean to send an email
that confirms an online order.
described in the section Defining Client Access with Interfaces (page 749).
Unlike a session bean, a message-driven bean has only a bean class.
In several respects, a message-driven bean resembles a stateless session bean.
• A message-driven bean’s instances retain no data or conversational state
for a specific client.
• All instances of a message-driven bean are equivalent, allowing the EJB
container to assign a message to any message-driven bean instance. The
container can pool these instances to allow streams of messages to be pro-
cessed concurrently.
• A single message-driven bean can process messages from multiple clients.
The instance variables of the message-driven bean instance can contain some
state across the handling of client messages—for example, a JMS API connec-
tion, an open database connection, or an object reference to an enterprise bean
object.
Client components do not locate message-driven beans and invoke methods
directly on them. Instead, a client accesses a message-driven bean through, for
example, JMS by sending messages to the message destination for which the
message-driven bean class is the MessageListener. You assign a message-
driven bean’s destination during deployment by using Application Server
resources.
Message-driven beans have the following characteristics:
• They execute upon receipt of a single client message.
• They are invoked asynchronously.
• They are relatively short-lived.
• They do not represent directly shared data in the database, but they can
access and update this data.
• They can be transaction-aware.
• They are stateless.
When a message arrives, the container calls the message-driven bean’s
onMessage method to process the message. The onMessage method normally
casts the message to one of the five JMS message types and handles it in accor-
dance with the application’s business logic. The onMessage method can call
helper methods, or it can invoke a session bean to process the information in the
message or to store it in a database.
JavaEETutorial.book Page 749 Thursday, February 1, 2007 4:36 PM
Remote Clients
A remote client of an enterprise bean has the following traits:
• It can run on a different machine and a different Java virtual machine
(JVM) than the enterprise bean it accesses. (It is not required to run on a
different JVM.)
• It can be a web component, an application client, or another enterprise
bean.
• To a remote client, the location of the enterprise bean is transparent.
To create an enterprise bean that allows remote access, you must do one of the
following:
• Decorate the business interface of the enterprise bean with the @Remote
annotation:
@Remote
public interface InterfaceName {
...
}
• Decorate the bean class with @Remote, specifying the business interface(s):
@Remote(InterfaceName.class)
public class BeanName implements InterfaceName {
...
}
The remote interface defines the business and life cycle methods that are specific
to the bean. For example, the remote interface of a bean named BankAccount-
Bean might have business methods named deposit and credit. Figure 20–1
shows how the interface controls the client’s view of an enterprise bean.
BankAccountBean
Remote Client Remote
Interface
deposit()
credit()
Local Clients
A local client has these characteristics:
• It must run in the same JVM as the enterprise bean it accesses.
• It can be a web component or another enterprise bean.
• To the local client, the location of the enterprise bean it accesses is not
transparent.
The local business interface defines the bean’s business and life cycle methods.
If the bean’s business interface is not decorated with @Local or @Remote, and the
bean class does not specify the interface using @Local or @Remote, the business
interface is by default a local interface. To build an enterprise bean that allows
only local access, you may, but are not required to do one of the following:
• Annotate the business interface of the enterprise bean as a @Local inter-
face. For example:
@Local
public interface InterfaceName {
...
}
• Specify the interface by decorating the bean class with @Local and specify
the interface name. For example:
@Local(InterfaceName.class)
public class BeanName implements InterfaceName {
...
}
web services. This flexibility enables you to integrate Java EE applications with
web services.
A web service client accesses a stateless session bean through the bean’s web
service endpoint implementation class. By default, all public methods in the
bean class are accessible to web service clients. The @WebMethod annotation may
be used to customize the behavior of web service methods. If the @WebMethod
annotation is used to decorate the bean class’s methods, only those methods dec-
orated with @WebMethod are exposed to web service clients.
For a code sample, see A Web Service Example: helloservice (page 780).
Isolation
The parameters of remote calls are more isolated than those of local calls. With
remote calls, the client and bean operate on different copies of a parameter
object. If the client changes the value of the object, the value of the copy in the
bean does not change. This layer of isolation can help protect the bean if the cli-
ent accidentally modifies the data.
In a local call, both the client and the bean can modify the same parameter
object. In general, you should not rely on this side effect of local calls. Perhaps
someday you will want to distribute your components, replacing the local calls
with remote ones.
As with remote clients, web service clients operate on different copies of param-
eters than does the bean that implements the web service.
Assembly
Root
META-INF
ejb-jar.xml
sun-ejb-jar.xml MANIFEST.MF
Does Not
Exist
Timeout
1. Remove method
or Timeout
1. Dependency injection, 2. PreDestroy callbacks,
if any if any
2. PostConstruct callbacks,
if any PrePassivate
callbacks, if any
Ready Passive
PostActivate
callbacks, if any
While in the ready stage, the EJB container may decide to deactivate, or passi-
vate, the bean by moving it from memory to secondary storage. (Typically, the
EJB container uses a least-recently-used algorithm to select a bean for passiva-
tion.) The EJB container invokes the method annotated @PrePassivate, if any,
immediately before passivating it. If a client invokes a business method on the
bean while it is in the passive stage, the EJB container activates the bean, calls
the method annotated @PostActivate, if any, and then moves it to the ready
stage.
At the end of the life cycle, the client invokes a method annotated @Remove, and
the EJB container calls the method annotated @PreDestroy, if any. The bean’s
instance is then ready for garbage collection.
Your code controls the invocation of only one life-cycle method: the method
annotated @Remove. All other methods in Figure 20–3 are invoked by the EJB
container. See Chapter 34 for more information.
Does Not
Exist Ready
The client initiates the life cycle by obtaining a reference to a stateless session
bean. The container performs any dependency injection and then invokes the
method annotated @PostConstruct, if any. The bean is now ready to have its
business methods invoked by the client.
At the end of the life cycle, the EJB container calls the method annotated @Pre-
Destroy, if any. The bean’s instance is then ready for garbage collection.
Further Information
For further information on Enterprise JavaBeans technology, see the following:
• Enterprise JavaBeans 3.0 specification:
http://java.sun.com/products/ejb/docs.html