12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-12-04 13:39:09
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在SpringBoot中,可以使用Spring的消息队列框架来发布和订阅消息。其中,常用的消息队列框架包括RabbitMQ、ActiveMQ、Kafka等。下面以RabbitMQ为例介绍如何在SpringBoot中发布
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在SpringBoot中,可以使用Spring的消息队列框架来发布和订阅消息。其中,常用的消息队列框架包括RabbitMQ、ActiveMQ、Kafka等。下面以RabbitMQ为例介绍如何在SpringBoot中发布和订阅消息:
pom.xml
文件中添加RabbitMQ的依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
application.properties
或application.yml
中添加RabbitMQ的配置信息:spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guest
RabbitTemplate
来发送消息。例如:import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class MessageSender {@Autowiredprivate AmqpTemplate rabbitTemplate;public void sendMessage(String message) {rabbitTemplate.convertAndSend("exchange", "routing-key", message);}}
@RabbitListener
注解来接收消息。例如:import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Componentpublic class MessageReceiver {@RabbitListener(queues = "queue-name")public void receiveMessage(String message) {System.out.println("Received message: " + message);}}
通过以上步骤,就可以在SpringBoot中实现发布和订阅消息了。当消息发送者调用sendMessage
方法时,消息接收者会收到消息并输出到控制台。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19