forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ralf0131-graceful-shutdown-in-tomcat'
- Loading branch information
Showing
17 changed files
with
536 additions
and
60 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
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> |
133 changes: 133 additions & 0 deletions
133
dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.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,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); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.