ActiveMQ Consumer / Producer连接侦听器

时间:2009-11-09 18:16:04

标签: java jms activemq

我似乎无法在ActiveMQ(Java版本)中找到监听新的生产者和消费者连接(或连接中断)的方法。我希望能够告诉消费者(或者他们可以发现自己)生产者的连接被丢弃了。另一种方式(生产者发现某个消费者断开连接)也是必需的。

我很感激一些帮助。

2 个答案:

答案 0 :(得分:4)

我认为您希望在特定目的地(特定队列或主题)上收听新的制作人和消费者。是吗?

您可以实例化ConsumerEventSource和ProducerEventSource,并通过分别调用它们的setConsumerListener和setProducerListener来注册您自己的侦听器。

所以:

Connection conn = yourconnection; // the connection your listener will use
Destination dest = yourdestination; // the destination you're paying attention to
ConsumerEventSource source = new ConsumerEventSource(conn, dest);
source.setConsumerListener(new ConsumerListener() {

   public void onConsumerEvent(ConsumerEvent event) {
      if (event.isStarted()) {
         System.out.println("a new consumer has started - " + event.getConsumerId());
      } else {
         System.out.println("a consumer has dropped - " + event.getConsumerId());
      }
   }

});

如果你看一下ConsumerEventSource或ProducerEventSource的代码,你会发现它们是简单的对象,它们使用AdvisorySupport的方法来监听一个特殊的咨询主题,该主题的业务是广播有关生产者和消费者的新闻。您可以通过阅读这些类的源代码来了解更多信息。

您使用“连接”可能是个问题;在ActiveMQ land(它是JMS land的子集)中,“Connection”是与特定目标无关的低级对象。特定客户端从Connection创建“会话” - 仍然不是特定于目标 - 然后创建特定于目标的QueueSender,QueueReceiver,TopicPublisher或TopicSubscriber。当创建它们时,或者创建它们的会话消失时,那些是您想要听到的事件,并且会听到您是否使用上面的代码。

答案 1 :(得分:2)

我需要的所有信息都发布在ActiveMQ Advisory主题中,例如“ActiveMQ.Advisory.Connection”或简称为“ActiveMQ.Advisory ..>”对于他们所有人。甚至在Stomp Connection中发生的事件也会在ActiveMQ Advisory主题中发布。以下代码给出了一个示例(使用通过Stomp连接的Flex Client进行测试):

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("user", "password", ActiveMQConnection.DEFAULT_BROKER_URL);
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(transacted, ackMode);
connection.start();
Destination destinationAdvisory = session.createTopic("ActiveMQ.Advisory..>");
MessageConsumer consumerAdvisory = session.createConsumer(destinationAdvisory);
consumerAdvisory.setMessageListener(new MessageListener() {
    public void onMessage(Message message) {

            if (message instanceof ActiveMQMessage) {
                ActiveMQMessage activeMessage = (ActiveMQMessage) message;
                Object command = activeMessage.getDataStructure();
                if (command instanceof ConsumerInfo) {
                    System.out.println("A consumer subscribed to a topic or queue: " + command);
                } else if (command instanceof RemoveInfo) {
                    RemoveInfo removeInfo = (RemoveInfo) command;
                    if (removeInfo.isConsumerRemove()) {
                        System.out.println("A consumer unsubscribed from a topic or queue");
                    }
                    else {
                        System.out.println("RemoveInfo, a connection was closed: " + command);
                    }
                } else if (command instanceof ConnectionInfo) {
                    System.out.println("ConnectionInfo, a new connection was made: " + command);
                } else {
                    System.out.println("Unknown command: " + command);
                }
            }
    }
});
相关问题