aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/Main.java')
-rw-r--r--src/main/java/org/traccar/Main.java46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/main/java/org/traccar/Main.java b/src/main/java/org/traccar/Main.java
index 6daf72bb4..e34fbb72a 100644
--- a/src/main/java/org/traccar/Main.java
+++ b/src/main/java/org/traccar/Main.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2020 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.
@@ -19,6 +19,13 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.traccar.broadcast.BroadcastService;
+import org.traccar.helper.model.DeviceUtil;
+import org.traccar.schedule.ScheduleManager;
+import org.traccar.storage.DatabaseModule;
+import org.traccar.storage.Storage;
+import org.traccar.web.WebModule;
+import org.traccar.web.WebServer;
import java.io.File;
import java.lang.management.ManagementFactory;
@@ -26,10 +33,10 @@ 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.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
public final class Main {
@@ -110,31 +117,36 @@ public final class Main {
public static void run(String configFile) {
try {
- Context.init(configFile);
- injector = Guice.createInjector(new MainModule());
+ injector = Guice.createInjector(new MainModule(configFile), new DatabaseModule(), new WebModule());
logSystemInfo();
LOGGER.info("Version: " + Main.class.getPackage().getImplementationVersion());
LOGGER.info("Starting server...");
- List<LifecycleObject> services = new LinkedList<>();
- services.add(Context.getServerManager());
- if (Context.getWebServer() != null) {
- services.add(Context.getWebServer());
+ if (injector.getInstance(BroadcastService.class).singleInstance()) {
+ DeviceUtil.resetStatus(injector.getInstance(Storage.class));
}
- services.add(Context.getScheduleManager());
- for (LifecycleObject service : services) {
+ var services = Stream.of(
+ ServerManager.class, WebServer.class, ScheduleManager.class, BroadcastService.class)
+ .map(injector::getInstance)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
+
+ for (var service : services) {
service.start();
}
Thread.setDefaultUncaughtExceptionHandler((t, e) -> LOGGER.error("Thread exception", e));
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
- LOGGER.info("Shutting down server...");
-
- Collections.reverse(services);
- for (LifecycleObject service : services) {
- service.stop();
+ LOGGER.info("Stopping server...");
+
+ for (var service : services) {
+ try {
+ service.stop();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
}
}));
} catch (Exception e) {