Skip to content

Commit

Permalink
Merge branch 'ralf0131-graceful-shutdown-in-tomcat'
Browse files Browse the repository at this point in the history
  • Loading branch information
ralf0131 committed May 17, 2018
2 parents 06e5e67 + 7dac296 commit c784fa0
Show file tree
Hide file tree
Showing 17 changed files with 536 additions and 60 deletions.
8 changes: 8 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
47 changes: 47 additions & 0 deletions dubbo-bootstrap/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +29,6 @@
import com.alibaba.dubbo.rpc.Protocol;

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

/**
* ProtocolConfig
Expand Down Expand Up @@ -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() {
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit c784fa0

Please sign in to comment.