Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] support disable shutdown hook #8369

Merged
merged 1 commit into from Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -48,5 +48,13 @@ public interface ConfigKeys {
*/
String DUBBO_CONFIG_IGNORE_DUPLICATED_INTERFACE = "dubbo.config.ignore-duplicated-interface";


/**
* Disable dubbo shutdown hook, Default value is false.
* When disable the shutdown hook, you may need to do :
* 1. offline all services by qos manually before shutdown dubbo application
* 2. no DubboBootstrapStopedEvent will be sent
* 3. all ShutdownHookCallback will not be called
* 4. dubbo application will not be destroyed when spring context is closed
*/
String DUBBO_LIFECYCLE_DISABLE_SHUTDOWN_HOOK = "dubbo.lifecycle.disable-shutdown-hook";
}
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;

import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -44,7 +45,7 @@ public class DubboShutdownHook extends Thread {
/**
* Has it already been destroyed or not?
*/
private static final AtomicBoolean destroyed = new AtomicBoolean(false);
private final AtomicBoolean destroyed = new AtomicBoolean(false);

private DubboShutdownHook(String name) {
super(name);
Expand All @@ -56,6 +57,15 @@ public static DubboShutdownHook getDubboShutdownHook() {

@Override
public void run() {
String disableShutdownHookValue = (String) ApplicationModel.getEnvironment().getConfiguration()
.getProperty(ConfigKeys.DUBBO_LIFECYCLE_DISABLE_SHUTDOWN_HOOK, "false");
if (Boolean.parseBoolean(disableShutdownHookValue)) {
if (logger.isWarnEnabled()) {
logger.warn("Shutdown hook is disabled, please shutdown dubbo services by qos manually");
}
return;
}

if (destroyed.compareAndSet(false, true)) {
if (logger.isInfoEnabled()) {
logger.info("Run shutdown hook now.");
Expand Down
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.config.spring;

import org.apache.dubbo.config.ConfigKeys;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ShutdownHookTest {

@Test
public void testDisableShutdownHook(){
SysProps.setProperty(ConfigKeys.DUBBO_LIFECYCLE_DISABLE_SHUTDOWN_HOOK, "true");

try {
ClassPathXmlApplicationContext providerContext;
String resourcePath = "org/apache/dubbo/config/spring";
providerContext = new ClassPathXmlApplicationContext(
resourcePath + "/demo-provider.xml",
resourcePath + "/demo-provider-properties.xml");
providerContext.start();

Assertions.assertEquals(true, DubboBootstrap.getInstance().isStarted());
Assertions.assertEquals(false, DubboBootstrap.getInstance().isShutdown());

// close spring context
providerContext.close();

// expect dubbo bootstrap will not be destroyed after closing spring context
Assertions.assertEquals(true, DubboBootstrap.getInstance().isStarted());
Assertions.assertEquals(false, DubboBootstrap.getInstance().isShutdown());
} finally {
SysProps.clear();
}
}
}