消息队列MQ

spingboot使用rabbitmq

本文主要是介绍spingboot使用rabbitmq,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.yml配置

spring:
  rabbitmq:
    host: 47.115.14.7
    port: 9903
    username: guest
    password: guest

3.生产者

@Component
public class ProduceSchedule {
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Scheduled(fixedRate = 1000)
    public void send() {
        String msg = "MY_QUEUE send ..." + System.currentTimeMillis();
        amqpTemplate.convertAndSend(QueueNames.MY_QUEUE, msg);
    }
}

4.消费者

@Component
@Slf4j
public class Receiver {

    @RabbitListener(queuesToDeclare = @Queue(QueueNames.MY_QUEUE))
    public void receive(String msg) {
        log.info("MY_QUEUE receive:{}", msg);
    }
}

 

这篇关于spingboot使用rabbitmq的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!