Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ShutDownHookListener #443

Merged
merged 4 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions motan-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<artifactId>javapoet</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.weibo.api.motan.closable;

/**
* @author zhanran
* Date: 2017/5/24
*/
public interface Closable<T> {
void close();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.weibo.api.motan.closable;

import com.weibo.api.motan.util.LoggerUtil;
import java.util.ArrayList;
import java.util.Collections;

/**
* @author zhanran
* Date: 2017/5/24
* add a shutDownHook to close some global resources
*/

public class ShutDownHook extends Thread {
//Smaller the priority is,earlier the resource is to be closed,default Priority is 20
private static final int defaultPriority = 20;
//only global resource should be register to ShutDownHook,don't register connections to it.
private static ShutDownHook instance;
private ArrayList<closableObject> resourceList = new ArrayList<closableObject>();

private ShutDownHook() {
}


private static void init() {
if (instance == null) {
instance = new ShutDownHook();
LoggerUtil.info("ShutdownHook is initialized");
}
}

@Override
public void run() {
closeAll();
}

public static void runHook(boolean sync) {
if (instance != null) {
if (sync)
instance.run();
else
instance.start();
}
}

//synchronized method to close all the resources in the list
private synchronized void closeAll() {
Collections.sort(resourceList);
LoggerUtil.info("Start to close global resource due to priority");
for (closableObject resource : resourceList) {
try {
resource.closable.close();
} catch (Exception e) {
LoggerUtil.error("Failed to close " + resource.closable.getClass(), e);
}
LoggerUtil.info("Success to close " + resource.closable.getClass());
}
LoggerUtil.info("Success to close all the resource!");
resourceList.clear();
}

public static void registerShutdownHook(Closable closable) {
registerShutdownHook(closable, defaultPriority);
}

public static synchronized void registerShutdownHook(Closable closable, int priority) {
if (instance == null) {
init();
}
instance.resourceList.add(new closableObject(closable, priority));
LoggerUtil.info("add resource " + closable.getClass() + " to list");
}

private static class closableObject implements Comparable<closableObject> {
Closable closable;
int priority;

public closableObject(Closable closable, int priority) {
this.closable = closable;
this.priority = priority;
}

@Override
public int compareTo(closableObject o) {
if (this.priority > o.priority) return -1;
else if (this.priority == o.priority) return 0;
else return 1;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.weibo.api.motan.closable;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* @author zhanran
* Date: 2017/5/24
* In order to shutdown motan server running in tomcat(run tomcat's shutdown.sh rather than kill PID manually),add ShutDownHookListener to web.xml
* 为了关闭在tomcat中运行的motan server(运行tomcat的shutdown.sh关闭而不是手动kill pid),在web.xml中添加ShutDownHookListener
*/
public class ShutDownHookListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
ShutDownHook.runHook(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import com.weibo.api.motan.common.URLParamType;
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.registry.NotifyListener;
Expand All @@ -52,6 +54,16 @@ public abstract class FailbackRegistry extends AbstractRegistry {
new ConcurrentHashMap<URL, ConcurrentHashSet<NotifyListener>>();

private static ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(1);
static{
ShutDownHook.registerShutdownHook(new Closable() {
@Override
public void close() {
if(!retryExecutor.isShutdown()){
retryExecutor.shutdown();
}
}
});
}

public FailbackRegistry(URL url) {
super(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import com.weibo.api.motan.util.LoggerUtil;

/**
Expand All @@ -34,7 +36,16 @@ public class RefererSupports {

// 正常情况下请求超过1s已经是能够忍耐的极限值了,delay 1s进行destroy
private static final int DELAY_TIME = 1000;

static{
ShutDownHook.registerShutdownHook(new Closable() {
@Override
public void close() {
if(!scheduledExecutor.isShutdown()){
scheduledExecutor.shutdown();
}
}
});
}
public static <T> void delayDestroy(final List<Referer<T>> referers) {
if (referers == null || referers.isEmpty()) {
return;
Expand Down
14 changes: 13 additions & 1 deletion motan-core/src/main/java/com/weibo/api/motan/rpc/RpcStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.weibo.api.motan.rpc;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -39,7 +42,16 @@ public class RpcStats {
new ConcurrentHashMap<String, ConcurrentHashMap<String, StatInfo>>();

private static ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);

static{
ShutDownHook.registerShutdownHook(new Closable() {
@Override
public void close() {
if(!scheduledExecutor.isShutdown()){
scheduledExecutor.shutdown();
}
}
});
}
/**
* call before invoke the request
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.common.URLParamType;
import com.weibo.api.motan.core.extension.ExtensionLoader;
Expand All @@ -39,9 +41,9 @@
/**
* @author maijunsheng
* @version 创建时间:2013-6-14
*
*
*/
public class HeartbeatClientEndpointManager implements EndpointManager {
public class HeartbeatClientEndpointManager implements EndpointManager{

private ConcurrentMap<Client, HeartbeatFactory> endpoints = new ConcurrentHashMap<Client, HeartbeatFactory>();

Expand Down Expand Up @@ -73,6 +75,14 @@ public void run() {

}
}, MotanConstants.HEARTBEAT_PERIOD, MotanConstants.HEARTBEAT_PERIOD, TimeUnit.MILLISECONDS);
ShutDownHook.registerShutdownHook(new Closable() {
@Override
public void close() {
if (!executorService.isShutdown()) {
executorService.shutdown();
}
}
});
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions motan-core/src/main/java/com/weibo/api/motan/util/StatsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Snapshot;
import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.common.URLParamType;
import com.weibo.api.motan.rpc.Application;
Expand Down Expand Up @@ -57,6 +59,14 @@ public void run() {
logStatisticCallback();
}
}, MotanConstants.STATISTIC_PEROID, MotanConstants.STATISTIC_PEROID, TimeUnit.SECONDS);
ShutDownHook.registerShutdownHook(new Closable() {
@Override
public void close() {
if(!executorService.isShutdown()){
executorService.shutdown();
}
}
});
}

public static void registryStatisticCallback(StatisticCallback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

<!-- 注册中心配置 使用不同注册中心需要依赖对应的jar包。-->
<!--<motan:registry regProtocol="local" name="registry"/>-->
<motan:registry regProtocol="local" name="registry"/>
<!--<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>-->
<motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181" connectTimeout="2000"/>
<!--<motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181" connectTimeout="2000"/>-->

<!-- motan协议配置 -->
<motan:protocol default="true" name="motan" haStrategy="failover"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.weibo.api.motan.registry.consul;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import com.weibo.api.motan.common.URLParamType;
import com.weibo.api.motan.registry.consul.client.MotanConsulClient;
import com.weibo.api.motan.registry.support.command.CommandFailbackRegistry;
Expand All @@ -16,7 +18,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ConsulRegistry extends CommandFailbackRegistry {
public class ConsulRegistry extends CommandFailbackRegistry implements Closable {
private MotanConsulClient client;
private ConsulHeartbeatManager heartbeatManager;
private int lookupInterval;
Expand Down Expand Up @@ -49,6 +51,7 @@ public ConsulRegistry(URL url, MotanConsulClient client) {

ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(20000);
notifyExecutor = new ThreadPoolExecutor(10, 30, 30 * 1000, TimeUnit.MILLISECONDS, workQueue);
ShutDownHook.registerShutdownHook(this);
LoggerUtil.info("ConsulRegistry init finish.");
}

Expand Down Expand Up @@ -319,6 +322,11 @@ private void updateCommandCache(String group, String command, boolean needNotify
}
}

@Override
public void close() {
heartbeatManager.close();
}

private class ServiceLookupThread extends Thread {
private String group;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import com.weibo.api.motan.closable.Closable;
import com.weibo.api.motan.closable.ShutDownHook;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
Expand All @@ -39,7 +41,7 @@
import com.weibo.api.motan.util.ConcurrentHashSet;
import com.weibo.api.motan.util.LoggerUtil;

public class ZookeeperRegistry extends CommandFailbackRegistry {
public class ZookeeperRegistry extends CommandFailbackRegistry implements Closable {
private ZkClient zkClient;
private Set<URL> availableServices = new ConcurrentHashSet<URL>();
private ConcurrentHashMap<URL, ConcurrentHashMap<ServiceListener, IZkChildListener>> serviceListeners = new ConcurrentHashMap<URL, ConcurrentHashMap<ServiceListener, IZkChildListener>>();
Expand All @@ -64,6 +66,7 @@ public void handleNewSession() throws Exception {
}
};
zkClient.subscribeStateChanges(zkStateListener);
ShutDownHook.registerShutdownHook(this);
}

public ConcurrentHashMap<URL, ConcurrentHashMap<ServiceListener, IZkChildListener>> getServiceListeners() {
Expand Down Expand Up @@ -369,4 +372,9 @@ private void reconnectClient() {
}
}
}

@Override
public void close() {
this.zkClient.close();
}
}