Java Reference
In-Depth Information
public void onMessage(Message message) {
} }
In the generated code, all we need to do is implement the body of the onMessage()
method, and deploy our project. The onMessage() method will process any mes-
sages on the JMS destination our message-driven bean is receiving messages from.
We can write any arbitrary code in the onMessage() method, the possibilities are
endless, however, this method is typically used to save data from the message into a
database, or to write some output into a log. In our example, we will simply send the
contents of the message to the stdout log of our application server.
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Received message:" +
} catch (JMSException ex) {
Level.SEVERE, null, ex);
} }
Notice that we had to cast the message parameter to the actual subinterface that was
sent to the message destination, which in our case is javax.jms.TextMessage . To
obtain the message contents, we invoked the getText() method of TextMessage ,
this method throws JMSException , because of this, we had to wrap its invocation in
a try / catch block.
NetBeans will remind us that we need to catch JMSException by
underlining the offending code with a wiggly red line, by hitting
Alt+Enter at the offending line we can have NetBeans generate the try /
catch block automatically.
Search WWH ::

Custom Search