RabbitMQ #1 demo
spring.rabbitmq.host= 127.0 . 0.1 spring.rabbitmq.port= 5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest |
Config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.example.demo; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; @Configuration public class RabbitMQCongig { @Bean public Queue testQueue() { return new Queue( "testQueue" ); } @Bean public DirectExchange testExchange(){ return new DirectExchange( "testExchange" ); } @Bean public Binding bindTestQueue() { return BindingBuilder.bind(testQueue()).to(testExchange()).with( "testRoutingKey" ); } } |
Sender
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.example.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Sender { @Autowired public RabbitTemplate rabbitTemplate; Logger logger = LoggerFactory.getLogger( this .getClass()); public void send(String msg) { logger.info( "=========================== 发送消息开始" + " ===========================" ); logger.info( "发送消息:【 " + msg + " 】" ); rabbitTemplate.convertAndSend( "testExchange" , "testRoutingKey" , msg); logger.info( "=========================== 发送消息结束" + " ===========================" ); } } |
消费者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.example.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener (queues = "testQueue" ) public class Receiver { Logger logger = LoggerFactory.getLogger( this .getClass()); @RabbitHandler public void process(String msg){ logger.info( "=========================== 接收消息开始" + " ===========================" ); logger.info( "接收消息:【 " + msg + " 】" ); logger.info( "=========================== 接收消息结束" + " ===========================" ); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!