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的集成配置。你可以根據具體的消息隊列選擇相應的依賴和配置方式。