Reputation: 807
How do I retrieve a message that is in the JMS Queue? I was looking at this solution: Jboss Messaging JMS, but that just sends and receives right away. If the server restarted, how can I retrieve the message with Java code?
I haven't found anything online; unless, of course, I totally misunderstood how JMS Queues work.
Upvotes: 1
Views: 2647
Reputation: 1045
The current version of JBoss uses HornetQ for JMS messaging.
If you are using Java JMS client code your are and attaching to a clustered messaging broker instance, your client should failover to another node.
In a single instance broker configuration, you will get a JMS exception in the client code. This means that you will need to get a new connection and start a new session.
For browsing a queue:
/**
* QueueBrowserGss.java
*
* Created on Sep 24, 2012, 3:52:28 PM
*
* To the extent possible under law, Red Hat, Inc. has dedicated all copyright to this
* software to the public domain worldwide, pursuant to the CC0 Public Domain Dedication. This
* software is distributed without any warranty.
*
* See <http://creativecommons.org/publicdomain/zero/1.0/>.
*
*/
package com.redhat.gss.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author grovedc
*
*/
public class QueueBrowserGss {
private static final Log logger = LogFactory.getLog(QueueBrowserGss.class);
@SuppressWarnings("unchecked")
public static void main(String[] args) {
QueueConnection queueConnection = null;
Queue queue = null;
QueueConnectionFactory queueConnFactory = null;
QueueBrowser queueBrowser = null;
QueueSession queueSession = null;
List<Object> messages = new ArrayList<Object>(7000);
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
// properties.put(Context.PROVIDER_URL, "jnp://" + server + ":" + port);
properties.put("Context.PROVIDER_URL", "jnp://10.0.0.150:1100");
try {
InitialContext ctx = new InitialContext(properties);
queue = (Queue) ctx.lookup("queue/D");
queueConnFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
queueConnection = queueConnFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueBrowser = queueSession.createBrowser(queue);
queueConnection.start();
for (Enumeration<Object> e = queueBrowser.getEnumeration(); e.hasMoreElements();) {
messages.add(e.nextElement());
}
logger.info("Messages are retrieved from queue. Process completed on: " + new Date());
logger.info("Number of Messages present: " + messages.size());
} catch (Exception ex) {
logger.error(String.format("Exception Occured : %s", ex.getMessage()), ex);
} finally {
try {
if (queueConnection != null)
queueConnection.stop();
} catch (JMSException e) {
logger.error(e);
}
}
}
}
The jnp protocol and ports are for JBoss Messaging.
Upvotes: 2
Reputation: 2987
Depending on your use-case, you can use a tool like JMSToolBox to browse/post/delete etc.. the content of JBoss/HornetQ destinations
Upvotes: 1
Reputation: 807
You have to make a QueueBrowser
and enumerate through them. Example code:
// Create the connection
InitialContext context = new InitialContext(properties);
QueueConnectionFactory queueConnFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
QueueConnection conn = queueConnFactory.createQueueConnection();
Queue queue = (Queue) context.lookup("/queue/Test");
QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
// Start the connection
conn.start()
// Create a QueueBrowser using the session
QueueBrowser queueBrowser = session.createBrowser(queue);
Enumeration e = queueBrowser.getEnumeration();
// Iterate through the queue
while(e.hasMoreElements()) {
Message msg = (Message) e.nextElement();
TextMessage txtMsg = (TextMessage) msg;
System.out.println(txtMsg.getText());
}
As this is just an example, you can change the TextMessage
part to suit your needs.
Upvotes: 0