Skip to content

Commit

Permalink
Fix sofaruntime manager destroy whitout shutdown (#1324)
Browse files Browse the repository at this point in the history
Signed-off-by: JermaineHua <[email protected]>
  • Loading branch information
CrazyHZM authored May 23, 2024
1 parent 0dceaf8 commit 9bfbb73
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
Expand All @@ -34,7 +33,7 @@
* @author huzijie
* @since 2.5.0
*/
public class SofaRuntimeContainer implements ApplicationContextAware, DisposableBean {
public class SofaRuntimeContainer implements ApplicationContextAware {

private static final Map<ClassLoader, ApplicationContext> APPLICATION_CONTEXT_MAP = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -104,10 +103,14 @@ public static void clear() {
JVM_INVOKE_SERIALIZE_MAP.clear();
}

@Override
public void destroy() {
public static void destroy(ClassLoader contextClassLoader) {
APPLICATION_CONTEXT_MAP.remove(contextClassLoader);
SOFA_RUNTIME_MANAGER_MAP.remove(contextClassLoader);

SofaRuntimeManager sofaRuntimeManager = SOFA_RUNTIME_MANAGER_MAP.remove(contextClassLoader);
if (sofaRuntimeManager != null) {
sofaRuntimeManager.shutDownExternally();
}

JVM_SERVICE_CACHE_MAP.remove(contextClassLoader);
JVM_INVOKE_SERIALIZE_MAP.remove(contextClassLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ private void doUninstallBiz(Biz biz) {
// Remove dynamic JVM service cache
DynamicJvmServiceProxyFinder.getInstance().afterBizUninstall(biz);

SofaRuntimeManager sofaRuntimeManager = SofaRuntimeContainer.getSofaRuntimeManager(biz
.getBizClassLoader());
SofaRuntimeContainer.destroy(biz.getBizClassLoader());

if (sofaRuntimeManager == null) {
throw new IllegalStateException("No SofaRuntimeManager match classLoader");
}
sofaRuntimeManager.shutDownExternally();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void destroy() throws Exception {

assertThat(SofaRuntimeContainer.getApplicationContext(classLoaderA)).isEqualTo(
genericApplicationContext);
sofaRuntimeContainer.destroy();
sofaRuntimeContainer.destroy(classLoaderA);
assertThat(SofaRuntimeContainer.getApplicationContext(classLoaderA)).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.support.GenericApplicationContext;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -50,14 +49,6 @@ public void clear() {
SofaRuntimeContainer.clear();
}

@Test
public void noSofaRuntimeManager() {
MockBiz mockBiz = new MockBiz();
assertThatThrownBy(() -> sofaBizUninstallEventHandler.handleEvent(new BeforeBizStopEvent(mockBiz)))
.isInstanceOf(IllegalStateException.class)
.hasMessage("No SofaRuntimeManager match classLoader");
}

@Test
public void shutDown() {
SofaRuntimeContainer sofaRuntimeContainer = new SofaRuntimeContainer(sofaRuntimeManager);
Expand Down

0 comments on commit 9bfbb73

Please sign in to comment.