pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml
spring:
rabbitmq:
host: localhost
port: 5672
password: guest
username: guest
接收MQ消息
@Slf4j
@Component
public class MqReceiver {
//方式一 @RabbitListener(queues = "myQueue")
//方式二 自动创建队列 @RabbitListener(queuesToDeclare = @Queue("myQueue"))
//方式三 自动创建只接收key消息的exchange和Queue绑定
@RabbitListener(bindings= @QueueBinding( key="computer", value=@Queue("computerOrder"), exchange=@Exchange("myOrder") ))
public void process(String message){
log.info("computer MqReceiver:{}",message);
}
}
发送MQ消息
@Slf4j
@Component
public class MqSender {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(){
amqpTemplate.convertAndSend("myOrder", "computer", "要发送的内容");
}
}
网友评论