BlockedListener 是监听连接阻塞情况的监听器
这个监听器在服务器内存报警或者硬盘报警的情况下,使用发送接口会产生阻塞,如果发送和接受等使用同一个连接,则会都阻塞,因此我们应该为消费者和生产者使用不同的CachingConnectionFactory,或者设置rabbitTemplate.setUsePublisherConnection(true);
BlockedListener的代码
代码语言:javascript复制@Slf4j
@Service
public class RabbitBlockedListener implements BlockedListener {
@Override
public void handleBlocked(String s) throws IOException {
log.info("=========================connection blocked, reason: {}", s);
}
@Override
public void handleUnblocked() throws IOException {
log.info("==============================connection unblocked");
}
}
是在RabbitMQ connection连接上的时候设置进去的
代码语言:javascript复制@Slf4j
@Service
public class RabbitConnectionListener implements ConnectionListener {
@Autowired
private RabbitBlockedListener rabbitBlockedListener;
@Override
public void onCreate(Connection connection) {
log.info("================onCreate: {}", connection);
connection.addBlockedListener(rabbitBlockedListener);
}
@Override
public void onClose(Connection connection) {
log.info("================onClose: {}", connection);
}
@Override
public void onShutDown(ShutdownSignalException signal) {
log.info("================onShutDown: {}", signal);
}
}
正常情况下,RabbitMQ的状态是这样的
测试内存报警
RabbitMQ内存报警水位是由vm_memory_high_watermark来控制的
在RabbitMQ使用 rabbitmqctl set_vm_memory_high_watermark 0.01
- 设置内存报警的水位,当内存使用达到了该水位时,RabbitMQ将产生报警
- 可以通过rabbitmqctl status | grep vm_memory_high_watermark查看当前的水位设置,默认时0.4
此时查看报警时的RabbitMQ的状态
此时往RabbitMQ发送一条消息,将会收到BlockedListener的消息,且reason会提示具体的错误原因
代码语言:javascript复制2019-01-09 23:49:53.408 INFO 73408 --- [xxxxxxx] c.m.demo.listener.RabbitBlockedListener : =========================connection blocked, reason:low on memory
再将水位修改回来 rabbitmqctl set_vm_memory_high_watermark 0.4
查看此时的程序的打印
代码语言:javascript复制2019-01-09 23:51:40.386 INFO 73408 --- [xxxxxxx] c.m.demo.listener.RabbitBlockedListener : ==============================connection unblocked
2019-01-09 2:351:40.396 INFO 73408 --- [xxxxxxx] c.m.d.l.i.ConfirmCallbackListenerImpl : ConfirmCallbackListener thread: CorrelationData: null, ack: true, cause: null
2019-01-09 23:51:40.398 INFO 73408 --- [cTaskExecutor-4] com.mt.demo.rabbitmq.HelloListener2 : receive int msg: 30
测试磁盘报警
同样的修改磁盘的配置
rabbitmqctl set_disk_free_limit 48G
- 设置空闲磁盘的大小,空闲值小于该值时产生报警,默认时48M
- 也可以使用这个命令将磁盘和内存的大小关联起来设置 xx时磁盘和内存的配比 rabbitmqctl set_disk_free_limit mem_relative xx
此时查看RabbitMQ的控制台
此时往RabbitMQ发送一条消息,将会收到BlockedListener的消息,且reason会提示具体的错误原因
代码语言:javascript复制2019-01-09 23:58:17.318 INFO 49240 --- [xxxxxxx] c.m.demo.listener.RabbitBlockedListener : =========================connection blocked, reason:low on disk
再将磁盘限制修改回来
rabbitmqctl set_disk_free_limit 48M
代码语言:javascript复制2019-01-09 23:58:49.793 INFO 49240 --- [xxxxxxx] c.m.demo.listener.RabbitBlockedListener : ==============================connection unblocked
2019-01-09 23:58:49.801 INFO 49240 --- [xxxxxxx] c.m.d.l.i.ConfirmCallbackListenerImpl : ConfirmCallbackListener thread: CorrelationData: null, ack: true, cause: null
2019-01-09 23:58:49.806 INFO 49240 --- [cTaskExecutor-3] com.mt.demo.rabbitmq.HelloListener2 : receive int msg: 30
测试同时报警
同时设置内存和磁盘报警
发送消息到队列,则会在这个监听器产生报警信息。
代码语言:javascript复制2019-01-10 00:00:08.811 INFO 49240 --- [xxxxxxx] c.m.demo.listener.RabbitBlockedListener : =========================connection blocked, reason:low on disk & memory
需要同时解除内存和磁盘的报警才会收到unblock的消息
代码语言:javascript复制2019-01-10 00:04:05.764 INFO 49240 --- [xxxxxxx] c.m.demo.listener.RabbitBlockedListener : ==============================connection unblocked
2019-01-10 00:04:05.765 INFO 49240 --- [xxxxxxx] c.m.d.l.i.ConfirmCallbackListenerImpl : ConfirmCallbackListener thread: CorrelationData: null, ack: true, cause: null
2019-01-10 00:04:05.766 INFO 49240 --- [cTaskExecutor-2] com.mt.demo.rabbitmq.HelloListener2 : receive int msg: 30