Java Reference
In-Depth Information
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
acknowledge 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.
The last thing this method does is to actually send the message to the message
queue. This is done by invoking the
send()
method on the
javax.jms.
MessageProducer
instance obtained in the previous line. This method takes an
instance of a class implementing
javax.jms.Message
or one of its subinterfaces
as a parameter. In the generated code, the generated method to create the message
(
createJMSMessageForjmsMyQueue()
in our example) is invoked inline, since this
method's return value is of the appropriate type.