Added logging

This commit is contained in:
genuineparts 2024-11-12 15:06:41 +01:00
parent 892f5fe56b
commit c34ecfebac
3 changed files with 72 additions and 11 deletions

View file

@ -1,5 +1,7 @@
package be.jaud;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
@ -16,30 +18,43 @@ 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.2";
private static final HashMap<String, Integer> 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 final Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws IOException {
public static void main(String[] args) {
conf = Settings.getInstance();
refresh();
HttpServer server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0);
server.createContext("/oebbapi", new OebbHandler());
server.setExecutor(null); // creates a default executor
LOG.info("OebbInfo Version " + version + " starting.");
try {
refresh();
} catch (IOException e) {
LOG.error("Error at refresh: {}", e.toString());
}
HttpServer server = null;
try {
server = HttpServer.create(new InetSocketAddress(conf.getInt("port")), 0);
} catch (IOException e) {
LOG.error("Error at HttpServer: {}", e.toString());
}
Runnable task = () -> {
try {
refresh();
} catch (IOException e) {
throw new RuntimeException(e);
LOG.error("Error at Task: {}", e.toString());
}
};
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
server.start();
if (server != null) {
server.createContext("/oebbapi", new OebbHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
}
private static void refresh() throws IOException {
@ -61,7 +76,7 @@ public class Main {
os.write(response.getBytes());
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
LOG.error("Error at HttpRequest: {}", e.toString());
}
}
}