package com.yupi.springbootinit.common; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component @Slf4j public class DelayedRabbitMQStarter implements ApplicationRunner { @Resource private RabbitListenerEndpointRegistry registry; // Spring 提供的监听器注册表 @Override public void run(ApplicationArguments args) throws Exception { // 延迟 5 秒后启动监听器 log.info("延迟5秒启动 rabbitMQ 监听"); Thread.sleep(5000); registry.getListenerContainer("hosts").start(); registry.getListenerContainer("bigbrother").start(); } }