Skip to content

Commit a05b1b2

Browse files
authored
Fix on application event (#979)
1 parent 858282c commit a05b1b2

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationContextClosedListener.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
*/
1717
package com.alipay.sofa.rpc.boot.context;
1818

19+
import org.springframework.beans.BeansException;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.context.ApplicationContextAware;
1922
import org.springframework.context.ApplicationEvent;
2023
import org.springframework.context.ApplicationListener;
24+
import org.springframework.context.event.ApplicationContextEvent;
2125
import org.springframework.context.event.ContextClosedEvent;
2226
import org.springframework.context.event.ContextStoppedEvent;
2327

@@ -29,9 +33,11 @@
2933
*
3034
* @author <a href="mailto:[email protected]">LiWei</a>
3135
*/
32-
public class ApplicationContextClosedListener implements ApplicationListener {
36+
public class ApplicationContextClosedListener implements ApplicationListener,
37+
ApplicationContextAware {
3338
private final ProviderConfigContainer providerConfigContainer;
3439
private final ServerConfigContainer serverConfigContainer;
40+
private ApplicationContext applicationContext;
3541

3642
public ApplicationContextClosedListener(ProviderConfigContainer providerConfigContainer,
3743
ServerConfigContainer serverConfigContainer) {
@@ -42,8 +48,16 @@ public ApplicationContextClosedListener(ProviderConfigContainer providerConfigCo
4248
@Override
4349
public void onApplicationEvent(ApplicationEvent event) {
4450
if ((event instanceof ContextClosedEvent) || (event instanceof ContextStoppedEvent)) {
45-
providerConfigContainer.unExportAllProviderConfig();
46-
serverConfigContainer.closeAllServer();
51+
if (applicationContext
52+
.equals(((ApplicationContextEvent) event).getApplicationContext())) {
53+
providerConfigContainer.unExportAllProviderConfig();
54+
serverConfigContainer.closeAllServer();
55+
}
4756
}
4857
}
58+
59+
@Override
60+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
61+
this.applicationContext = applicationContext;
62+
}
4963
}

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationContextRefreshedListener.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package com.alipay.sofa.rpc.boot.context;
1818

19+
import org.springframework.beans.BeansException;
1920
import org.springframework.context.ApplicationContext;
21+
import org.springframework.context.ApplicationContextAware;
2022
import org.springframework.context.ApplicationListener;
2123
import org.springframework.context.event.ContextRefreshedEvent;
2224

@@ -28,15 +30,24 @@
2830
*
2931
* @author <a href="mailto:[email protected]">leizhiyuan</a>
3032
*/
31-
public class ApplicationContextRefreshedListener implements
33+
public class ApplicationContextRefreshedListener implements ApplicationContextAware,
3234
ApplicationListener<ContextRefreshedEvent> {
3335

36+
private ApplicationContext applicationContext;
37+
3438
@Override
3539
public void onApplicationEvent(ContextRefreshedEvent event) {
36-
ApplicationContext applicationContext = event.getApplicationContext();
37-
//rpc 开始启动事件监听器
38-
applicationContext.publishEvent(new SofaBootRpcStartEvent(applicationContext));
39-
//rpc 启动完毕事件监听器
40-
applicationContext.publishEvent(new SofaBootRpcStartAfterEvent(applicationContext));
40+
if (applicationContext.equals(event.getApplicationContext())) {
41+
ApplicationContext applicationContext = event.getApplicationContext();
42+
//rpc 开始启动事件监听器
43+
applicationContext.publishEvent(new SofaBootRpcStartEvent(applicationContext));
44+
//rpc 启动完毕事件监听器
45+
applicationContext.publishEvent(new SofaBootRpcStartAfterEvent(applicationContext));
46+
}
47+
}
48+
49+
@Override
50+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
51+
this.applicationContext = applicationContext;
4152
}
4253
}

0 commit comments

Comments
 (0)