by
0 16

How to create an asynchronous MessageListener in JMS

This code is an example of Java Message Service message listener implementation.
It shows how to create an asynchronous MessageListener, in order to receive message asynchronously as they are delivered to the message consumer.
The client needs to create a message listener that implements the MessageListener interface.

The MessageListener is asynchronously notified whenever a message has been 
published to the Queue. This is done via the onMessage method in the 
MessageListener interface.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/42220/How-to-create-an-asynchronous-MessageLis?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
//Import the JMS API classes. import javax.jms.*; public class HelloWorldMessage implements MessageListener { public void onMessage(Message message) { /* Unpack and handle the messages received */ String newStockData = null; /* Unpack and handle the messages received */ try { newStockData = ((TextMessage) message).getText(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (newStockData != null) { /* Logic related to the data */ } } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus