use of org.hobbit.core.service.api.ServiceDelegateEntity in project facete3 by hobbit-project.
the class LauncherServiceCapable method serviceLauncher.
@Bean
public ApplicationRunner serviceLauncher(ServiceCapable serviceCapable, ConfigurableApplicationContext ctx) {
ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext) getRoot(ctx, ApplicationContext::getParent);
// ConfigurableApplicationContext rootCtx = ctx;
ServiceDelegateEntity<? extends ServiceCapable> activeService = ServiceCapableWrapper.wrap(serviceCapable);
// Add a listener that closes the service's (root) context on service termination
activeService.addListener(new Listener() {
@Override
public void failed(State priorState, Throwable t) {
logger.info("ServiceCapable service wrapped [FAILED] for " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()), t);
// logger.info("ServiceCapable service wrapper stopped");
// ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext)getRoot(ctx.getParent(), ApplicationContext::getParent);
rootCtx.close();
}
@Override
public void terminated(State priorState) {
logger.info("ServiceCapable service wrapper [TERMINATED] for " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
// logger.info("ServiceCapable service wrapper stopped");
// ConfigurableApplicationContext rootCtx = (ConfigurableApplicationContext)getRoot(ctx.getParent(), ApplicationContext::getParent);
rootCtx.close();
}
}, MoreExecutors.directExecutor());
// If the service's context gets closed, terminate the service
ctx.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
logger.info("Context is closing - shutdown service " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
if (activeService != null && activeService.isRunning()) {
try {
activeService.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
// thread.interrupt();
} catch (TimeoutException e) {
try {
activeService.stopAsync().awaitTerminated(10, TimeUnit.SECONDS);
} catch (Exception f) {
throw new RuntimeException(f);
}
// e.printStackTrace();
throw new RuntimeException(e);
}
}
}
});
return args -> {
logger.info("LauncherServiceCapable::ApplicationRunner starting service... " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
// activeService.startAsync().awaitRunning();
activeService.startAsync().awaitTerminated();
logger.info("LauncherServiceCapable::ApplicationRunner service started... " + (activeService == null ? "(no active service)" : activeService.getEntity().getClass()));
};
}
Aggregations