AlbumenJ commented on code in PR #14432: URL: https://github.com/apache/dubbo/pull/14432#discussion_r1730509813
########## dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java: ########## @@ -42,144 +42,133 @@ public class DubboShutdownHook extends Thread { private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DubboShutdownHook.class); - private final ApplicationModel applicationModel; + private DubboShutdownHook() { + super("DubboShutdownHook"); + } - /** - * Has it already been registered or not? - */ - private final AtomicBoolean registered = new AtomicBoolean(false); + private static class SingletonHelper { + private static final DubboShutdownHook INSTANCE = new DubboShutdownHook(); + } /** - * Has it already been destroyed or not? + * Returns the singleton instance of <code>DubboShutdownHook</code>. + * + * @return singleton instance */ - private final AtomicBoolean destroyed = new AtomicBoolean(false); + public static DubboShutdownHook getInstance() { + return SingletonHelper.INSTANCE; + } + + private boolean checkExternalManagedModule(ApplicationModel applicationModel) { + for (ModuleModel moduleModel : applicationModel.getModuleModels()) { + if (moduleModel.isLifeCycleManagedExternally() && !moduleModel.isDestroyed()) { + return true; + } + } + return false; + } + + protected static int getServerShutdownTimeout(ApplicationModel appModel) { + try { + return ConfigurationUtils.getServerShutdownTimeout(appModel); + } catch (RuntimeException re) { + return 0; + } + } /** - * Whether ignore listen on shutdown hook? + * Checks whether external managed {@code ModuleModel} exists, and wait until the corresponding external + * managed modules of {@code ApplicationModel} has been destroyed or the serverShutdownTimeout expired. + * */ - private final boolean ignoreListenShutdownHook; + private void checkAndWaitUntilTimeout() { + long start = System.currentTimeMillis(); + /* To avoid shutdown conflicts between Dubbo and Spring, + * wait for the modules bound to Spring to be handled by Spring until timeout. + */ + Map<ApplicationModel, Integer> serverShutdownWaits = FrameworkModel.getAllInstances().stream() + .flatMap(fwkModel -> fwkModel.getAllApplicationModels().stream()) + .distinct() + .filter(appModel -> + appModel.getModuleModels().stream().anyMatch(ModuleModel::isLifeCycleManagedExternally)) + .collect(Collectors.toMap( + appModel -> appModel, + DubboShutdownHook::getServerShutdownTimeout, + (existingValue, newValue) -> existingValue, + HashMap::new)); + + for (Map.Entry<ApplicationModel, Integer> entry : serverShutdownWaits.entrySet()) { + ApplicationModel applicationModel = entry.getKey(); + Integer val = entry.getValue(); + + if (val.intValue() <= 0 || applicationModel.isDestroyed()) { + continue; + } - public DubboShutdownHook(ApplicationModel applicationModel) { - super("DubboShutdownHook"); - this.applicationModel = applicationModel; - Assert.notNull(this.applicationModel, "ApplicationModel is null"); - ignoreListenShutdownHook = Boolean.parseBoolean( - ConfigurationUtils.getProperty(applicationModel, CommonConstants.IGNORE_LISTEN_SHUTDOWN_HOOK)); Review Comment: Should support this -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org