RabbitMQ学习之队列监控

对于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:[email protected]: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.监控队列状态

猜你喜欢

转载自blog.csdn.net/tianwei7518/article/details/53982640
今日推荐