要將RabbitMQ與Spring Boot整合使用,可以按照以下步驟操作:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring,rabbitmq.username=guest
spring.rabbitmq.password=guest
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQProducer {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(String message) {
amqpTemplate.convertAndSend("myQueue", message);
System.out.println("Message sent: " + message);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQConsumer {
@RabbitListener(queues = "myQueue")
public void receiveMessage(String message) {
System.out.println("Message received: " + message);
}
}
通過以上步驟,就可以實現RabbitMQ與Spring Boot的整合使用。