SpringBoot集成Rabbit使用TopicRabbit指定发送集合
来源:互联网 发布:windows镜像下载网站 编辑:程序博客网 时间:2024/06/05 11:20
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,截图时候没有注意)
- 添加Queue
- 添加这个User 到exchange(注意routing-key)
SpringBoot集成Rabbitmq
- 注册配置bean
@Configurablepublic 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 */@Componentpublic 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 */@Componentpublic 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,当前线程:mainSender : hello :2017-04-25 17:44:20,当前线程:main2017-04-25 17:44:25.754 INFO 67685 --- [pool-1-thread-1] p.a.service.impl.AccountReceiverImpl : 用户侧流水:hello :2017-04-25 17:44:20,当前线程:mainSender : hello :2017-04-25 17:44:25,当前线程:mainSender : hello :2017-04-25 17:44:30,当前线程:main2017-04-25 17:44:32.048 INFO 67685 --- [pool-1-thread-2] p.a.service.impl.AccountReceiverImpl : 用户侧流水:hello :2017-04-25 17:44:30,当前线程:mainSender : hello :2017-04-25 17:44:32,当前线程:mainSender : hello :2017-04-25 17:44:33,当前线程:main2017-04-25 17:44:35.556 INFO 67685 --- [pool-1-thread-3] p.a.service.impl.AccountReceiverImpl : 用户侧流水:hello :2017-04-25 17:44:33,当前线程:mainSender : hello :2017-04-25 17:44:35,当前线程:mainSender : hello :2017-04-25 17:44:37,当前线程:main2017-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
阅读全文
0 0
- SpringBoot集成Rabbit使用TopicRabbit指定发送集合
- SpringBoot集成Rabbit使用TopicRabbit指定发送集合
- 使用springboot集成jseesite
- springboot使用问题集合
- springboot使用问题集合
- springboot集成JMS含附件邮件发送
- springboot集成log4j(完成错误信息邮件发送)
- SpringBoot-WebSocket广播消息+单点消息(指定用户发送消息)
- SpringBoot拦截全局异常并发送邮件给指定邮箱
- eclipse 使用maven 集成springboot + retrofit
- springboot集成mongodb使用mongoTemplate和MongoRepository
- SpringBoot使用FreeMarker模板发送邮件
- springboot+Rabit实战三:(springboot+rabbit 项目搭建)
- spring-rabbit使用注意事项
- spring-rabbit的使用
- Spring 发送邮件 使用File指定附件
- SpringBoot学习-(十四)SpringBoot中建立WebSocket连接(STOMP实现发送消息给指定用户)
- 使用Springboot集成dubbox进行微服务治理
- Java锁的种类和区别
- SpringBoot整合Quartz-动态读取任务执行(2.2.1)
- SIM上运营商SPN(Service Provider Name)的读取
- Springboot系列文章
- 熵的通俗理解
- SpringBoot集成Rabbit使用TopicRabbit指定发送集合
- Spring之ioc操作与属性注入
- 输出100-500之间的水仙花数
- 数据库
- Hive(十八)--全局排序
- codevs 1079 回家 Floyd 解题报告
- StringBoot集成Rabbit,根据业务返回ACK
- windows环境下通过c++使用redis
- SpringBoot 序列化器注解使用方法