在c# . net中访问activemq统计插件

本文关键字:activemq 统计 插件 访问 net | 更新日期: 2023-09-27 18:14:55

我正在尝试访问c#中的activemq统计数据,以以下示例为例:

http://activemq.apache.org/statisticsplugin.html

我尝试了很多方法,但我收到的MapMessage是空的或抛出异常,如"不能将AMQMessage转换为MapMessage"

这是我的班级:

public class Statistics
{
    private readonly IConnectionFactory connectionFactory;
    private readonly IConnection connection;
    private readonly ISession session;
    public Statistics( string brokerUri)
    {
        this.connectionFactory = new ConnectionFactory(brokerUri);
        this.connection = connectionFactory.CreateConnection();
        this.connection.Start();
        this.session = connection.CreateSession();
    }
    public void GetStats()
    {
        // Crear consumidor

        try
        {
            // Creo una cola y consumidor
            IDestination queueReplyTo = session.CreateTemporaryQueue();
            IMessageConsumer consumer = session.CreateConsumer(queueReplyTo);

            // Crear cola  y productor
            ActiveMQQueue newQueue = new ActiveMQQueue("ActiveMQ.Statistics.Destination.TEST1");
            IMessageProducer producer = session.CreateProducer(null);
            // Crear cola monitorizada
            string listeningQueue = "TEST1";
            ActiveMQQueue query = new ActiveMQQueue(listeningQueue);
            // Mandar mensaje vacío y replicar
            IMessage msg = session.CreateMessage();
            producer.Send(newQueue, msg);
            msg.NMSReplyTo = queueReplyTo;
            producer.Send(query, msg);
            // Recibir
            IMapMessage reply = (IMapMessage)consumer.ReceiveNoWait();

            if (reply != null)
            {
                IPrimitiveMap statsMap = reply.Body;
                foreach (string statKey in statsMap.Keys)
                {
                    Console.WriteLine("{0} = {1}", statKey, statsMap[statKey]);
                }
            }
        }
        catch (Exception e)
        {
            var t = e.Message + " " + e.StackTrace;
        }
    }

}

我的错误在哪里?提前感谢

在c# . net中访问activemq统计插件

你可以试试这个吗:

public class Statistics
{
    private readonly IConnectionFactory connectionFactory;
    private readonly IConnection connection;
    private readonly ISession session;
    public Statistics( string brokerUri)
    {
        this.connectionFactory = new ConnectionFactory(brokerUri);
        this.connection = connectionFactory.CreateConnection();
        this.connection.Start();
        this.session = connection.CreateSession();
    }
    public void GetStats()
    {
        // Crear consumidor

        try
        {
            // Creo una cola y consumidor
            IDestination queueReplyTo = session.CreateTemporaryQueue();
            IMessageConsumer consumer = session.CreateConsumer(queueReplyTo);
            // Crear cola monitorizada
            string listeningQueue = "TEST1";
            ActiveMQQueue testQueue  =  session.GetQueue(listeningQueue);

            // Crear cola  y productor
            ActiveMQQueue query =  session.GetQueue("ActiveMQ.Statistics.Destination.TEST1");
            IMessageProducer producer = session.CreateProducer(null);
            // Mandar mensaje vacío y replicar
            IMessage msg = session.CreateMessage();
            producer.Send(testQueue, msg);
            msg.NMSReplyTo = queueReplyTo;
            producer.Send(query, msg);
            // Recibir
            IMapMessage reply = (IMapMessage)consumer.Receive();

            if (reply != null)
            {
                IPrimitiveMap statsMap = reply.Body;
                foreach (string statKey in statsMap.Keys)
                {
                    Console.WriteLine("{0} = {1}", statKey, statsMap[statKey]);
                }
            }
        }
        catch (Exception e)
        {
            var t = e.Message + " " + e.StackTrace;
        }
    }

}