
be.bagofwords.application.ApplicationManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bow-utils Show documentation
Show all versions of bow-utils Show documentation
Utility classes that are used in the count-db project and other bow-* projects
The newest version!
package be.bagofwords.application;
import be.bagofwords.ui.UI;
import java.util.Map;
import java.util.Set;
public class ApplicationManager {
public static void runSafely(MainClass main, Map config, BaseApplicationContextFactory factory) {
try {
ApplicationContext applicationContext = new ApplicationContext(config);
factory.wireApplicationContext(applicationContext);
main.run(applicationContext);
} catch (Throwable exp) {
UI.writeError("Received unexpected exception, terminating application.", exp);
}
}
private boolean applicationTerminated;
private ApplicationContext context;
public ApplicationManager(ApplicationContext context) {
this.context = context;
this.applicationTerminated = false;
context.getBean(BowTaskScheduler.class).schedulePeriodicTask(this::checkMainMethodTermination, 1000);
}
public void checkMainMethodTermination() {
if (!applicationTerminated) {
Set threadSet = Thread.getAllStackTraces().keySet();
boolean mainThreadFound = false;
for (Thread thread : threadSet) {
if (thread.getId() == 1) {
mainThreadFound = true;
}
}
if (!mainThreadFound) {
context.terminateApplication();
applicationTerminated = true;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy