-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1820, improve graceful shutdown.
- Loading branch information
Showing
9 changed files
with
197 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/DubboShutdownHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 com.alibaba.dubbo.config; | ||
|
||
import com.alibaba.dubbo.common.extension.ExtensionLoader; | ||
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory; | ||
import com.alibaba.dubbo.rpc.Protocol; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* The shutdown hook thread to do the clean up stuff. | ||
* This is a singleton in order to ensure there is only one shutdown hook registered. | ||
* Because {@link ApplicationShutdownHooks} use {@link java.util.IdentityHashMap} | ||
* to store the shutdown hooks. | ||
*/ | ||
public class DubboShutdownHook extends Thread { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DubboShutdownHook.class); | ||
|
||
private static final DubboShutdownHook dubboShutdownHook = new DubboShutdownHook("DubboShutdownHook"); | ||
|
||
public static DubboShutdownHook getDubboShutdownHook() { | ||
return dubboShutdownHook; | ||
} | ||
|
||
/** | ||
* Has it already been destroyed or not? | ||
*/ | ||
private final AtomicBoolean destroyed; | ||
|
||
private DubboShutdownHook(String name) { | ||
super(name); | ||
this.destroyed = new AtomicBoolean(false); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (logger.isInfoEnabled()) { | ||
logger.info("Run shutdown hook now."); | ||
} | ||
destroyAll(); | ||
} | ||
|
||
/** | ||
* Destroy all the resources, including registries and protocols. | ||
*/ | ||
public void destroyAll() { | ||
if (!destroyed.compareAndSet(false, true)) { | ||
return; | ||
} | ||
// destroy all the registries | ||
AbstractRegistryFactory.destroyAll(); | ||
// destroy all the protocols | ||
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class); | ||
for (String protocolName : loader.getLoadedExtensions()) { | ||
try { | ||
Protocol protocol = loader.getLoadedExtension(protocolName); | ||
if (protocol != null) { | ||
protocol.destroy(); | ||
} | ||
} catch (Throwable t) { | ||
logger.warn(t.getMessage(), t); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...c/test/java/com/alibaba/dubbo/config/spring/initializer/DubboApplicationListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 com.alibaba.dubbo.config.spring.initializer; | ||
|
||
import com.alibaba.dubbo.config.DubboShutdownHook; | ||
import org.apache.dubbo.bootstrap.DubboBootstrap; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
public class DubboApplicationListenerTest { | ||
|
||
@Test | ||
public void testTwoShutdownHook() { | ||
DubboShutdownHook spyHook = Mockito.spy(DubboShutdownHook.getDubboShutdownHook()); | ||
ClassPathXmlApplicationContext applicationContext = getApplicationContext(spyHook, true); | ||
applicationContext.refresh(); | ||
applicationContext.close(); | ||
// shutdown hook can't be verified, because it will executed after main thread has finished. | ||
// so we can only verify it by manually run it. | ||
try { | ||
spyHook.start(); | ||
spyHook.join(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
Mockito.verify(spyHook, Mockito.times(2)).destroyAll(); | ||
} | ||
|
||
@Test | ||
public void testOneShutdownHook() { | ||
DubboShutdownHook spyHook = Mockito.spy(DubboShutdownHook.getDubboShutdownHook()); | ||
ClassPathXmlApplicationContext applicationContext = getApplicationContext(spyHook, false); | ||
applicationContext.refresh(); | ||
applicationContext.close(); | ||
Mockito.verify(spyHook, Mockito.times(1)).destroyAll(); | ||
} | ||
|
||
private ClassPathXmlApplicationContext getApplicationContext(DubboShutdownHook hook, boolean registerHook) { | ||
DubboBootstrap bootstrap = new DubboBootstrap(registerHook, hook); | ||
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); | ||
applicationContext.addApplicationListener(new DubboApplicationListener(bootstrap)); | ||
return applicationContext; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters