rabbitmq查看队列中消息数量的方法

  1. 配置rabbitmq
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit.xsd" >

	<description>rabbitmq 连接服务配置</description>

	<!-- 连接配置 publisherConfirms 发布的消息是否确定-->
	<bean id="rabbitMqConnectionFactory"
		  class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
		<property name="addresses" value="${rabbitmq.addresses}"/>
		<property name="username" value="${rabbitmq.username}"/>
		<property name="password" value="${rabbitmq.password}"/>
		<property name="virtualHost" value="${rabbitmq.vhost}"/>
		<property name="channelCacheSize" value="${rabbitmq.channelCacheSize}"/>
	</bean>


	<!-- spring template声明 -->
	<rabbit:template id="amqpTemplate" connection-factory="rabbitMqConnectionFactory"
	/>


</beans>
  1. 工具类实现
/**
 * 监控rabbitmq的工具类
 */
@Slf4j
public class RabbitMqUtil{
    private static RabbitTemplate amqpTemplate;

    /**
     * 初始化amqpTemplate
     */
    static {
        amqpTemplate = SpringContextUtil.getBean(RabbitTemplate.class);
        Assert.notNull(amqpTemplate,"获取不到amqpTemplate");
    }

    /**
     * 查询队列中的消息数量
     * @param exchange 交换机名字
     * @param exchangeType 交换机类型 fanout 或 direct
     * @param quene 队列名字
     * @return
     */
    public static BaseResultDTO getMessageCount(String exchange, String exchangeType, String quene) {
        Assert.hasText(exchange,"exchange不能为空");
        Assert.hasText(quene,"队列名不能为空");
        Assert.hasText(exchangeType,"exchangeType不能为空");
        checkInitSuccess();


        BaseResultDTO baseResultDTO = new BaseResultDTO();
        ConnectionFactory connectionFactory = amqpTemplate.getConnectionFactory();
        // 创建连接
        Connection connection = connectionFactory.createConnection();
        // 创建通道
        Channel channel = connection.createChannel(false);
        // 设置消息交换机
        try {
            channel.exchangeDeclare(exchange, exchangeType, true, false, null);
            AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(quene);
            //获取队列中的消息个数
            Integer queueCount = declareOk.getMessageCount();
            // 关闭通道和连接
            channel.close();
            connection.close();

            baseResultDTO.setSuccess(true);
            baseResultDTO.setMessage(queueCount.toString());
        } catch (IOException e) {
            LoggerFormatUtil.warn(e,log,"连接rabbitmq异常");
        }


        return baseResultDTO;
    }


    /**
     * 校验amqpTemplate是否已经实例化
     * @return
     */
    private static void checkInitSuccess(){
        if (null == amqpTemplate){
            amqpTemplate = SpringContextUtil.getBean(RabbitTemplate.class);
        }
        Assert.notNull(amqpTemplate, "amqpTemplate 实例化异常");
    }
}

  1. spring辅助工具类获取bean
@Component
public class SpringContextUtil implements ApplicationContextAware {
    public static ApplicationContext applicationContext = null;

    public SpringContextUtil() {
    }

    @SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName, Class<T> beanType) {
        Assert.isTrue(applicationContext != null, "应用上下文不能为空");
        Object bean = applicationContext.getBean(beanName);
        return bean == null ? null : (T)bean;
    }

    /**
     * 按类型获取bean
     * @param beanType
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> beanType){
        Assert.isTrue(applicationContext != null, "应用上下文不能为空");
        T bean = applicationContext.getBean(beanType);
        return bean == null ? null : bean;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值