如何在ActiveMQ中获取所有排队的消息? [英] How to get all enqueued messages in ActiveMQ?

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

问题描述

我想构建一个简单的使用者程序(使用Java),以将所有消息存储在ActiveMQ主题中。
我有一个生产者,它在队列中发送TextMessage。



但是我不知道如何开始写我的使用者来检索旧消息并等待



如果有一个例子,谢谢!



这是我的生产者: http://pastebin.com/uRy9D8mY



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



何时我先于消费者运行生产者,然后再运行消费者,但我一无所获。
当运行我的使用者然后是生产者时,我在队列中添加了72条消息,但是我的使用者仅获得24条消息...

解决方案

我建议阅读本教程(Apache ActiveMQ也是) SUN Jms教程



有许多方法可以使用Spring等各种框架或纯Java来编写JMS / ActiveMQ程序。 / p>

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

  public class MyListener实现MessageListener {
public void onMessage(Message message){
//在此处读取和处理消息。
}
}

由于您已经在生成消息,所以我认为您已经

  session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); 
Consumer = session.createConsumer( MyQueue);
listener = new MyListener();
Consumer.setMessageListener(listener);
connection.start();
//此时,消息应该从队列到达您的侦听器。

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


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.

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

This is my Consumer: 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...

解决方案

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

There are many ways to write JMS/ActiveMQ programs, using various frameworks such as Spring, or by using plain 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.

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

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