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

springboot整合RabbitMQ 消费端处理数据

pom 依赖

   		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

写一个rabbitmq配置文件

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableRabbit
public class RabbitMQConfig {/*** 注入交换机* @return*/@Beanpublic FanoutExchange exchange1() {return new FanoutExchange ("自己的交换机");}/*** 注入队列* @return*/@Beanpublic Queue queue1() {return new Queue("名字随便取");}/*** 交换机和队列绑定* @return*/@Beanpublic Binding binding1() {return BindingBuilder.bind(queue1()).to(exchange1());}

消费者端代码

@Component
@Slf4j
public class MyConsumer {/***   队列名和配置文件中队列名一样即可消费到**/@RabbitListener(queues = "名字随便取")public void handleMessage(Message message) {byte[] body = message.getBody();// 指定编码方式将字节数组转换为字符串String content = null; // 获取消息体字节数组content = new String(body, "GBK");System.out.println(content);}}
http://www.lryc.cn/news/94426.html

相关文章:

  • 计算机中CPU、内存、缓存的关系
  • 【Linux实验】构造一个简单的 shell
  • 【电路原理学习笔记】第2章:电压、电流和电阻:2.6 电路
  • 基于深度学习的人脸检测技术
  • 【linux kernel】一文总结linux内核通知链
  • kafka入门,Kafka 副本(十三)
  • 利用PPT制作简单的矢量图
  • 18-Linux 常用命令
  • 2024考研408-计算机组成原理第六章-总线学习笔记
  • uni_app 微信小程序 苹果手机 边框显示不全
  • vue 访问第三方 跨域, 配置vue.config.js
  • 使用gradio库的File模块实现文件上传和展示
  • 网络安全进阶学习第四课——SSRF服务器请求伪造
  • js处理扁平数组和树结构相互转换
  • Spark弹性分布式数据集
  • ffmpeg学习记录
  • ChatGPT:为教育创新提供五大机遇
  • Educational Codeforces Round 151 (Rated for Div. 2)
  • 【AI机器学习入门与实战】机器学习算法都有哪些分类?
  • React之hooks
  • 1.监控分布式--zabbix
  • java stream 多个集合去重取交集
  • 给LLM装上知识:从LangChain+LLM的本地知识库问答到LLM与知识图谱的结合
  • 视频与AI,与进程交互(二) pytorch 极简训练自己的数据集并识别
  • LLM - 第2版 ChatGLM2-6B (General Language Model) 的工程配置
  • 从0开始,手写MySQL事务
  • React中useState的setState方法请求了好多次
  • 【MYSQL基础】基础命令介绍
  • 多元回归预测 | Matlab基于灰狼算法优化深度置信网络(GWO-DBN)的数据回归预测,matlab代码回归预测,多变量输入模型
  • 校园wifi网页认证登录入口