这篇文章主要介绍了RabbitMQ的简单模式怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇RabbitMQ的简单模式怎么实现文章都会有所收获,下面我们一起来看看吧。
生产者声明队列并向队列发送消息
导入 com.example.rabbitmq.constant.RabbitConstant;
导入 org.springframework.amqp.core.Queue;
导入 org.springframework.context.annotation.Bean;
导入 org.springframework.context.annotation.Configuration;
@Configuration public class RabbitSimpleProvider {
@Bean
public Queue simpleQueue() {
return new Queue(RabbitConstant.SIMPLE_QUEUE_NAME);
}
}
消费者监听队列并消费消息
导入 com.example.rabbitmq.constant.RabbitConstant;
导入 org.springframework.amqp.rabbit.annotation.RabbitHandler;
导入 org.springframework.amqp.rabbit.annotation.RabbitListener;
导入 org.springframework.stereotype.Component;
@Component public class RabbitSimpleConsumer {
@RabbitHandler
@RabbitListener(queues = RabbitConstant.SIMPLE_QUEUE_NAME)
public void simpleListener(String context) {
System.out.println("rabbit receiver: " + context);
}
}
单元测试
@Test public void simple() {
rabbitTemplate.convertAndSend(RabbitConstant.SIMPLE_QUEUE_NAME, "hello world!");
}
响应结果
关于“RabbitMQ的简单模式怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“RabbitMQ的简单模式怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。