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);
    }
}

 

posted on 2021-05-21 10:13  入梦炼心  阅读(51)  评论(0编辑  收藏  举报