• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
coldcodeSJTU
博客园    首页    新随笔    联系   管理    订阅  订阅

RabbitMQ-记录一个问题及解决

我在配置文件中写入了
rabbitmq: host: 127.0.0.1 port: 5672 username: guest passport: guest virtualhost: / switchFlag: true pool_size: 10
然后希望实现一个监听器,所以加上了注解形式@RabbitListener(queues = CommonConstants.QUERE_NAME_NOTIFY),由于我工作环境原因,在不同电脑上coding(笔记本没有安装rabbitMQ),为了能否方便使用配置文件中的switchFlag进行切换,所以使用了@ConditionalOnProperty(value = "rabbitmq.switchFlag"),但是我今天心血来潮,想把direct的exchange切换成topic,由于之前的做法是`public void run(ApplicationArguments args) throws Exception {
String host = rabbitmqProperties.getHost();
Integer port = rabbitmqProperties.getPort();
String userName = rabbitmqProperties.getUsername();
String password = rabbitmqProperties.getPassport();
String virtualhost = rabbitmqProperties.getVirtualhost();
Integer poolSize = rabbitmqProperties.getPoolSize();

    RabbitmqConnectionPool.initRabbitmqConnectionPool(host, port, userName, password, virtualhost, poolSize);
    RabbitmqConnection connection = RabbitmqConnectionPool.getConnection();
    Channel channel = connection.getConnection().createChannel();
    // 声明exchange中的消息为可持久化,不自动删除
    // channel.exchangeDeclare(CommonConstants.EXCHANGE_NAME_DIRECT, BuiltinExchangeType.DIRECT, true, false, null);

    channel.exchangeDeclare(CommonConstants.EXCHANGE_NAME_TOPIC, BuiltinExchangeType.TOPIC, true, false, null);

    // 声明点赞的消息队列

// channel.queueDeclare(CommonConstants.QUERE_NAME_PRAISE, true, false, false, null);
// //绑定队列到交换机
// channel.queueBind(CommonConstants.QUERE_NAME_PRAISE, CommonConstants.EXCHANGE_NAME_DIRECT, CommonConstants.QUERE_KEY_PRAISE);
//
// // 声明收藏的消息队列
// channel.queueDeclare(CommonConstants.QUERE_NAME_COLLECT, true, false, false, null);
// //绑定队列到交换机
// channel.queueBind(CommonConstants.QUERE_NAME_COLLECT, CommonConstants.EXCHANGE_NAME_DIRECT, CommonConstants.QUERE_KEY_COLLECT);

    channel.queueDeclare(CommonConstants.QUERE_NAME_NOTIFY, true, false, false, null);
    channel.queueBind(CommonConstants.QUERE_NAME_NOTIFY, CommonConstants.EXCHANGE_NAME_TOPIC, CommonConstants.QUERE_KEY_NOTIFY);

    channel.close();`而这个的运行是在listen之后的,导致一直在报bug。最后解决办法是:先手动注释listen的注解,声明exchange和queue之后,再重新运行代码。
posted @ 2024-08-17 15:23  coldcodeDong  阅读(25)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3