ActiveMQ:如何使较旧的消息出队? [英] ActiveMQ: how to dequeue older messages?

查看:148
本文介绍了ActiveMQ:如何使较旧的消息出队?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用ActiveMQ,现在我们面临以下问题.

I'm learning how to use ActiveMQ and now we are facing the following problem.

假设我在ActiveMQ上有一个名为topic.test的主题,该主题有两个订阅者. 在给定的时间内,我只有一个订阅者在等待消息,而生产者则针对我上面提到的主题发送消息.

Suppose that I have a topic named topic.test on ActiveMQ which have two subscribers. In a given moment, I have only one of those subscribers waiting for messages, and a producer send a message for the topic I mentioned above.

好吧,已连接的订户会收到消息,但其他订户在连接后是否不应该再收到该消息呢?好吧,就我而言,这没有发生:我的订户仅在连接时接收消息.在未连接时发送的所有其他消息都不会被它们接收.我可能做错了什么?

Ok, the connected subscriber get the message, but shouldn't the other subscriber receive that message later when it is connected? Well, in my case it's not happening: my subscribers are only receiving messages while connected. All the other messages, which were sent while they were not connected are not being received by them. What could I be doing wrong?

这是我为测试ActiveMQ编写的一些源代码.也许您可以找到问题所在.

Here is some of the source code I wrote to test ActiveMQ. Maybe you could find what is wrong with it.

我的消费者代码:

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        connection.setClientID("leitorTeste");
        conexao.start();
        Session sessao = conexao.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic fonte = sessao.createTopic("topic.test");
        MessageConsumer consumer = sessao.createConsumer(fonte);
        javax.jms.Message presente = null;
        while ((presente = consumer.receive()) != null) {
            System.out.println(((TextMessage) presente).getText());
        }
        consumer.setMessageListener(new LeitorMensagens());
        conexao.close();

这是我的生产者代码:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        Session sessao = conexao.createSession(true, Session.AUTO_ACKNOWLEDGE);
        connection.start();
        Destination destino = sessao.createTopic("topic.test");
        MessageProducer produtorMensagem = sessao.createProducer(destino);
        produtorMensagem.setDeliveryMode(DeliveryMode.PERSISTENT);
        TextMessage message = sessao.createTextMessage("Hi!");
        produtorMensagem.send(message);
        sessao.commit();
        connection.close();

我还应该添加其他配置到ActiveMQ,以便我的使用者获得较旧的消息吗?

Is there any other configuration I should add to ActiveMQ so that my consumers could get older messages?

推荐答案

您必须使消费者永久".否则,一旦取消订阅,AMQ就会忘记"他们.为此,请使用

You must make your consumers "permanent". Otherwise, AMQ "forgets" about them as soon as they unsubscribe. To do this, use Session.createDurableSubscriber()

这篇关于ActiveMQ:如何使较旧的消息出队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆