Java Reference
In-Depth Information
After the JMS connection is created, the method obtains a JMS session by invoking
the
createSession()
method on the
Connection
object. The
createSession()
method has two parameters, the first parameter is a Boolean indicating if the created
session is transacted. Transacted sessions allow the code sending messages to a
JMS destination to send several messages as part of a transaction. To send several
messages as part of a transaction, the JMS client sends messages to the queue as
usual, then invokes the
commit()
method on the JMS session. By default, the code
generated by NetBeans does not create a transacted JMS session, but we can override
this by simply changing the value of the first parameter in
createSession()
to
true
.
The second parameter of the
createSession()
method indicates how JMS messages
will be acknowledged by the message receiver. There are three valid values for this
parameter, all three are defined as constants in the
javax.jms.Session
interface.
The value of the second parameter to
createSession()
is ignored when creating a
transacted session.
Acknowledge Mode
Description
Session.AUTO_ACKNOWLEDGE
When using this mode, the JMS session will auto-
acknowledge message receipt for the client.
Session.CLIENT_ACKNOWLEDGE
When using this mode, message receivers must
explicitly invoke the
acknowledge()
method
defined in
javax.jms.Message
in order to ac-
knowledge receipt of a message.
Session.DUPS_OK_ACKNOWLEDGE
When using this mode, the JMS session will lazily
acknowledge message receipts on behalf of the JMS
client. Using this acknowledge mode may result in
some messages being delivered more than once, but
it can improve performance by eliminating some
of the work the session must do in order to avoid
duplicate message deliveries.
Of the three acknowledge modes,
Session.AUTO_ACKNOWLEDGE
is the most
commonly used, since it slightly reduces the amount of work to be done by
application developers. NetBeans uses this mode by default in the generated code,
but we are free to modify the generated code as necessary to meet our requirements.
After creating a JMS session, the next thing the generated code does is to create a JMS
message producer by invoking the
createProducer()
method on the JMS session
object. This method takes a JMS destination as its sole parameter, unsurprisingly, in
the generated code the injected message queue is sent as a parameter to this method.