Spring Framework没有内置的消息队列,但是它提供了对多个消息队列的集成支持,比如ActiveMQ、RabbitMQ和Kafka等。你可以根据具体的消息队列选择相应的集成方式进行配置。
下面以ActiveMQ为例,介绍如何配置Spring与ActiveMQ的集成:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(String destination, String message) {
jmsTemplate.convertAndSend(destination, message);
}
}
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver {
@JmsListener(destination = "myQueue")
public void receiveMessage(String message) {
System.out.println("Received message: " + message);
}
}
这样就完成了Spring与ActiveMQ的集成配置。你可以根据具体的消息队列选择相应的依赖和配置方式。