gpt4 book ai didi

java - 从 JBoss JMS 队列检索消息

转载 作者:行者123 更新时间:2023-12-02 03:29:48 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

在单实例代理配置中,您将在客户端代码中收到 JMS 异常。这意味着您将需要获得新连接并启动新 session 。

浏览队列:

/**
* 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 协议(protocol)和端口用于 JBoss Messaging。

关于java - 从 JBoss JMS 队列检索消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com