java 如何在 ActiveMQ 中获取所有排队的消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10795220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:35:56  来源:igfitidea点击:

How to get all enqueued messages in ActiveMQ?

javaactivemq

提问by kdelemme

I want to build a simple consumer program (in java) to get all messages stocked in an ActiveMQ subject. I have a producer which send TextMessage in the queue.

我想构建一个简单的消费者程序(在 Java 中)来获取存储在 ActiveMQ 主题中的所有消息。我有一个生产者在队列中发送 TextMessage。

But I don't know how to start to write my consumer for retrieving old messages and wait for new one.

但我不知道如何开始编写我的消费者以检索旧消息并等待新消息。

If you have an example, thanks!

如果有例子,谢谢!

This is my Producer: http://pastebin.com/uRy9D8mY

这是我的制作人:http: //pastebin.com/uRy9D8mY

This is my Consumer: http://pastebin.com/bZh4r66e

这是我的消费者:http: //pastebin.com/bZh4r66e

When I run my producer before my consumer, then run the consumer, I got nothing. When I run my consumer then my producer, I add 72 messages in the queue but my consumer got only 24 message...

当我在我的消费者之前运行我的生产者,然后运行消费者时,我什么也没得到。当我运行我的消费者然后我的生产者时,我在队列中添加了 72 条消息,但我的消费者只收到了 24 条消息......

回答by Petter Nordlander

I suggest reading this tutorial (as does Apache ActiveMQ) SUN Jms tutorial

我建议阅读本教程(和 Apache ActiveMQ 一样)SUN Jms 教程

There are many ways to write JMS/ActiveMQ programs, using various frameworks such as Spring, or by using plain java.

编写 JMS/ActiveMQ 程序的方法有很多种,可以使用 Spring 等各种框架,也可以使用普通的 java。

Essentially, write a listener class like this:

本质上,编写一个这样的监听器类:

public class MyListener implements MessageListener{
   public void onMessage(Message message){
      // Read and handle message here.
   }
}

Since you already are producing message, I assume you have connection up and running.

由于您已经在生成消息,因此我假设您已经建立并运行了连接。

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer("MyQueue");
listener = new MyListener ();
consumer.setMessageListener(listener);
connection.start(); 
// At this point, messages should arrive from the queue to your listener.

Then there are some error handling code not included in this example, but you should be able to figure it out with the help of the tutorial and JMS documentation.

然后还有一些错误处理代码未包含在此示例中,但您应该能够在教程和 JMS 文档的帮助下弄清楚。

回答by Karthikeyan KR

Using the code given below you can read all the messages en-queued in the queue.

使用下面给出的代码,您可以读取队列中排队的所有消息。

  • In this code the while loop is an unending loop it will iterate all the messages in the queue.
  • Once there is no messages in the queue it will wait for 5 seconds then automatically stops the connection and breaks the loop.
  • 在这段代码中,while 循环是一个无休止的循环,它将迭代队列中的所有消息。
  • 一旦队列中没有消息,它将等待 5 秒钟,然后自动停止连接并中断循环。

If you required an unending consumer which will read all the messages whenever newly added to the queue, then remove the else part, so the program will not terminate.

如果您需要一个无休止的消费者,它会在新添加到队列时读取所有消息,然后删除 else 部分,这样程序就不会终止。

    ConnectionFactory factory =  new ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection con = factory.createConnection();
    Session session =   con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("tmp_queue2");
    MessageConsumer consumer = session.createConsumer(queue);
    con.start();     
    while (true) {     
        Message msg = consumer.receive(5000); 
        if (msg instanceof TextMessage) {
            TextMessage tm = (TextMessage) msg;
            System.out.println(tm.getText());       
        }
        else{
            System.out.println("Queue Empty"); 
            con.stop();
            break;
        }
    }

Hope this consumer program will helps people who were new to ActiveMQ.

希望这个消费者程序能帮助那些刚接触 ActiveMQ 的人。