SpringBoot实战(八)之RabbitMQ
什么是RabbitMQ? RabbitMQ 是一个消息代理。它的核心原理非常简单:接收和发送消息。你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处。在这个比喻中,RabbitMQ 就扮演着邮箱、邮局以及邮递员的角色。 RabbitMQ 和邮局的主要区别是,它不是用来处理纸张的,它是用来接收、存储和发送消息(message)这种二进制数据的。 ? 本文主要演示是Springboot+RabbitMQ简单整合+实例说明 ? 关于安装RabbitMQ,由于RabbitMQ是用Erlang语言写的,首先必须安装Erlang的环境。 RabbitMQ在Window下的安装可以参考该博文链接:https://blog.csdn.net/weixin_39735923/article/details/79288578 该博文十分清楚详细,我就不多说了。 ? 下面进入示例: ? 一、maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> modelVersion>4.0.0</> groupId>org.springframeworkartifactId>gs-messaging-rabbitmqversion>0.1.0parent> >org.springframework.boot>spring-boot-starter-parent>1.5.8.RELEASEpropertiesjava.version>1.8java.versiondependenciesdependency> >spring-boot-starter-amqp> buildpluginsplugin> >spring-boot-maven-plugin> project> ? 二、编写Receive package hello; import java.util.concurrent.CountDownLatch; org.springframework.stereotype.Component; @Component public class Receiver { private CountDownLatch latch = new CountDownLatch(1); void receiveMessage(String message) { System.out.println("Received <" + message + ">"); latch.countDown(); } public CountDownLatch getLatch() { return latch; } }
为方便起见,这个POJO也有一个 注册监听器并发送消息Spring AMQP?
? ? 三、编写Runner java.util.concurrent.TimeUnit; org.springframework.amqp.rabbit.core.RabbitTemplate; org.springframework.boot.CommandLineRunner; class Runner implements CommandLineRunner { private final RabbitTemplate rabbitTemplate; Receiver receiver; Runner(Receiver receiver,RabbitTemplate rabbitTemplate) { this.receiver = receiver; this.rabbitTemplate = rabbitTemplate; } @Override void run(String... args) throws Exception { System.out.println("Sending message..."); rabbitTemplate.convertAndSend(Application.topicExchangeName,"foo.bar.baz","Hello from RabbitMQ!"); receiver.getLatch().await(10000,TimeUnit.MILLISECONDS); } } ? 四、编写配置文件 spring.application.name=spirng-boot-rabbitmq spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ? 五、编写启动类 org.springframework.amqp.core.Binding; org.springframework.amqp.core.BindingBuilder; org.springframework.amqp.core.Queue; org.springframework.amqp.core.TopicExchange; org.springframework.amqp.rabbit.connection.ConnectionFactory; org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; org.springframework.boot.SpringApplication; org.springframework.boot.autoconfigure.SpringBootApplication; org.springframework.context.annotation.Bean; @SpringBootApplication Application { static final String topicExchangeName = "spring-boot-exchange"; final String queueName = "spring-boot"; @Bean Queue queue() { return new Queue(queueName,false); } @Bean TopicExchange exchange() { new TopicExchange(topicExchangeName); } @Bean Binding binding(Queue queue,TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#"); } @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { new MessageListenerAdapter(receiver,"receiveMessage"); } void main(String[] args) InterruptedException { SpringApplication.run(Application.Spring Boot会自动创建连接工厂和RabbitTemplate,从而减少您必须编写的代码量。 |