从JBoss JMS队列中检索消息

时间:2016-07-08 12:36:44

标签: java jboss jms

如何检索JMS队列中的消息?我正在寻找这个解决方案:Jboss Messaging JMS,但这只是立即发送和接收。如果服务器重新启动,我该如何使用Java代码检索消息?

我在网上找不到任何东西;当然,除非我完全误解了JMS队列的工作原理。

3 个答案:

答案 0 :(得分:1)

当前版本的JBoss使用HornetQ进行JMS消息传递。

如果您正在使用Java JMS客户端代码并附加到群集消息传递代理实例,则客户端应该故障转移到另一个节点。

在单实例代理配置中,您将在客户端代码中获得JMS异常。这意味着您需要获得新连接并开始新会话。

浏览队列:

/**
 * 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);
            }
        }
    }
}

jnp协议和端口用于JBoss Messaging。

答案 1 :(得分:1)

根据您的使用情况,您可以使用JMSToolBox之类的工具来浏览/发布/删除等.JBoss / HornetQ目的地的内容

答案 2 :(得分:0)

您必须制作QueueBrowser并通过它们进行枚举。示例代码:

// 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());
}

由于这仅是一个示例,您可以更改TextMessage部分以满足您的需求。