如何访问.net中的activemq statistics插件

时间:2013-04-01 15:33:01

标签: activemq nms

我正在尝试访问c#

中的activemq统计信息http://activemq.apache.org/statisticsplugin.html

这是我到目前为止所拥有的。我无法得到消费者的回复。我可以在监视器网站中为队列增加计数。

public class Statistics 
{
    private readonly string queueName = string.Empty;
    private readonly string queueToMonitor = string.Empty; 
    private readonly IConnectionFactory connectionFactory;
    private readonly IConnection connection;
    private readonly ISession session;
    private readonly IMessageProducer producer;
    private readonly ActiveMQQueue queue;


    public Statistics(string qName, string brokerUri, string queueToMon)
    {
        this.queueName = qName;
        this.queueToMonitor = "ActiveMQ.Statistics.Destination." + queueToMon; 
        this.connectionFactory = new ConnectionFactory(brokerUri);
        this.connection = connectionFactory.CreateConnection();
        this.connection.Start();
        this.session = connection.CreateSession();

        queue = new ActiveMQQueue(qName);
        producer = session.CreateProducer(queue);

    }

    public void GetStats()
    {
        try
        {
            var statusQueue = session.CreateTemporaryQueue();
            var consumer = session.CreateConsumer(statusQueue);
            ActiveMQQueue query = new ActiveMQQueue(queueToMonitor);

            var msg = session.CreateMessage();
            msg.NMSReplyTo = statusQueue;
            producer.Send(queue, msg);

            var reply = (ActiveMQMapMessage)consumer.Receive();

            if (reply != null)
            {
                var test = reply.Content.ToString(); 
            }
         }
        catch (Exception e)
        {
            var t = e.Message + " " + e.StackTrace;
        }

    }
}

1 个答案:

答案 0 :(得分:3)

您正在将消息发送到错误的队列。您需要将消息发送到 ActiveMQ.Statistics.Destination.QueueToMonitor 目的地。我重写了你的GetStats()函数来证明它是有效的。关键变化是生产者将消息发送到哪个目的地。

public void GetStats()
{
    try
    {
        IDestination statusQueue = session.CreateTemporaryQueue();
        IMessageConsumer consumer = session.CreateConsumer(statusQueue);
        IDestination query = session.GetQueue(queueToMonitor);
        IMessage msg = session.CreateMessage();
        IMessageProducer producer = session.CreateProducer(query);

        msg.NMSReplyTo = statusQueue;
        producer.Send(msg);

        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;
    }
}