当前位置: 首页 > news >正文

Springboot结合RabbitMQ

pom.xml

 <!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

application.yaml

spring:rabbitmq:host: 127.0.0.1username: guestpassword: guestport: 5672virtual-host: test_vhostconnection-timeout: 600000# 关闭自动ack,设置成手动acklistener:simple:acknowledge-mode: manualprefetch: 10concurrency: 2max-concurrency: 5

配置文件,也可以使用@PostConstruct

@Configuration
public class RabbitMQConfig {/*** 绑定*/public static final String EXCHANGE_NAME="springboot_topic_exchange";public static final String QUEUE_NAME="springboot_queue";@Bean("exchange")public Exchange exchange(){return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();}@Bean("queue")public Queue queue(){return QueueBuilder.durable(QUEUE_NAME).build();}@Bean()public Binding bindQueueExchange(@Qualifier("exchange")Exchange exchange,@Qualifier("queue")Queue queue){return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();}/** Fanout 模式*/public static final String EXCHANGE_NAME2="my_topic_exchange";public static final String QUEUE_NAME2="my_queue";//创建交换机@Beanpublic FanoutExchange fannoutExchange(){return new FanoutExchange(EXCHANGE_NAME2,true,false);}//创建队列@Beanpublic Queue fannoutQueue(){return new Queue(QUEUE_NAME2,true,false,false);}//声明绑定关系@Beanpublic Binding bindQueue(@Qualifier("exchange")Exchange exchange,@Qualifier("queue")Queue queue){return BindingBuilder.bind(fannoutQueue()).to(fannoutExchange());}/** topic 模式*/@Beanpublic Queue topicQueue(){return new Queue(QUEUE_NAME,true,false,false);}//创建交换机@Beanpublic TopicExchange topicExchange(){return new TopicExchange(EXCHANGE_NAME,true,false);}//声明绑定关系@Beanpublic Binding bindTopicQueue(){return BindingBuilder.bind(topicQueue()).to(topicExchange()).with("yuyang.#");}

消费者代码

@Component
@Slf4j
public class MessageListener  {@RabbitListener(queues="springboot_queue")public void process(Message message) {log.info("helloWorld模式 received message : {}",new String(message.getBody()));}}

生产者代码

	@Autowiredprivate RabbitTemplate rabbitTemplate;// pub/sub 发布订阅模式   交换机类型 fanout@GetMapping("/fanoutSend")public String fanoutSend() throws AmqpException, UnsupportedEncodingException {//fanout模式只往exchange里发送消息。分发到exchange下的所有queuerabbitMQService.sendMessage("my_topic_exchange","*","fanoutSend");return "message sended : "+message;}//topic 工作模式   交换机类型 topic@GetMapping("/topicSend")public String topicSend() throws AmqpException, UnsupportedEncodingException {//fanout模式只往exchange里发送消息。分发到exchange下的所有queuerabbitTemplate.convertAndSend("spring_topic_exchange","yuyang.test","sadedf");return "ok";}

手动ack

	@RabbitListener(queues = "springboot_queue")public void listenerQueue(Message message, Channel channel) throws IOException {log.info(new String(message.getBody()));channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);}
http://www.lryc.cn/news/451167.html

相关文章:

  • UNIAPP 动态菜单实现方法
  • windows C++-使用任务和 XML HTTP 请求进行连接(一)
  • HTB:Oopsie[WriteUP]
  • 【JAVA高级】如何使用Redis加锁和解锁(一)、Lua脚本执行原理及流程
  • 2024年使用宝塔面板轻松部署Java Web
  • 闯关训练一:Linux基础
  • 鸿蒙NEXT开发-ArkTS(基于最新api12稳定版)
  • laravel延迟队列 取消未支付超时订单订单
  • 解锁高效开发的秘密武器
  • 【CSS】兼容处理
  • C语言线程
  • 自闭症寄宿学校 vs. 日常教育:为孩子提供更多可能
  • RxSwift系列(二)操作符
  • Gin框架简易搭建(3)--Grom与数据库
  • JavaScript模块化-CommonJS规范和ESM规范
  • 解决银河麒麟V10中的apt Lock异常
  • windows11环境安装lua及luarocks(踩坑篇)
  • Glide基本用法及With方法源码解析
  • html中的文本标签(含标签的实现案例)
  • 通信协议感悟
  • IDEA几大常用AI插件
  • 51单片机学习第六课---B站UP主江协科技
  • sadTalker本地编译
  • 强化学习核心概念与公式总结
  • 基础算法--双指针【概念+图解+题解+解释】
  • 国产化系统/鸿蒙开发足浴店收银源码-收缩左侧———未来之窗行业应用跨平台架构
  • 如何从硬盘恢复丢失/删除的视频
  • 《Effective C++》第三版——设计与声明(1)
  • 数值计算的程序设计问题举例
  • Java之方法的使用