background image

Using the JMS API with Application Clients

<< Container-Managed Transactions | Further Information about JMS >>
<< Container-Managed Transactions | Further Information about JMS >>

Using the JMS API with Application Clients

bean-managed transactions allows you to process the message by using more than one
transaction or to have some parts of the message processing take place outside a transaction
context. In most cases, however, container-managed transactions provide greater reliability and
are therefore preferable.
When you create a session in an enterprise bean, the container ignores the arguments you
specify, because it manages all transactional properties for enterprise beans. It is still a good idea
to specify arguments of true and 0 to the createSession method to make this situation clear:
session = connection.createSession(true, 0);
When you use container-managed transactions, you normally use the Required transaction
attribute (the default) for your enterprise bean's business methods.
You do not specify a message acknowledgment mode when you create a message-driven bean
that uses container-managed transactions. The container acknowledges the message
automatically when it commits the transaction.
If a message-driven bean uses bean-managed transactions, the message receipt cannot be part
of the bean-managed transaction, so the container acknowledges the message outside the
transaction.
If the onMessage method throws a RuntimeException, the container does not acknowledge
processing the message. In that case, the JMS provider will redeliver the unacknowledged
message in the future.
Using the JMS API with Application Clients and Web
Components
An application client in a Java EE application can use the JMS API in much the same way that a
stand-alone client program does. It can produce messages, and it can consume messages by
using either synchronous receives or message listeners. See
Chapter 23, "A Message-Driven
Bean Example"
for an example of an application client that produces messages. For an example
of using an application client to produce and to consume messages, see
"An Application
Example That Deploys a Message-Driven Bean on Two Servers" on page 985
.
The Java EE platform specification does not impose strict constraints on how web components
should use the JMS API. In the Application Server, a web component can send messages and
consume them synchronously but cannot consume them asynchronously.
Because a blocking synchronous receive ties up server resources, it is not a good programming
practice to use such a receive call in a web component. Instead, use a timed synchronous
receive. For details about blocking and timed synchronous receives, see
"Writing the Client
Programs for the Synchronous Receive Example" on page 912
.
Using the JMS API in a Java EE Application
The Java EE 5 Tutorial · September 2007
960