Skip to content

Commit

Permalink
服务停止 优雅关闭优化
Browse files Browse the repository at this point in the history
  • Loading branch information
闫逍旭 committed Aug 20, 2016
1 parent 12ac984 commit 2e3dc69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
35 changes: 30 additions & 5 deletions mpush-boot/src/main/java/com/mpush/bootstrap/Main.java
Expand Up @@ -20,9 +20,10 @@
package com.mpush.bootstrap; package com.mpush.bootstrap;


import com.mpush.tools.log.Logs; import com.mpush.tools.log.Logs;
import sun.misc.Signal;
import sun.misc.SignalHandler;


public class Main { public class Main {

public static void main(String[] args) { public static void main(String[] args) {
Logs.init(); Logs.init();
Logs.Console.error("launch mpush server..."); Logs.Console.error("launch mpush server...");
Expand All @@ -32,11 +33,35 @@ public static void main(String[] args) {
} }


private static void addHook(final ServerLauncher launcher) { private static void addHook(final ServerLauncher launcher) {
Runtime.getRuntime().addShutdownHook(new Thread() { Hook hook = new Hook(launcher);
public void run() { //Signal.handle(new Signal("USR2"), hook);
Runtime.getRuntime().addShutdownHook(new Thread(hook, "mpush-hook-thread"));
}

private static class Hook implements Runnable, SignalHandler {
private final ServerLauncher launcher;

private Hook(ServerLauncher launcher) {
this.launcher = launcher;
}

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

@Override
public void handle(Signal signal) {
stop();
}

private void stop() {
try {
launcher.stop(); launcher.stop();
Logs.Console.error("jvm exit, all server stopped..."); } catch (Exception e) {
Logs.Console.error("mpush server stop ex", e);
} }
}); Logs.Console.error("jvm exit, all server stopped...");
}
} }
} }
23 changes: 13 additions & 10 deletions mpush-boot/src/main/java/com/mpush/bootstrap/ServerLauncher.java
Expand Up @@ -20,7 +20,8 @@
package com.mpush.bootstrap; package com.mpush.bootstrap;




import com.mpush.api.service.Server; import com.mpush.api.service.BaseService;
import com.mpush.api.service.Service;
import com.mpush.bootstrap.job.*; import com.mpush.bootstrap.job.*;
import com.mpush.core.server.AdminServer; import com.mpush.core.server.AdminServer;
import com.mpush.core.server.ConnectionServer; import com.mpush.core.server.ConnectionServer;
Expand All @@ -30,6 +31,8 @@
import com.mpush.zk.ZKClient; import com.mpush.zk.ZKClient;
import com.mpush.zk.node.ZKServerNode; import com.mpush.zk.node.ZKServerNode;


import java.util.concurrent.TimeUnit;

/** /**
* Created by yxx on 2016/5/14. * Created by yxx on 2016/5/14.
* *
Expand Down Expand Up @@ -61,17 +64,17 @@ public void start() {
chain.run(); chain.run();
} }


public void stop() { public void stop() throws Exception {
stopServer(connectServer); stopService(connectServer);
stopServer(gatewayServer); stopService(gatewayServer);
stopServer(adminServer); stopService(adminServer);
ZKClient.I.stop(); stopService(ZKClient.I);
MonitorService.I.stop(); stopService(MonitorService.I);
} }


private void stopServer(Server server) { private void stopService(Service service) throws Exception {
if (server != null) { if (service != null) {
server.stop(null); service.stop().get(1, TimeUnit.MINUTES);
} }
} }
} }

0 comments on commit 2e3dc69

Please sign in to comment.