SpringBoot整合RabbitMQ

SpringBoot整合RabbitMQ

/** * 自动配置 * 1、RabbitAutoConfiguration * 2、有自动配置了连接工厂ConnectionFactory; * 3、RabbitProperties 封装了 RabbitMQ的配置 * 4、 RabbitTemplate :给RabbitMQ发送和接受消息; * 5、 AmqpAdmin : RabbitMQ系统管理功能组件; * AmqpAdmin:创建和删除 Queue,Exchange,Binding * 6、@EnableRabbit + @RabbitListener 监听消息队列的内容 * */
@EnableRabbit  //开启基于注解的RabbitMQ模式
@SpringBootApplication
public class Springboot02AmqpApplication {
   

	public static void main(String[] args) {
   
		SpringApplication.run(Springboot02AmqpApplication.class, args);
	}
}

1、RabbitAutoConfiguration
2、有自动配置了连接工厂ConnectionFactory;

3、RabbitProperties 封装了 RabbitMQ的配置

4、 RabbitTemplate :给RabbitMQ发送和接受消息;

	@Autowired
	RabbitTemplate rabbitTemplate;
 //发送数据
    @Test
    public void contextLoads() {
   

        //rabbitTemplate.send(exchange,routeKey,message);

        Map<String,Object> map = new HashMap<>();
        map.put("msg","hello curry");
        map.put("data", Arrays.asList("helloworld",123,true));
        //默认使用application/x-java-serialized-object序列化规则
        rabbitTemplate.convertAndSend("exchange.direct","atwyy.news",map);

    }
    //接收数据,如何将数据转为json发送
    @Test
    public void receive(){
   
        Object receiveAndConvert = rabbitTemplate.receiveAndConvert("atwyy.news");
        System.out.println(receiveAndConvert.getClass());
        System.out.println(receiveAndConvert);
    }
    @Test
    public void sendMsg(){
   
        rabbitTemplate.convertAndSend("exchange.fanout","",new Book("围城","钱钟书"));
    }

5、 AmqpAdmin : RabbitMQ系统管理功能组件;
AmqpAdmin:创建和删除 Queue,Exchange,Binding

   @Autowired
    AmqpAdmin amqpAdmin;

// //创建转换器
// @Test
// public void createExchange(){
   
// amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
// System.out.println("创建amqpadmin.exchange");
// }
// //创建队列
// @Test
// public void createQueue(){
   
// amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
// System.out.println("创建amqpadmin.queue");
// }
// //绑定
// @Test
// public void createBinding(){
   
// amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.hh",null));
// }

6、@EnableRabbit + @RabbitListener 监听消息队列的内

@Service
public class BookService {
   
    @RabbitListener(queues = "atwyy.news")
    public void receive(Book book){
   
        System.out.println("收到消息: " + book);
    }
    @RabbitListener(queues = "atwyy")
    public void receive02(Message message){
   
        System.out.println(message.getBody());
        System.out.println(message.getMessageProperties());
    }
}
全部评论

相关推荐

面试官问:为什么不考研?该怎么回答啊😭我说现在的就业环境差到底了,还有就是我不想学数学,感觉面试官笑容都凝固了😢
DayDayNoBug的鲜芋球:我说的是“上学期其实尝试过去探索一些研究的方向,但感觉那些对我来说都没有很大的吸引力,相比起研究我可能更喜欢开发这种实践性的东西,它会让我觉得很有意思并且会为之深入进去”(虽然也不知这个回答怎么样哈哈哈哈哈哈)
点赞 评论 收藏
分享
感觉今年拿到大厂实习offer的人很多,光是身边同学室友都是好几个offer。由此可见,秋招得有多卷
小浪_Coding:必须卷的起飞, 应该比25更卷一点, 25已经是哀声一片了, 26会更难一点, 现在还有`很多25未找到的
点赞 评论 收藏
分享
06-12 16:50
已编辑
小米_软件开发(准入职员工)
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
每晚夜里独自颤抖:要求太多的没必要理
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务