延迟启动Mq
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext run = SpringApplication.run(ServiceApplication.class, args);
MqDelayStart rabbitMQRun = run.getBean(MqDelayStart.class);
rabbitMQRun.start();
}
@Bean
public MqDelayStart rabbitMQRun() {
return new MqDelayStart();
}
private static class MqDelayStart {
@Resource
RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
public void start() throws InterruptedException {
Thread.sleep(10*1000);
rabbitListenerEndpointRegistry.start();
}
}
动态停止或启动消费,可以指定全部或某个消费
@Component
public class MqStartOrStopConfig {
@Resource
private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
@NacosConfigListener(dataId = "xxxx.yaml", groupId = "ddddd4b-04fd1c05d61e", type = ConfigType.YAML, properties = @NacosProperties(namespace = "shared"))
public void startOrStopMq(String configInfo) {
try {
String startOrStopMq = getValue(configInfo, "startOrStopMq");
if (StringUtils.equals(startOrStopMq, "start") && !rabbitListenerEndpointRegistry.isRunning()) {
rabbitListenerEndpointRegistry.start();
} else if (StringUtils.equals(startOrStopMq, "stop") && rabbitListenerEndpointRegistry.isRunning()) {
rabbitListenerEndpointRegistry.stop();
}
} catch (Exception e) {
System.out.println(e);
}
}
}