检测ActiveMQ主题的使用者变化 [英] Detect change in Consumers of an ActiveMQ topic

查看:204
本文介绍了检测ActiveMQ主题的使用者变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆tomcat,它们使用ActiveMQ主题中的消息.现在,如果集群中的tomcat之一发生故障,那么我猜认为该用户数量将下降1.

I have a cluster of tomcats which consume messages from an ActiveMQ topic. Now, if one of the tomcat in the cluster goes down then i am guessing that number of consumers would go down by 1.

现在,我想使用有关该主题的一些回调或侦听器来检测该更改.那可行吗?

Now, i would like to detect that change using some callback or listener on that topic. Is that feasible ?

诸如:Region.getDestinations(ActiveMQDestination)会起作用吗?

Will something like : Region.getDestinations(ActiveMQDestination) work ?

推荐答案

咨询消息是您所需要的.

Advisory Message is what you need.

每次收到包含此代码的消息时,这表示您开始或停止消费.

each time you got a message with this code this means you have consumer starting or stopping.

doc http://activemq.apache.org/advisory-message.html

示例:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ConsumerInfo;
import org.apache.activemq.command.RemoveInfo;

public class AdvisorySupportConsumerAdvisoryTopic {

    public static void main(String[] args) throws JMSException {
        Connection conn = null;
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory("auto://localhost:5671");
            conn = cf.createConnection("admin", "admin");
            ActiveMQSession session = (ActiveMQSession) conn.createSession(false,
                    ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
            conn.start();
            Queue q = session.createQueue("Q");
            Destination advisoryDestination = org.apache.activemq.advisory.AdvisorySupport.getConsumerAdvisoryTopic(q);
            MessageConsumer consumer = session.createConsumer(advisoryDestination);
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message msg) {
                    if (msg instanceof ActiveMQMessage) {
                        try {
                            ActiveMQMessage aMsg = (ActiveMQMessage) msg;
                            System.out.println(aMsg.getStringProperty("consumerCount"));
                            System.out.println(aMsg.getStringProperty("producerCount"));
                            if (aMsg.getDataStructure() instanceof ConsumerInfo) {
                                // Consumer start
                                ConsumerInfo consumerInfo = (ConsumerInfo) aMsg.getDataStructure();
                                System.out.println(consumerInfo);
                            } else if (aMsg.getDataStructure() instanceof RemoveInfo) {
                                // Consumer stop
                                RemoveInfo removeInfo = (RemoveInfo) aMsg.getDataStructure();
                                System.out.println(removeInfo);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

每次收到带有此代码的消息时,这表示您已连接开始或停止.

each time you got a message with this code this means you have Connection starting or stopping.

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ConnectionInfo;
import org.apache.activemq.command.RemoveInfo;

public class AdvisorySupportConnectionAdvisoryTopic {

    public static void main(String[] args) throws JMSException {
        Connection conn = null;
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory("auto://localhost:5671");
            conn = cf.createConnection("admin", "admin");
            ActiveMQSession session = (ActiveMQSession) conn.createSession(false,
                    ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
            conn.start();
            Destination advisoryDestination = org.apache.activemq.advisory.AdvisorySupport.getConnectionAdvisoryTopic();
            MessageConsumer consumer = session.createConsumer(advisoryDestination);
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message msg) {
                    if (msg instanceof ActiveMQMessage) {
                        try {
                            ActiveMQMessage aMsg = (ActiveMQMessage) msg;
                            if (aMsg.getDataStructure() instanceof ConnectionInfo) {
                                // Connection start
                                ConnectionInfo connectionInfo = (ConnectionInfo) aMsg.getDataStructure();
                                System.out.println(connectionInfo);
                            } else if (aMsg.getDataStructure() instanceof RemoveInfo) {
                                // Connection stop
                                RemoveInfo removeInfo = (RemoveInfo) aMsg.getDataStructure();
                                System.out.println(removeInfo);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

这篇关于检测ActiveMQ主题的使用者变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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