Made executor more resillient

This commit is contained in:
genuineparts 2024-11-12 20:53:29 +01:00
parent 19c2ab446f
commit 43d4e43d99
3 changed files with 84 additions and 18 deletions

View file

@ -15,22 +15,20 @@ import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class Main { public class Main {
private static final String version = "0.3"; private static final String version = "0.4";
private static final HashMap<String, Integer> data = new HashMap<>(); private static final HashMap<String, Integer> data = new HashMap<>();
public static Settings conf; public static Settings conf;
static OebbCheck check = new OebbCheck(); static OebbCheck check = new OebbCheck();
static long time; static long time;
static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 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 final Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) { public static void main(String[] args) {
conf = Settings.getInstance(); conf = Settings.getInstance();
LOG.info("OebbInfo Version " + version + " starting."); LOG.info("OebbInfo Version {} starting.",version);
try { try {
refresh(); refresh();
} catch (IOException e) { } catch (IOException e) {
@ -55,6 +53,16 @@ public class Main {
server.setExecutor(null); // creates a default executor server.setExecutor(null); // creates a default executor
server.start(); 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 { private static void refresh() throws IOException {

View file

@ -2,6 +2,8 @@ package be.jaud;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -12,7 +14,7 @@ import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
public class OebbCheck { public class OebbCheck {
public static final Logger LOG = LoggerFactory.getLogger(OebbCheck.class);
public OebbCheck(){ public OebbCheck(){
} }
@ -24,26 +26,31 @@ public class OebbCheck {
>0 is the delay in Minutes >0 is the delay in Minutes
9999 means the Train has been cancelled 9999 means the Train has been cancelled
*/ */
public int getStatus(String Station, String Time, String FinalStop, String TrainType, String maxjourneys) throws IOException { public int getStatus(String Station, String Time, String FinalStop, String TrainType, String maxjourneys) {
JSONArray journeys; JSONArray journeys = null;
try { try {
journeys = getData(Station, Time, maxjourneys); journeys = getData(Station, Time, maxjourneys);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); LOG.error("URISyntaxException: {}", e.toString());
} catch (IOException e) {
LOG.error("IOException: {}", e.toString());
} }
for (Object journey : journeys) { if(journeys != null) {
JSONObject jour = (JSONObject) journey; for (Object journey : journeys) {
if(jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)){ JSONObject jour = (JSONObject) journey;
if(jour.get("rt") instanceof JSONObject data) { if (jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)) {
if (data.get("status").equals("Ausfall")) { if (jour.get("rt") instanceof JSONObject data) {
return 9999; if (data.get("status").equals("Ausfall")) {
return 9999;
} else {
return data.getInt("dlm");
}
} else { } else {
return data.getInt("dlm"); return 0;
} }
}else{
return 0;
} }
} }
return -1;
} }
return -1; return -1;
} }

View file

@ -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);
}
}
}
}