Skip to content

Commit

Permalink
Fix on application event (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
HzjNeverStop authored Jun 8, 2022
1 parent 858282c commit a05b1b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
*/
package com.alipay.sofa.rpc.boot.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextStoppedEvent;

Expand All @@ -29,9 +33,11 @@
*
* @author <a href="mailto:[email protected]">LiWei</a>
*/
public class ApplicationContextClosedListener implements ApplicationListener {
public class ApplicationContextClosedListener implements ApplicationListener,
ApplicationContextAware {
private final ProviderConfigContainer providerConfigContainer;
private final ServerConfigContainer serverConfigContainer;
private ApplicationContext applicationContext;

public ApplicationContextClosedListener(ProviderConfigContainer providerConfigContainer,
ServerConfigContainer serverConfigContainer) {
Expand All @@ -42,8 +48,16 @@ public ApplicationContextClosedListener(ProviderConfigContainer providerConfigCo
@Override
public void onApplicationEvent(ApplicationEvent event) {
if ((event instanceof ContextClosedEvent) || (event instanceof ContextStoppedEvent)) {
providerConfigContainer.unExportAllProviderConfig();
serverConfigContainer.closeAllServer();
if (applicationContext
.equals(((ApplicationContextEvent) event).getApplicationContext())) {
providerConfigContainer.unExportAllProviderConfig();
serverConfigContainer.closeAllServer();
}
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package com.alipay.sofa.rpc.boot.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

Expand All @@ -28,15 +30,24 @@
*
* @author <a href="mailto:[email protected]">leizhiyuan</a>
*/
public class ApplicationContextRefreshedListener implements
public class ApplicationContextRefreshedListener implements ApplicationContextAware,
ApplicationListener<ContextRefreshedEvent> {

private ApplicationContext applicationContext;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
//rpc 开始启动事件监听器
applicationContext.publishEvent(new SofaBootRpcStartEvent(applicationContext));
//rpc 启动完毕事件监听器
applicationContext.publishEvent(new SofaBootRpcStartAfterEvent(applicationContext));
if (applicationContext.equals(event.getApplicationContext())) {
ApplicationContext applicationContext = event.getApplicationContext();
//rpc 开始启动事件监听器
applicationContext.publishEvent(new SofaBootRpcStartEvent(applicationContext));
//rpc 启动完毕事件监听器
applicationContext.publishEvent(new SofaBootRpcStartAfterEvent(applicationContext));
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

0 comments on commit a05b1b2

Please sign in to comment.