@@ -41,7 +41,7 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
4141 .execute (new TypeReference <>() {});
4242 } catch (Exception e ) {
4343 // 抛出异常 代表没有旧的启动 或者旧的不靠谱
44- log .info ("尝试访问旧的应用失败。本异常不重要,正常启动启动都会输出,请忽略。" + e .getMessage ());
44+ log .info ("尝试访问旧的应用失败。本异常不重要,正常启动启动都会输出,请忽略。" + e .getMessage ());
4545
4646 // 尝试杀死旧的进程
4747 killOldIfNecessary (environment );
@@ -60,59 +60,65 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
6060 }
6161
6262 private void killOldIfNecessary (String environment ) {
63- ProcessHandle .allProcesses ().forEach (process -> {
64- String command = process .info ().command ().orElse (null );
65- // 不是java应用
66- boolean isJava = StringUtils .endsWithIgnoreCase (command , "java" ) || StringUtils .endsWithIgnoreCase (command ,
67- "java.exe" );
68- if (!isJava ) {
69- return ;
70- }
71- String [] arguments = process .info ().arguments ().orElse (null );
72- // 没有参数
73- if (arguments == null ) {
74- return ;
75- }
76- // 是否是dbhub
77- boolean isDbhub = false ;
78- String environmentArgument = null ;
79- for (String argument : arguments ) {
80- if (StringUtils .equals ("ali-dbhub-server-start.jar" , argument )) {
81- isDbhub = true ;
63+ try {
64+ ProcessHandle .allProcesses ().forEach (process -> {
65+ String command = process .info ().command ().orElse (null );
66+ // 不是java应用
67+ boolean isJava = StringUtils .endsWithIgnoreCase (command , "java" ) || StringUtils .endsWithIgnoreCase (
68+ command ,
69+ "java.exe" );
70+ if (!isJava ) {
71+ return ;
72+ }
73+ String [] arguments = process .info ().arguments ().orElse (null );
74+ // 没有参数
75+ if (arguments == null ) {
76+ return ;
77+ }
78+ // 是否是dbhub
79+ boolean isDbhub = false ;
80+ String environmentArgument = null ;
81+ for (String argument : arguments ) {
82+ if (StringUtils .equals ("chat2db-server-start.jar" , argument )) {
83+ isDbhub = true ;
84+ }
85+ if (StringUtils .startsWith (argument , "-Dspring.profiles.active=" )) {
86+ environmentArgument = StringUtils .substringAfter (argument , "-Dspring.profiles.active=" );
87+ }
88+ }
89+ // 不是dbhub
90+ if (!isDbhub ) {
91+ return ;
8292 }
83- if (StringUtils .startsWith (argument , "-Dspring.profiles.active=" )) {
84- environmentArgument = StringUtils .substringAfter (argument , "-Dspring.profiles.active=" );
93+ // 判断是否是正式环境
94+ if (StringUtils .equals (SystemEnvironmentEnum .RELEASE .getCode (), environment ) && StringUtils .equals (
95+ SystemEnvironmentEnum .RELEASE .getCode (), environmentArgument )) {
96+ log .info ("正式环境需要关闭进程" );
97+ destroyProcess (process , command , arguments );
98+ return ;
8599 }
86- }
87- // 不是dbhub
88- if (!isDbhub ) {
89- return ;
90- }
91- // 判断是否是正式环境
92- if (StringUtils .equals (SystemEnvironmentEnum .RELEASE .getCode (), environment ) && StringUtils .equals (
93- SystemEnvironmentEnum .RELEASE .getCode (), environmentArgument )) {
94- log .info ("正式环境需要关闭进程" );
95- destroyProcess (process , command , arguments );
96- return ;
97- }
98100
99- // 判断是否是测试环境
100- if (StringUtils .equals (SystemEnvironmentEnum .TEST .getCode (), environment ) && StringUtils .equals (
101- SystemEnvironmentEnum .TEST .getCode (), environmentArgument )) {
102- log .info ("测试环境需要关闭进程" );
103- destroyProcess (process , command , arguments );
104- return ;
105- }
101+ // 判断是否是测试环境
102+ if (StringUtils .equals (SystemEnvironmentEnum .TEST .getCode (), environment ) && StringUtils .equals (
103+ SystemEnvironmentEnum .TEST .getCode (), environmentArgument )) {
104+ log .info ("测试环境需要关闭进程" );
105+ destroyProcess (process , command , arguments );
106+ return ;
107+ }
108+
109+ // 判断是否是本地环境
110+ boolean devDestroy = StringUtils .equals (SystemEnvironmentEnum .DEV .getCode (), environment ) && (
111+ environmentArgument == null
112+ || StringUtils .equals (SystemEnvironmentEnum .DEV .getCode (), environmentArgument ));
113+ if (devDestroy ) {
114+ log .info ("本地环境需要关闭进程" );
115+ destroyProcess (process , command , arguments );
116+ }
117+ });
118+ } catch (Throwable t ) {
119+ log .warn ("尝试关闭多余的进程失败,不影响正常启动" , t );
120+ }
106121
107- // 判断是否是本地环境
108- boolean devDestroy = StringUtils .equals (SystemEnvironmentEnum .DEV .getCode (), environment ) && (
109- environmentArgument == null
110- || StringUtils .equals (SystemEnvironmentEnum .DEV .getCode (), environmentArgument ));
111- if (devDestroy ) {
112- log .info ("本地环境需要关闭进程" );
113- destroyProcess (process , command , arguments );
114- }
115- });
116122 }
117123
118124 private void destroyProcess (ProcessHandle process , String command , String [] arguments ) {
0 commit comments