background image

When to Use Session Beans

<< What Is a Session Bean | What Is a Message-Driven Bean >>
<< What Is a Session Bean | What Is a Message-Driven Bean >>

When to Use Session Beans

A stateless session bean can implement a web service, but other types of enterprise beans
cannot.
When to Use Session Beans
In general, you should use a session bean if the following circumstances hold:
At any given time, only one client has access to the bean instance.
The state of the bean is not persistent, existing only for a short period (perhaps a few hours).
The bean implements a web service.
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 invocations.
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, "The Duke's Bank
Application."
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 clients. For example,
you might use a stateless session bean to send an email that confirms an online order.
What Is a Message-Driven Bean?
A message-driven bean is an enterprise bean that allows Java EE applications to process
messages asynchronously. It normally acts as a JMS message listener, which is similar to an
event listener except that it receives JMS messages instead of events. The messages can be sent
by any Java EE component (an application client, another enterprise bean, or a web
component) or by a JMS application or system that does not use Java EE technology.
Message-driven beans can process JMS messages or other kinds of messages.
For a simple code sample, see
Chapter 23, "A Message-Driven Bean Example."
For more
information about using message-driven beans, see
"Using the JMS API in a Java EE
Application" on page 954
and
Chapter 32, "Java EE Examples Using the JMS API."
What Is a Message-Driven Bean?
The Java EE 5 Tutorial · September 2007
634