
be.bagofwords.application.ApplicationLifeCycle 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
package be.bagofwords.application;
import be.bagofwords.application.annotations.EagerBowComponent;
import be.bagofwords.util.SpringUtils;
import be.bagofwords.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import java.util.List;
@EagerBowComponent
public class ApplicationLifeCycle implements ApplicationListener {
private boolean applicationWasTerminated = false;
@Autowired
private ApplicationContext applicationContext;
public ApplicationLifeCycle() {
}
public synchronized void terminateApplication() {
if (!applicationWasTerminated) {
List extends CloseableComponent> terminatableBeans = SpringUtils.getInstantiatedBeans(applicationContext, CloseableComponent.class);
for (CloseableComponent object : terminatableBeans) {
if (!(object instanceof LateCloseableComponent)) {
object.terminate();
}
}
for (CloseableComponent object : terminatableBeans) {
if (object instanceof LateCloseableComponent) {
object.terminate();
}
}
applicationWasTerminated = true;
}
}
public void waitUntilTerminated() {
while (!applicationWasTerminated) {
Utils.threadSleep(500);
}
}
public boolean applicationWasTerminated() {
return applicationWasTerminated;
}
@Override
public void onApplicationEvent(ContextClosedEvent event) {
terminateApplication();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy