Made executor more resillient
This commit is contained in:
parent
19c2ab446f
commit
43d4e43d99
3 changed files with 84 additions and 18 deletions
|
@ -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 {
|
||||||
|
|
|
@ -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,29 +26,34 @@ 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());
|
||||||
}
|
}
|
||||||
|
if(journeys != null) {
|
||||||
for (Object journey : journeys) {
|
for (Object journey : journeys) {
|
||||||
JSONObject jour = (JSONObject) journey;
|
JSONObject jour = (JSONObject) journey;
|
||||||
if(jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)){
|
if (jour.get("st").equals(FinalStop) && jour.get("pr").equals(TrainType)) {
|
||||||
if(jour.get("rt") instanceof JSONObject data) {
|
if (jour.get("rt") instanceof JSONObject data) {
|
||||||
if (data.get("status").equals("Ausfall")) {
|
if (data.get("status").equals("Ausfall")) {
|
||||||
return 9999;
|
return 9999;
|
||||||
} else {
|
} else {
|
||||||
return data.getInt("dlm");
|
return data.getInt("dlm");
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
private static JSONArray getData(String Station, String Time, String maxjourneys) throws IOException, URISyntaxException {
|
private static JSONArray getData(String Station, String Time, String maxjourneys) throws IOException, URISyntaxException {
|
||||||
URL url = new URI("https://fahrplan.oebb.at/bin/stboard.exe/dn?L=vs_scotty.vs_liveticker&evaId=" + Station + "&boardType=dep&time="+ Time +"&productsFilter=1011110111011&additionalTime=0&maxJourneys=" + maxjourneys + "&outputMode=tickerDataOnly&start=yes&selectDate=today").toURL();
|
URL url = new URI("https://fahrplan.oebb.at/bin/stboard.exe/dn?L=vs_scotty.vs_liveticker&evaId=" + Station + "&boardType=dep&time="+ Time +"&productsFilter=1011110111011&additionalTime=0&maxJourneys=" + maxjourneys + "&outputMode=tickerDataOnly&start=yes&selectDate=today").toURL();
|
||||||
|
|
51
src/main/java/be/jaud/ResilientExecutor.java
Normal file
51
src/main/java/be/jaud/ResilientExecutor.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue