Search in sources :

Example 1 with ServiceDelegateEntity

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()));
    };
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ServiceDelegateEntity(org.hobbit.core.service.api.ServiceDelegateEntity) Logger(org.slf4j.Logger) ServiceCapableWrapper(org.aksw.commons.service.core.ServiceCapableWrapper) ApplicationRunner(org.springframework.boot.ApplicationRunner) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ApplicationListener(org.springframework.context.ApplicationListener) Function(java.util.function.Function) ApplicationContext(org.springframework.context.ApplicationContext) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) State(com.google.common.util.concurrent.Service.State) Listener(com.google.common.util.concurrent.Service.Listener) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Bean(org.springframework.context.annotation.Bean) ServiceCapable(org.hobbit.core.service.api.ServiceCapable) ApplicationListener(org.springframework.context.ApplicationListener) Listener(com.google.common.util.concurrent.Service.Listener) State(com.google.common.util.concurrent.Service.State) TimeoutException(java.util.concurrent.TimeoutException) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) TimeoutException(java.util.concurrent.TimeoutException) Bean(org.springframework.context.annotation.Bean)

Aggregations

MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 Listener (com.google.common.util.concurrent.Service.Listener)1 State (com.google.common.util.concurrent.Service.State)1 Objects (java.util.Objects)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Function (java.util.function.Function)1 ServiceCapableWrapper (org.aksw.commons.service.core.ServiceCapableWrapper)1 ServiceCapable (org.hobbit.core.service.api.ServiceCapable)1 ServiceDelegateEntity (org.hobbit.core.service.api.ServiceDelegateEntity)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ApplicationRunner (org.springframework.boot.ApplicationRunner)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ApplicationListener (org.springframework.context.ApplicationListener)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)1