diff --git a/src/main/java/be/jaud/Main.java b/src/main/java/be/jaud/Main.java index 9f4c3ac..583bba9 100644 --- a/src/main/java/be/jaud/Main.java +++ b/src/main/java/be/jaud/Main.java @@ -15,22 +15,20 @@ import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; import java.util.HashMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Main { - private static final String version = "0.3"; + private static final String version = "0.4"; private static final HashMap data = new HashMap<>(); public static Settings conf; static OebbCheck check = new OebbCheck(); static long time; static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); - static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); + public static ResilientExecutor service = new ResilientExecutor(1); public static final Logger LOG = LoggerFactory.getLogger(Main.class); public static void main(String[] args) { conf = Settings.getInstance(); - LOG.info("OebbInfo Version " + version + " starting."); + LOG.info("OebbInfo Version {} starting.",version); try { refresh(); } catch (IOException e) { @@ -55,6 +53,16 @@ public class Main { server.setExecutor(null); // creates a default executor server.start(); } + final Thread mainThread = Thread.currentThread(); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + LOG.info("OebbInfo Version {} stopping", version); + service.shutdownNow(); + mainThread.join(); + } catch (InterruptedException e) { + LOG.error("EXCEPTION WHEN STOPPING", e); + } + })); } private static void refresh() throws IOException { diff --git a/src/main/java/be/jaud/OebbCheck.java b/src/main/java/be/jaud/OebbCheck.java index cb6130e..fd840a6 100644 --- a/src/main/java/be/jaud/OebbCheck.java +++ b/src/main/java/be/jaud/OebbCheck.java @@ -2,6 +2,8 @@ package be.jaud; import org.json.JSONArray; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; @@ -12,7 +14,7 @@ import java.net.URISyntaxException; import java.net.URL; public class OebbCheck { - + public static final Logger LOG = LoggerFactory.getLogger(OebbCheck.class); public OebbCheck(){ } @@ -24,26 +26,31 @@ public class OebbCheck { >0 is the delay in Minutes 9999 means the Train has been cancelled */ - public int getStatus(String Station, String Time, String FinalStop, String TrainType, String maxjourneys) throws IOException { - JSONArray journeys; + public int getStatus(String Station, String Time, String FinalStop, String TrainType, String maxjourneys) { + JSONArray journeys = null; try { journeys = getData(Station, Time, maxjourneys); } catch (URISyntaxException e) { - throw new RuntimeException(e); + LOG.error("URISyntaxException: {}", e.toString()); + } catch (IOException e) { + LOG.error("IOException: {}", e.toString()); } - for (Object journey : journeys) { - JSONObject jour = (JSONObject) journey; - if(jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)){ - if(jour.get("rt") instanceof JSONObject data) { - if (data.get("status").equals("Ausfall")) { - return 9999; + if(journeys != null) { + for (Object journey : journeys) { + JSONObject jour = (JSONObject) journey; + if (jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)) { + if (jour.get("rt") instanceof JSONObject data) { + if (data.get("status").equals("Ausfall")) { + return 9999; + } else { + return data.getInt("dlm"); + } } else { - return data.getInt("dlm"); + return 0; } - }else{ - return 0; } } + return -1; } return -1; } diff --git a/src/main/java/be/jaud/ResilientExecutor.java b/src/main/java/be/jaud/ResilientExecutor.java new file mode 100644 index 0000000..e874f35 --- /dev/null +++ b/src/main/java/be/jaud/ResilientExecutor.java @@ -0,0 +1,51 @@ +package be.jaud; + + +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ResilientExecutor extends ScheduledThreadPoolExecutor { + public static final Logger LOG = LoggerFactory.getLogger(ResilientExecutor.class); + + public ResilientExecutor(int corePoolSize) { + super(corePoolSize); + } + + @Override + public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { + return super.scheduleAtFixedRate(wrapRunnable(command), initialDelay, period, unit); + } + + @Override + public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { + return super.scheduleWithFixedDelay(wrapRunnable(command), initialDelay, delay, unit); + } + + private Runnable wrapRunnable(Runnable command) { + return new LogOnExceptionRunnable(command); + } + + private static class LogOnExceptionRunnable implements Runnable { + private final Runnable theRunnable; + + public LogOnExceptionRunnable(Runnable theRunnable) { + super(); + this.theRunnable = theRunnable; + } + + @Override + public void run() { + try { + theRunnable.run(); + } catch (Exception e) { + LOG.error("error in executing: {} It will no longer be run!", this.theRunnable, e); + System.exit(1); + } + } + } + +} \ No newline at end of file