SpringBoot集成Rabbit使用TopicRabbit指定发送集合

Rabbitmq中绑定

exchange:flow

routing-key:user

bind-queue:flow_user

白话文就是,把user绑定到flow_user序列

发送方使用routing-key推送:

//把routing-key发送给名为flow的exchenge,然后exchenge负责向绑定的这个Queue推送
 amqpTemplate.convertAndSend("flow","user", context);

Rabbit配置

  • 添加exchange(这里类型type应该是topic,截图时候没有注意)
添加exchange
添加exchange
  • 添加Queue
添加Queue
添加Queue
  • 添加这个User 到exchange(注意routing-key)

SpringBoot集成Rabbitmq

  • 注册配置bean
@Configurable
public class TopicRabbitConfig {
    public final static String FLOW = "flow";
    
    public final static String USER = "user";
    public final static String USER_QUEUE = "flow_user";

   
    @Bean
    public Queue queueMessages3() {
        return new Queue(USER_QUEUE);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(FLOW);
    }
    @Bean
    Binding bindingExchangeMessages3(Queue queueMessages3, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages3).to(exchange).with(FLOW);
    }
}
  • 发送方代码
/**
 * @Package: pterosaur.account.service.impl
 * @Description: 模拟发送消息,测试使用
 * @author: liuxin
 * @date: 17/4/19 下午3:17
 */
@Component
public class AccountSentImpl {
    @Autowired
    private AmqpTemplate amqpTemplate;

    private ExecutorService threadPool = Executors.newFixedThreadPool(8);

    public void send() {
       for (int i=0;i<10;i++){
           String context = "hello :" + DateUtil.formatDatetime(System.currentTimeMillis())+",当前线程:"+Thread.currentThread().getName();
           System.out.println("Sender : " + context);
           threadPool.execute(new Runnable() {
               @Override
               public void run() {
                   amqpTemplate.convertAndSend(TopicRabbitConfig.FLOW,TopicRabbitConfig.USER, context);
               }
           });
       }
    }

}
  • 接受方代码
/**
 * @Package: pterosaur.account.service.impl
 * @Description: mq信息处理实现类
 * @author: liuxin
 * @date: 17/4/19 下午2:55
 */
@Component
public class AccountReceiverImpl implements AccountReceiver {
    private static final Logger logger = LoggerFactory.getLogger(AccountReceiverImpl.class);

    @Autowired
    ExecutorService threadPool;


    /**
     * 用户流水
     *
     * @param message
     */
    @RabbitListener(queues = TopicRabbitConfig.USER_QUEUE)
    @RabbitHandler
    public void processUser(String message) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                logger.info("用户侧流水:{}",message);
            }
        });
    }


}

  • 测试代码
Sender : hello :2017-04-25 17:44:15,当前线程:main
Sender : hello :2017-04-25 17:44:20,当前线程:main
2017-04-25 17:44:25.754  INFO 67685 --- [pool-1-thread-1] p.a.service.impl.AccountReceiverImpl     : 用户侧流水:hello :2017-04-25 17:44:20,当前线程:main
Sender : hello :2017-04-25 17:44:25,当前线程:main
Sender : hello :2017-04-25 17:44:30,当前线程:main
2017-04-25 17:44:32.048  INFO 67685 --- [pool-1-thread-2] p.a.service.impl.AccountReceiverImpl     : 用户侧流水:hello :2017-04-25 17:44:30,当前线程:main
Sender : hello :2017-04-25 17:44:32,当前线程:main
Sender : hello :2017-04-25 17:44:33,当前线程:main
2017-04-25 17:44:35.556  INFO 67685 --- [pool-1-thread-3] p.a.service.impl.AccountReceiverImpl     : 用户侧流水:hello :2017-04-25 17:44:33,当前线程:main
Sender : hello :2017-04-25 17:44:35,当前线程:main
Sender : hello :2017-04-25 17:44:37,当前线程:main
2017-04-25 17:44:38.797  INFO 67685 --- [pool-1-thread-1] p.a.service.impl.AccountReceiverImpl     : 用户侧流水:hello :2017-04-25 17:44:37,当前线程:main

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,100评论 19 139
  • 来源 RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。支持消息的持久化、事务、拥塞控...
    jiangmo阅读 10,568评论 2 34
  • rabbitMQ是一款基于AMQP协议的消息中间件,它能够在应用之间提供可靠的消息传输。在易用性,扩展性,高可用性...
    点融黑帮阅读 3,144评论 3 41
  • RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用。 消息...
    彩虹之梦阅读 1,173评论 2 1
  • 1. 历史 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的...
    高广超阅读 6,287评论 3 51

友情链接更多精彩内容