diff options
Diffstat (limited to 'src/main/java/org/traccar/Main.java')
-rw-r--r-- | src/main/java/org/traccar/Main.java | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/src/main/java/org/traccar/Main.java b/src/main/java/org/traccar/Main.java index 63e5c1f90..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,7 +19,13 @@ import com.google.inject.Guice; import com.google.inject.Injector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.api.HealthCheckService; +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; @@ -28,7 +34,9 @@ import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.nio.charset.Charset; import java.util.Locale; -import java.util.Timer; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; public final class Main { @@ -107,40 +115,39 @@ public final class Main { } } - private static void scheduleHealthCheck() { - HealthCheckService service = new HealthCheckService(); - if (service.isEnabled()) { - new Timer().scheduleAtFixedRate( - service.createTask(), service.getPeriod(), service.getPeriod()); - } - } - 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..."); - Context.getServerManager().start(); - if (Context.getWebServer() != null) { - Context.getWebServer().start(); + if (injector.getInstance(BroadcastService.class).singleInstance()) { + DeviceUtil.resetStatus(injector.getInstance(Storage.class)); } - Context.getScheduleManager().start(); - scheduleHealthCheck(); + 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..."); - - Context.getScheduleManager().stop(); - if (Context.getWebServer() != null) { - Context.getWebServer().stop(); + LOGGER.info("Stopping server..."); + + for (var service : services) { + try { + service.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } } - Context.getServerManager().stop(); })); } catch (Exception e) { LOGGER.error("Main method error", e); |