Skip to content

Commit

Permalink
support disable shutdown hook (#8369)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylixs committed Jul 29, 2021
1 parent 3d1d326 commit b48b792
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
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();
}
}
}

0 comments on commit b48b792

Please sign in to comment.