Skip to content
Permalink
Browse files
Merge branch 'ralf0131-graceful-shutdown-in-tomcat'
  • Loading branch information
ralf0131 committed May 17, 2018
2 parents a45206c + 48f34fe commit b48e8bac049e41e43e982b115e91c4f0c5a6775c
Showing 17 changed files with 536 additions and 60 deletions.
@@ -319,6 +319,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-bootstrap</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>hessian-lite</artifactId>
@@ -418,6 +425,7 @@
<include>com.alibaba:dubbo-serialization-fst</include>
<include>com.alibaba:dubbo-serialization-kryo</include>
<include>com.alibaba:dubbo-serialization-jdk</include>
<include>com.alibaba:dubbo-bootstrap</include>
</includes>
</artifactSet>
<transformers>
@@ -0,0 +1,47 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-parent</artifactId>
<groupId>com.alibaba</groupId>
<version>2.6.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-bootstrap</artifactId>


<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-config-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,133 @@
/*
* 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.bootstrap;

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.config.ServiceConfig;
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory;
import com.alibaba.dubbo.rpc.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* A bootstrap class to easily start and stop Dubbo via programmatic API.
* The bootstrap class will be responsible to cleanup the resources during stop.
*/
public class DubboBootstrap {

private static final Logger logger = LoggerFactory.getLogger(DubboBootstrap.class);

/**
* The list of ServiceConfig
*/
private List<ServiceConfig> serviceConfigList;

/**
* Has it already been destroyed or not?
*/
private final AtomicBoolean destroyed;

/**
* The shutdown hook used when Dubbo is running under embedded environment
*/
private Thread shutdownHook;

public DubboBootstrap() {
this.serviceConfigList = new ArrayList<ServiceConfig>();
this.destroyed = new AtomicBoolean(false);
this.shutdownHook = new Thread(new Runnable() {
@Override
public void run() {
if (logger.isInfoEnabled()) {
logger.info("Run shutdown hook now.");
}
destroy();
}
}, "DubboShutdownHook");
}

/**
* Register service config to bootstrap, which will be called during {@link DubboBootstrap#stop()}
* @param serviceConfig the service
* @return the bootstrap instance
*/
public DubboBootstrap regsiterServiceConfig(ServiceConfig serviceConfig) {
serviceConfigList.add(serviceConfig);
return this;
}

public void start() {
registerShutdownHook();
for (ServiceConfig serviceConfig: serviceConfigList) {
serviceConfig.export();
}
}

public void stop() {
for (ServiceConfig serviceConfig: serviceConfigList) {
serviceConfig.unexport();
}
destroy();
removeShutdownHook();
}

/**
* Register the shutdown hook
*/
public void registerShutdownHook() {
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

/**
* Remove this shutdown hook
*/
public void removeShutdownHook() {
try {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
catch (IllegalStateException ex) {
// ignore - VM is already shutting down
}
}

/**
* Destroy all the resources, including registries and protocols.
*/
private void destroy() {
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);
}
}
}
}
@@ -43,19 +43,27 @@ public static boolean isTerminated(Executor executor) {
return false;
}

/**
* Use the shutdown pattern from:
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
* @param executor the Executor to shutdown
* @param timeout the timeout in milliseconds before termination
*/
public static void gracefulShutdown(Executor executor, int timeout) {
if (!(executor instanceof ExecutorService) || isTerminated(executor)) {
return;
}
final ExecutorService es = (ExecutorService) executor;
try {
es.shutdown(); // Disable new tasks from being submitted
// Disable new tasks from being submitted
es.shutdown();
} catch (SecurityException ex2) {
return;
} catch (NullPointerException ex2) {
return;
}
try {
// Wait a while for existing tasks to terminate
if (!es.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {
es.shutdownNow();
}
@@ -73,18 +73,6 @@ public abstract class AbstractConfig implements Serializable {
legacyProperties.put("dubbo.service.url", "dubbo.service.address");
}

static {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (logger.isInfoEnabled()) {
logger.info("Run shutdown hook now.");
}
ProtocolConfig.destroyAll();
}
}, "DubboShutdownHook"));
}

protected String id;

private static String convertLegacyValue(String key, String value) {
@@ -21,7 +21,6 @@
import com.alibaba.dubbo.common.status.StatusChecker;
import com.alibaba.dubbo.common.threadpool.ThreadPool;
import com.alibaba.dubbo.config.support.Parameter;
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory;
import com.alibaba.dubbo.remoting.Codec;
import com.alibaba.dubbo.remoting.Dispatcher;
import com.alibaba.dubbo.remoting.Transporter;
@@ -30,7 +29,6 @@
import com.alibaba.dubbo.rpc.Protocol;

import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* ProtocolConfig
@@ -135,8 +133,6 @@ public class ProtocolConfig extends AbstractConfig {
// if it's default
private Boolean isDefault;

private static final AtomicBoolean destroyed = new AtomicBoolean(false);

public ProtocolConfig() {
}

@@ -149,27 +145,6 @@ public ProtocolConfig(String name, int port) {
setPort(port);
}

// TODO: 2017/8/30 to move this method somewhere else
public static void destroyAll() {
if (!destroyed.compareAndSet(false, true)) {
return;
}

AbstractRegistryFactory.destroyAll();

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);
}
}
}

@Parameter(excluded = true)
public String getName() {
return name;
@@ -18,7 +18,6 @@

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.config.support.Parameter;
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory;

import java.util.Map;

@@ -96,13 +95,9 @@ public RegistryConfig(String address) {
setAddress(address);
}

public static void destroyAll() {
AbstractRegistryFactory.destroyAll();
}

@Deprecated
public static void closeAll() {
destroyAll();
public RegistryConfig(String address, String protocol) {
setAddress(address);
setProtocol(protocol);
}

public String getProtocol() {
@@ -17,7 +17,6 @@

package com.alibaba.dubbo.config;

import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.mock.MockProtocol2;
import com.alibaba.dubbo.rpc.Protocol;
import org.junit.Test;
@@ -33,15 +32,6 @@
import static org.junit.Assert.assertThat;

public class ProtocolConfigTest {
@Test
public void testDestroyAll() throws Exception {
Protocol protocol = Mockito.mock(Protocol.class);
MockProtocol2.delegate = protocol;
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class);
loader.getExtension("mockprotocol2");
ProtocolConfig.destroyAll();
Mockito.verify(protocol).destroy();
}

@Test
public void testDestroy() throws Exception {

0 comments on commit b48e8ba

Please sign in to comment.