diff --git a/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/spring/config/SofaModuleProperties.java b/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/spring/config/SofaModuleProperties.java index 0e74dad34..0b748306f 100644 --- a/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/spring/config/SofaModuleProperties.java +++ b/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/spring/config/SofaModuleProperties.java @@ -16,10 +16,10 @@ */ package com.alipay.sofa.isle.spring.config; -import static com.alipay.sofa.isle.spring.config.SofaModuleProperties.PREFIX; - import org.springframework.boot.context.properties.ConfigurationProperties; +import static com.alipay.sofa.isle.spring.config.SofaModuleProperties.PREFIX; + /** * Properties * @@ -27,19 +27,20 @@ */ @ConfigurationProperties(prefix = PREFIX) public class SofaModuleProperties { - static final String PREFIX = "com.alipay.sofa.boot"; + static final String PREFIX = "com.alipay.sofa.boot"; private String activeProfiles; - private long beanLoadCost = 100; - private boolean allowBeanDefinitionOverriding = false; - private boolean moduleStartUpParallel = true; - private boolean publishEventToParent = false; - private boolean enableIsle = true; - private boolean allowModuleOverriding = false; - private boolean ignoreModuleInstallFailure = false; - private float parallelRefreshCoreCountFactor = 5.0f; - private long parallelRefreshTimeout = 60; - private long parallelRefreshCheckPeriod = 30; + private long beanLoadCost = 100; + private boolean allowBeanDefinitionOverriding = false; + private boolean moduleStartUpParallel = true; + private boolean publishEventToParent = false; + private boolean enableIsle = true; + private boolean allowModuleOverriding = false; + private boolean ignoreModuleInstallFailure = false; + private boolean unregisterComponentWhenModuleInstallFailure = true; + private float parallelRefreshCoreCountFactor = 5.0f; + private long parallelRefreshTimeout = 60; + private long parallelRefreshCheckPeriod = 30; public String getActiveProfiles() { return activeProfiles; @@ -128,4 +129,12 @@ public long getParallelRefreshCheckPeriod() { public void setParallelRefreshCheckPeriod(long parallelRefreshCheckPeriod) { this.parallelRefreshCheckPeriod = parallelRefreshCheckPeriod; } + + public boolean isUnregisterComponentWhenModuleInstallFailure() { + return unregisterComponentWhenModuleInstallFailure; + } + + public void setUnregisterComponentWhenModuleInstallFailure(boolean unregisterComponentWhenModuleInstallFailure) { + this.unregisterComponentWhenModuleInstallFailure = unregisterComponentWhenModuleInstallFailure; + } } diff --git a/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/stage/SpringContextInstallStage.java b/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/stage/SpringContextInstallStage.java index d81a66c95..7e0fddd66 100644 --- a/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/stage/SpringContextInstallStage.java +++ b/sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/stage/SpringContextInstallStage.java @@ -27,9 +27,11 @@ import com.alipay.sofa.isle.loader.DynamicSpringContextLoader; import com.alipay.sofa.isle.loader.SpringContextLoader; import com.alipay.sofa.isle.spring.config.SofaModuleProperties; +import com.alipay.sofa.runtime.api.ServiceRuntimeException; import com.alipay.sofa.runtime.api.component.ComponentName; import com.alipay.sofa.runtime.log.SofaLogger; import com.alipay.sofa.runtime.spi.component.ComponentInfo; +import com.alipay.sofa.runtime.spi.component.ComponentManager; import com.alipay.sofa.runtime.spi.component.Implementation; import com.alipay.sofa.runtime.spi.util.ComponentNameFactory; import com.alipay.sofa.runtime.spring.SpringContextComponent; @@ -40,13 +42,9 @@ import org.springframework.util.StringUtils; import java.util.ArrayList; +import java.util.Collection; import java.util.List; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Future; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.stream.Collectors; /** @@ -329,6 +327,7 @@ protected void doRefreshSpringContext(DeploymentDescriptor deployment, } catch (Throwable t) { SofaLogger.error(ErrorCode.convert("01-11002", deployment.getName()), t); application.addFailed(deployment); + unRegisterComponent(application, ctx); } finally { deployment.deployFinish(); } @@ -339,6 +338,23 @@ protected void doRefreshSpringContext(DeploymentDescriptor deployment, } } + private void unRegisterComponent(ApplicationRuntimeModel application, + ConfigurableApplicationContext ctx) { + if (sofaModuleProperties.isUnregisterComponentWhenModuleInstallFailure()) { + ComponentManager componentManager = application.getSofaRuntimeContext() + .getComponentManager(); + Collection componentInfos = componentManager + .getComponentInfosByApplicationContext(ctx); + for (ComponentInfo componentInfo : componentInfos) { + try { + componentManager.unregister(componentInfo); + } catch (ServiceRuntimeException e) { + SofaLogger.error(ErrorCode.convert("01-03001", componentInfo.getName()), e); + } + } + } + } + private void publishContextAsSofaComponent(DeploymentDescriptor deployment, ApplicationRuntimeModel application, ApplicationContext context) { diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/component/impl/ComponentManagerImpl.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/component/impl/ComponentManagerImpl.java index b411c4d4a..7ad877531 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/component/impl/ComponentManagerImpl.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/component/impl/ComponentManagerImpl.java @@ -27,12 +27,9 @@ import com.alipay.sofa.runtime.spi.component.ComponentInfo; import com.alipay.sofa.runtime.spi.component.ComponentManager; import com.alipay.sofa.runtime.spring.SpringContextComponent; +import org.springframework.context.ApplicationContext; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; @@ -242,6 +239,19 @@ public void resolvePendingResolveComponent(ComponentName componentName) { } } + @Override + public Collection getComponentInfosByApplicationContext(ApplicationContext application) { + List componentInfos = new ArrayList<>(); + + for (ComponentInfo componentInfo : registry.values()) { + if (Objects.equals(application, componentInfo.getApplicationContext())) { + componentInfos.add(componentInfo); + } + } + + return componentInfos; + } + private void typeRegistry(ComponentInfo componentInfo) { ComponentName name = componentInfo.getName(); if (name != null) { diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spi/component/ComponentManager.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spi/component/ComponentManager.java index 96fdd8cf4..e5375f8f6 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spi/component/ComponentManager.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spi/component/ComponentManager.java @@ -19,6 +19,7 @@ import com.alipay.sofa.runtime.api.ServiceRuntimeException; import com.alipay.sofa.runtime.api.component.ComponentName; import com.alipay.sofa.runtime.model.ComponentType; +import org.springframework.context.ApplicationContext; import java.util.Collection; @@ -113,4 +114,12 @@ public interface ComponentManager { * @param componentName component name */ void resolvePendingResolveComponent(ComponentName componentName); + + /** + * get components by component application + * + * @param application component application + * @return components + */ + Collection getComponentInfosByApplicationContext(ApplicationContext application); }