Java Reference
In-Depth Information
Autocloseable Resources
The JMS API has been completely re-worked to make use of the Java 7 autocloseable API. If you are familiar with
AutoCloseable , then you know that it allows for better resource handing in a simpler manner. When combined with
the multi-catch implementation that was introduced in Java SE 7, code can become terse, easier to read, and more
robust. In the following example, a message browser is used to iterate through the messages that are contained in
a queue using the standard API. Note that the Connection , Session , and QueueBrowser instances do not have to be
closed since they are contained within the try -clause. Encapsulating AutoCloseable instances within the try -clause
ensures that they will be closed when they are no longer needed.
public void browseMessages() {
try(Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
QueueBrowser browser = session.createBrowser(inboundQueue);) {
Enumeration msgs = browser.getEnumeration();
if(!msgs.hasMoreElements()){
System.out.println("No more messages within the queue...");
} else {
while(msgs.hasMoreElements()){
Message currMsg = (Message)msgs.nextElement();
System.out.println("Message ID: " + currMsg.getJMSMessageID());
}
}
} catch (JMSException ex) {
System.out.println(ex);
}
}
Message Body
A new getBody method has been added to the javax.jms.Message object to allow for easier access to the body
of a message. The getBody method accepts one parameter that is the class type of the message body. For instance,
to obtain the body of a message that is of the String type, make a call to the method, as follows:
...
message.getBody(String.class)
...
New Messaging Features
The JMS 2.0 update includes some important new messaging features that extend upon the pre-2.0 functionality, and
help bring JMS in alignment with more modern technologies. Two of the biggest messaging additions include delivery
delay of messages, and the ability to send asynchronous messages. This section will briefly touch upon each of these
new features.
 
Search WWH ::




Custom Search