RabbitMQ学习之队列监控

本文介绍了RabbitMQ的监控方法,包括使用Java API查询队列中未消费的消息数及消费者数量,通过连接状态变化监控RabbitMQ的工作状态,并提供了一个Java实例。

对于RabbitMQ的监控,除了服务器基本信息(硬盘、CPU、内存、IO等)以及MQ的进程和端口,我们也可以通过请求url访问管理API监控其集群和队列的情况。在java api 3.6.0以后,channel接口为我们提供了如下接口:

/**
 * Returns the number of messages in a queue ready to be delivered
 * to consumers. This method assumes the queue exists. If it doesn't,
 * an exception will be closed with an exception.
 * @param queue the name of the queue
 * @return the number of messages in ready state
 * @throws IOException Problem transmitting method.
 */
    long messageCount(String queue) throws IOException;

  /**
   * Returns the number of consumers on a queue.
   * This method assumes the queue exists. If it doesn't,
   * an exception will be closed with an exception.
   * @param queue the name of the queue
   * @return the number of consumers
   * @throws IOException Problem transmitting method.
   */
    long consumerCount(String queue) throws IOException;

messageCount:查询队列未消费的消息数,可以监控消息堆积的情况。
consumerCount:队列的消费者个数,可以对消费者进行监控
java实例:

public class JavaAPIMonitor {

    private static String queue_name = "test_queue";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // amqp://userName:password@hostName:portNumber/virtualHost
        factory.setUri("amqp://test:test@10.1.199.169:5672/test");
        Connection conn = factory.newConnection();
        // System.out.println(conn.getChannelMax());
        Channel channel = conn.createChannel();
        // System.out.println(channel.getChannelNumber());
        System.out.println(channel.messageCount(queue_name));
        System.out.println(channel.consumerCount(queue_name));
        channel.close();
        conn.close();
    }
}
运行结果:
277782
3

除此之外,也可以通过连接的状态变化,进行监控,如:连接被阻塞(内存,CPU等不足)

ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        connection.addBlockedListener(new BlockedListener() {

            public void handleUnblocked() throws IOException {
                // 连接取消阻塞
            }
            public void handleBlocked(String reason) throws IOException {
                // 连接阻塞
            }
        });
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
                //连接关闭
            }
        });

先关监控参考文章:
1.RabbitMQ之管理与监控
2.REST API监控RabbitMQ
3.如何监控RabbitMQ
4.使用AMQP模拟检测来确认RabbitMQ是否运行
5.监控队列状态

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值