簡體   English   中英

ActiveMQ:如何將舊郵件出列?

[英]ActiveMQ: how to dequeue older messages?

我正在學習如何使用ActiveMQ,現在我們面臨以下問題。

假設我在ActiveMQ上有一個名為topic.test的主題,該主題有兩個訂閱者。 在給定的時刻,我只有其中一個訂閱者等待消息,並且生產者發送上面提到的主題的消息。

好的,連接的用戶收到消息,但是其他用戶在連接時不應該收到該消息? 好吧,就我而言,它沒有發生:我的訂閱者只在連接時收到消息。 所有其他消息在他們未連接時發送的消息都沒有被他們接收。 我能做錯什么?

這是我為測試ActiveMQ而編寫的一些源代碼。 也許你可以找到它的錯誤。

我的完整代碼:

        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,以便我的消費者可以收到舊消息?

你必須讓你的消費者“永久”。 否則,AMQ會在取消訂閱后立即“忘記”他們。 為此,請使用Session.createDurableSubscriber()

您還可以在代理上設置一種稱為追溯消費者政策的內容。 這是針對主題訂閱者 - 這些訂閱者不耐用,但可能希望收到他們可能錯過的“最近”消息 - 另請參閱訂閱恢復策略

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM