作者:灸灵侯嗒黍_147 | 来源:互联网 | 2023-08-18 16:10
此篇教程以Rabbitmq作为消息队列服务端,使用Spring Boot产生和发布消息。
使用Spring AMQP的RabbitTemplate发布消息,使用MessageListenerAdapter订阅消息。
其中对应的Maven如下:
4.0.0org.springframework.bootspring-boot-starter-parent2.4.0 cn.it1995demo0.0.1-SNAPSHOTdemoDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtestorg.springframework.amqpspring-rabbit-testtestorg.springframework.amqpspring-amqp2.3.1compileorg.springframework.amqpspring-rabbit2.3.1compileorg.springframework.bootspring-boot-maven-plugin
下面是创建接受者,获取发布者的发布的消息:
package cn.it1995.demo;import org.springframework.stereotype.Component;import java.util.concurrent.CountDownLatch;@Component
public class Receiver {private CountDownLatch latch &#61; new CountDownLatch(1);public void receiveMessage(String message){System.out.println("Received <" &#43; message &#43; ">");latch.countDown();}public CountDownLatch getLatch(){return latch;}
}
这个Receiver是POJO类&#xff0c;这里的POJO是指没有带有业务处理的类&#xff0c;当注册后&#xff0c;就能在任意的地方进使用。
CountDownLatch&#xff0c;里面会有一个接收消息的信号。
注册监听者以及发送消息
使用Spring AMQP的RabbitTemplate提供的函数用于接收消息&#xff0c;逻辑如下&#xff1a;
1. 配置消息监听者容器&#xff1b;
2.声明队列&#xff0c;交换机对其都进行绑定&#xff1b;
3.配置发送者组建&#xff0c;使得监听者能接收到。
代码如下&#xff1a;
package cn.it1995.demo;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;&#64;SpringBootApplication
public class DemoApplication {static final String topicExchangeName &#61; "spring-boot-exchange";static final String queueName &#61; "spring-boot";&#64;BeanQueue queue(){return new Queue(queueName, false);}&#64;BeanTopicExchange exchange(){return new TopicExchange(topicExchangeName);}&#64;BeanBinding binding(Queue queue, TopicExchange exchange){return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");}&#64;BeanSimpleMessageListenerContainer container(ConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter){SimpleMessageListenerContainer container &#61; new SimpleMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.setQueueNames(queueName);container.setMessageListener(listenerAdapter);return container;}&#64;BeanMessageListenerAdapter listenerAdapter(Receiver receiver){return new MessageListenerAdapter(receiver, "receiveMessage");}public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
listenerAdapter()方法将消息监听者注册到容器中&#xff08;默认是在container()&#xff09;。
queue()方法创建了AMQP的队列。exchange()方法创建了topic交换机。
binding()方法将其进行绑定。
发送测试数据到Rabbitmq
package cn.it1995.demo;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;&#64;Component
public class Runner implements CommandLineRunner {private final RabbitTemplate rabbitTemplate;private final Receiver receiver;public Runner(Receiver receiver, RabbitTemplate rabbitTemplate){this.receiver &#61; receiver;this.rabbitTemplate &#61; rabbitTemplate;}&#64;Overridepublic void run(String... args) throws Exception {System.out.println("Sending message ...");rabbitTemplate.convertAndSend(DemoApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);}
}
项目的application.properties如下&#xff1a;
spring.rabbitmq.host&#61;122.xxx.xxx.xxx
spring.rabbitmq.port&#61;5672
spring.rabbitmq.username&#61;xxxx
spring.rabbitmq.password&#61;xxxxxxx
spring.rabbitmq.virtual-host&#61;/xxxxx
程序运行截图如下&#xff1a;
源码打包下载地址&#xff1a;
https://github.com/fengfanchen/Java/tree/master/SpringBootRabbitmq