Commit cf71cf88 authored by xiaoguang.xu's avatar xiaoguang.xu

fix : 修复了一个项目没有MQ,graceful-shutdown 启动报错的问题

parent 713c8454
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId> <groupId>cn.quantgroup</groupId>
<version>0.2.5</version> <version>0.2.5.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId> <groupId>cn.quantgroup</groupId>
<version>0.2.5</version> <version>0.2.5.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId> <groupId>cn.quantgroup</groupId>
<version>0.2.5</version> <version>0.2.5.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
...@@ -77,7 +77,12 @@ ...@@ -77,7 +77,12 @@
<version>2.8.8</version> <version>2.8.8</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!--这个可以让我们项目启动速度有质的飞跃-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId> <groupId>cn.quantgroup</groupId>
<version>0.2.5</version> <version>0.2.5.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>cn.quantgroup</groupId> <groupId>cn.quantgroup</groupId>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<version>0.2.5</version> <version>0.2.5.1</version>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
<lombok.version>1.16.20</lombok.version> <lombok.version>1.16.20</lombok.version>
<guava.version>23.0</guava.version> <guava.version>23.0</guava.version>
<apollo.client.version>0.10.2</apollo.client.version> <apollo.client.version>0.10.2</apollo.client.version>
<common.parent.version>0.2.5</common.parent.version> <common.parent.version>0.2.5.1</common.parent.version>
</properties> </properties>
<dependencies> <dependencies>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId> <groupId>cn.quantgroup</groupId>
<version>0.2.5</version> <version>0.2.5.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils; import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry; import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.Lifecycle; import org.springframework.context.Lifecycle;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -20,39 +21,48 @@ import javax.annotation.PreDestroy; ...@@ -20,39 +21,48 @@ import javax.annotation.PreDestroy;
@Configuration @Configuration
public class BaseDestroyHandler { public class BaseDestroyHandler {
@Autowired @Configuration
private ApplicationContext applicationContext; @ConditionalOnClass(RabbitListenerEndpointRegistry.class)
public static class DestroyMessageQueueListener {
private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry; @Autowired
private ApplicationContext applicationContext;
@PostConstruct private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
public void initMQConf() {
try { @PostConstruct
rabbitListenerEndpointRegistry = applicationContext.getBean( public void initMQConf() {
RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME, try {
RabbitListenerEndpointRegistry.class); rabbitListenerEndpointRegistry = applicationContext.getBean(
} catch (Exception e) { RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME,
log.warn("注入MQ endpoint registry 失败, 如果你有MQ队列接收, 可能你要关注一下了"); RabbitListenerEndpointRegistry.class);
} catch (Exception e) {
log.warn("注入MQ endpoint registry 失败, 如果你有MQ队列接收, 可能你要关注一下了");
}
} }
}
@PreDestroy @PreDestroy
private void stopRedisSub() { private void stopRabbitMQ() {
log.info("我什么都没做, redis stopped"); try {
if (rabbitListenerEndpointRegistry == null) {
log.warn("我什么都没做, MQ listener stopped");
return;
}
rabbitListenerEndpointRegistry.getListenerContainers().forEach(Lifecycle::stop);
} catch (Exception e) {
//貌似这段日志不会打印...
log.error("貌似停止 MQ 遇到了问题... 你有MQ么?", e);
}
log.info("MQ listener stopped");
}
} }
@PreDestroy @PreDestroy
private void stopRabbitMQ() { private void stopOther() {
try { log.info("我什么都没做, other stopped");
if (rabbitListenerEndpointRegistry == null) {
log.warn("我什么都没做, MQ listener stopped");
return;
}
rabbitListenerEndpointRegistry.getListenerContainers().forEach(Lifecycle::stop);
} catch (Exception e) {
//貌似这段日志不会打印...
log.error("貌似停止 MQ 遇到了问题... 你有MQ么?", e);
}
log.info("MQ listener stopped");
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment