Search in sources :

Example 11 with ServiceManager

use of com.google.common.util.concurrent.ServiceManager in project incubator-gobblin by apache.

the class ServiceBasedAppLauncher method start.

/**
 * Starts the {@link ApplicationLauncher} by starting all associated services. This method also adds a shutdown hook
 * that invokes {@link #stop()} and the {@link #close()} methods. So {@link #stop()} and {@link #close()} need not be
 * called explicitly; they can be triggered during the JVM shutdown.
 */
@Override
public synchronized void start() {
    if (this.hasStarted) {
        LOG.warn("ApplicationLauncher has already started");
        return;
    }
    this.hasStarted = true;
    this.serviceManager = new ServiceManager(this.services);
    // A listener that shutdowns the application if any service fails.
    this.serviceManager.addListener(new ServiceManager.Listener() {

        @Override
        public void failure(Service service) {
            super.failure(service);
            LOG.error(String.format("Service %s has failed.", service.getClass().getSimpleName()), service.failureCause());
            try {
                service.stopAsync();
                ServiceBasedAppLauncher.this.stop();
            } catch (ApplicationException ae) {
                LOG.error("Could not shutdown services gracefully. This may cause the application to hang.");
            }
        }
    });
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                ServiceBasedAppLauncher.this.stop();
            } catch (ApplicationException e) {
                LOG.error("Failed to shutdown application", e);
            } finally {
                try {
                    ServiceBasedAppLauncher.this.close();
                } catch (IOException e) {
                    LOG.error("Failed to close application", e);
                }
            }
        }
    });
    LOG.info("Starting the Gobblin application and all its associated Services");
    // Start the application
    this.serviceManager.startAsync().awaitHealthy();
}
Also used : ServiceManager(com.google.common.util.concurrent.ServiceManager) JMXReportingService(org.apache.gobblin.runtime.services.JMXReportingService) MetricsReportingService(org.apache.gobblin.runtime.services.MetricsReportingService) Service(com.google.common.util.concurrent.Service) IOException(java.io.IOException)

Aggregations

ServiceManager (com.google.common.util.concurrent.ServiceManager)11 Service (com.google.common.util.concurrent.Service)5 Properties (java.util.Properties)4 TimeoutException (java.util.concurrent.TimeoutException)3 StreamClient (co.cask.cdap.client.StreamClient)2 StreamWriter (co.cask.cdap.client.StreamWriter)2 PipeConfiguration (co.cask.cdap.filetailer.config.PipeConfiguration)2 FileTailerMetricsProcessor (co.cask.cdap.filetailer.metrics.FileTailerMetricsProcessor)2 FileTailerQueue (co.cask.cdap.filetailer.queue.FileTailerQueue)2 FileTailerSink (co.cask.cdap.filetailer.sink.FileTailerSink)2 FileTailerStateProcessor (co.cask.cdap.filetailer.state.FileTailerStateProcessor)2 FileTailerStateProcessorImpl (co.cask.cdap.filetailer.state.FileTailerStateProcessorImpl)2 LogTailer (co.cask.cdap.filetailer.tailer.LogTailer)2 File (java.io.File)2 IOException (java.io.IOException)2 TaskExecutor (org.apache.gobblin.runtime.TaskExecutor)2 ConfigurationLoadingException (co.cask.cdap.filetailer.config.exception.ConfigurationLoadingException)1 RetryException (com.github.rholder.retry.RetryException)1 Function (com.google.common.base.Function)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1