aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-04-14 18:03:32 -0700
committerAnton Tananaev <anton@traccar.org>2022-04-14 18:03:32 -0700
commit437a7651096ecdd8e9cabeeca760b9af89b10458 (patch)
tree158176e6025fbf12e28140cc8068fd66055cb1e0 /src/main/java/org/traccar
parent3241b83dc95fa151ac66dd88c102f08cc7cfaa7a (diff)
downloadtrackermap-server-437a7651096ecdd8e9cabeeca760b9af89b10458.tar.gz
trackermap-server-437a7651096ecdd8e9cabeeca760b9af89b10458.tar.bz2
trackermap-server-437a7651096ecdd8e9cabeeca760b9af89b10458.zip
Refactor services
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r--src/main/java/org/traccar/LifecycleObject.java21
-rw-r--r--src/main/java/org/traccar/Main.java21
-rw-r--r--src/main/java/org/traccar/ServerManager.java4
-rw-r--r--src/main/java/org/traccar/TrackerConnector.java6
-rw-r--r--src/main/java/org/traccar/schedule/ScheduleManager.java12
-rw-r--r--src/main/java/org/traccar/web/WebServer.java7
6 files changed, 50 insertions, 21 deletions
diff --git a/src/main/java/org/traccar/LifecycleObject.java b/src/main/java/org/traccar/LifecycleObject.java
new file mode 100644
index 000000000..7af21d528
--- /dev/null
+++ b/src/main/java/org/traccar/LifecycleObject.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed 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.traccar;
+
+public interface LifecycleObject {
+ void start() throws Exception;
+ void stop();
+}
diff --git a/src/main/java/org/traccar/Main.java b/src/main/java/org/traccar/Main.java
index 63e5c1f90..570f3c6ad 100644
--- a/src/main/java/org/traccar/Main.java
+++ b/src/main/java/org/traccar/Main.java
@@ -27,6 +27,9 @@ import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Locale;
import java.util.Timer;
@@ -123,11 +126,16 @@ public final class Main {
LOGGER.info("Version: " + Main.class.getPackage().getImplementationVersion());
LOGGER.info("Starting server...");
- Context.getServerManager().start();
+ List<LifecycleObject> services = new LinkedList<>();
+ services.add(Context.getServerManager());
if (Context.getWebServer() != null) {
- Context.getWebServer().start();
+ services.add(Context.getWebServer());
+ }
+ services.add(Context.getScheduleManager());
+
+ for (LifecycleObject service : services) {
+ service.start();
}
- Context.getScheduleManager().start();
scheduleHealthCheck();
@@ -136,11 +144,10 @@ public final class Main {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("Shutting down server...");
- Context.getScheduleManager().stop();
- if (Context.getWebServer() != null) {
- Context.getWebServer().stop();
+ Collections.reverse(services);
+ for (LifecycleObject service : services) {
+ service.stop();
}
- Context.getServerManager().stop();
}));
} catch (Exception e) {
LOGGER.error("Main method error", e);
diff --git a/src/main/java/org/traccar/ServerManager.java b/src/main/java/org/traccar/ServerManager.java
index 2e2cf7cff..15faf9f2b 100644
--- a/src/main/java/org/traccar/ServerManager.java
+++ b/src/main/java/org/traccar/ServerManager.java
@@ -29,7 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-public class ServerManager {
+public class ServerManager implements LifecycleObject {
private static final Logger LOGGER = LoggerFactory.getLogger(ServerManager.class);
@@ -50,6 +50,7 @@ public class ServerManager {
return protocolList.get(name);
}
+ @Override
public void start() throws Exception {
for (TrackerConnector connector: connectorList) {
try {
@@ -62,6 +63,7 @@ public class ServerManager {
}
}
+ @Override
public void stop() {
for (TrackerConnector connector: connectorList) {
connector.stop();
diff --git a/src/main/java/org/traccar/TrackerConnector.java b/src/main/java/org/traccar/TrackerConnector.java
index 9e2d27ae5..fc6e93399 100644
--- a/src/main/java/org/traccar/TrackerConnector.java
+++ b/src/main/java/org/traccar/TrackerConnector.java
@@ -17,7 +17,7 @@ package org.traccar;
import io.netty.channel.group.ChannelGroup;
-public interface TrackerConnector {
+public interface TrackerConnector extends LifecycleObject {
boolean isDatagram();
@@ -25,8 +25,4 @@ public interface TrackerConnector {
ChannelGroup getChannelGroup();
- void start() throws Exception;
-
- void stop();
-
}
diff --git a/src/main/java/org/traccar/schedule/ScheduleManager.java b/src/main/java/org/traccar/schedule/ScheduleManager.java
index 5d5054100..d43285451 100644
--- a/src/main/java/org/traccar/schedule/ScheduleManager.java
+++ b/src/main/java/org/traccar/schedule/ScheduleManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 - 2021 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,29 +15,29 @@
*/
package org.traccar.schedule;
+import org.traccar.LifecycleObject;
+
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-public class ScheduleManager {
+public class ScheduleManager implements LifecycleObject {
private ScheduledExecutorService executor;
+ @Override
public void start() {
-
executor = Executors.newSingleThreadScheduledExecutor();
new TaskDeviceInactivityCheck().schedule(executor);
new TaskWebSocketKeepalive().schedule(executor);
-
}
+ @Override
public void stop() {
-
if (executor != null) {
executor.shutdown();
executor = null;
}
-
}
}
diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java
index 932781156..0ed5d013e 100644
--- a/src/main/java/org/traccar/web/WebServer.java
+++ b/src/main/java/org/traccar/web/WebServer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2021 Anton Tananaev (anton@traccar.org)
+ * Copyright 2012 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
+import org.traccar.LifecycleObject;
import org.traccar.Main;
import org.traccar.api.DateParameterConverterProvider;
import org.traccar.config.Config;
@@ -69,7 +70,7 @@ import java.io.Writer;
import java.net.InetSocketAddress;
import java.util.EnumSet;
-public class WebServer {
+public class WebServer implements LifecycleObject {
private static final Logger LOGGER = LoggerFactory.getLogger(WebServer.class);
@@ -238,6 +239,7 @@ public class WebServer {
}
}
+ @Override
public void start() {
try {
server.start();
@@ -246,6 +248,7 @@ public class WebServer {
}
}
+ @Override
public void stop() {
try {
server.stop();