Java Reference
In-Depth Information
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.
Notice that most of the body of the generated method to send JMS messages is
enclosed in a
try
/
finally
block. Most of the lines inside the
try
block have the
potential of throwing a
JMSException
, if this happens, the code attempts to close the
JMS session and connection, which is the exact same thing that needs to be done if the
code ends normally, therefore it makes sense to put this code in the
finally
block.
Although it is possible to write standalone applications that can retrieve messages
from a messaging destination, most Java EE applications rely on message driven beans
for this task, and NetBeans makes it very easy to generate message driven beans.
Processing JMS messages with message
driven Beans
In order to create a message driven bean, we need to right-click on our EJB project
and select
File | New
, then select the
Enterprise JavaBeans
category and the
Message-Driven Bean
file type.