Skip to content

Commit 0ca175c

Browse files
committed
shutdown hook
1 parent 4ac4e07 commit 0ca175c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lts-admin/src/main/java/com/lts/admin/support/SystemInitListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public void contextInitialized(ServletContextEvent servletContextEvent) {
4747
if (StringUtils.isEmpty(ltsMonitorCfgPath)) {
4848
ltsMonitorCfgPath = this.getClass().getResource("/").getPath();
4949
}
50-
MonitorAgentStartup.main(new String[]{ltsMonitorCfgPath});
50+
MonitorAgentStartup.start(ltsMonitorCfgPath);
5151
}
5252
}
5353

5454
@Override
5555
public void contextDestroyed(ServletContextEvent servletContextEvent) {
56-
56+
MonitorAgentStartup.stop();
5757
}
5858
}

lts-monitor/src/main/java/com/lts/monitor/MonitorAgentStartup.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
import com.lts.core.commons.utils.StringUtils;
44

55
import java.util.Map;
6+
import java.util.concurrent.atomic.AtomicBoolean;
67

78
/**
89
* @author Robert HG ([email protected]) on 3/5/16.
910
*/
1011
public class MonitorAgentStartup {
1112

13+
private final static MonitorAgent agent = new MonitorAgent();
14+
private static final AtomicBoolean started = new AtomicBoolean(false);
15+
1216
public static void main(String[] args) {
1317
String cfgPath = args[0];
1418
start(cfgPath);
1519
}
1620

17-
private static void start(String cfgPath) {
21+
public static void start(String cfgPath) {
22+
23+
if (!started.compareAndSet(false, true)) {
24+
return;
25+
}
1826

1927
try {
2028
MonitorCfg cfg = MonitorCfgLoader.load(cfgPath);
2129

22-
final MonitorAgent agent = new MonitorAgent();
23-
2430
agent.setRegistryAddress(cfg.getRegistryAddress());
2531
agent.setClusterName(cfg.getClusterName());
2632
if (StringUtils.isNotEmpty(cfg.getBindIp())) {
@@ -46,4 +52,10 @@ public void run() {
4652
}
4753
}
4854

55+
public static void stop() {
56+
if (started.compareAndSet(true, false)) {
57+
agent.stop();
58+
}
59+
}
60+
4961
}

0 commit comments

Comments
 (0)