Example usage for com.google.common.util.concurrent AbstractService AbstractService

List of usage examples for com.google.common.util.concurrent AbstractService AbstractService

Introduction

In this page you can find the example usage for com.google.common.util.concurrent AbstractService AbstractService.

Prototype

protected AbstractService() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:co.cask.cdap.internal.app.runtime.artifact.SystemArtifactLoader.java

@Inject
SystemArtifactLoader(final ArtifactRepository artifactRepository) {
    this.serviceDelegate = new RetryOnStartFailureService(new Supplier<Service>() {
        @Override/*from w  w  w.  ja v a  2s.c  o m*/
        public Service get() {
            return new AbstractService() {
                @Override
                protected void doStart() {
                    String oldUserId = SecurityRequestContext.getUserId();
                    try {
                        SecurityRequestContext.setUserId(Principal.SYSTEM.getName());
                        artifactRepository.addSystemArtifacts();
                        // if there is no exception, all good, continue on
                        notifyStarted();
                    } catch (Exception e) {
                        // transient error, fail it and retry
                        notifyFailed(e);
                    } finally {
                        SecurityRequestContext.setUserId(oldUserId);
                    }
                }

                @Override
                protected void doStop() {
                    notifyStopped();
                }
            };
        }
    }, RetryStrategies.exponentialDelay(200, 5000, TimeUnit.MILLISECONDS));
}

From source file:co.cask.cdap.internal.app.namespace.DefaultNamespaceEnsurer.java

@Inject
public DefaultNamespaceEnsurer(final NamespaceAdmin namespaceAdmin) {
    this.serviceDelegate = new RetryOnStartFailureService(new Supplier<Service>() {
        @Override//from w  w  w . ja  v a 2s  . com
        public Service get() {
            return new AbstractService() {
                @Override
                protected void doStart() {
                    try {
                        namespaceAdmin.createNamespace(Constants.DEFAULT_NAMESPACE_META);
                        // if there is no exception, assume successfully created and break
                        LOG.info("Created default namespace successfully.");
                        notifyStarted();
                    } catch (AlreadyExistsException e) {
                        // default namespace already exists
                        LOG.info("Default namespace already exists.");
                        notifyStarted();
                    } catch (NamespaceCannotBeCreatedException e) {
                        notifyFailed(e);
                    }
                }

                @Override
                protected void doStop() {
                    notifyStopped();
                }
            };
        }
    }, RetryStrategies.exponentialDelay(200, 5000, TimeUnit.MILLISECONDS));
}

From source file:co.cask.cdap.internal.app.runtime.schedule.DistributedSchedulerService.java

@Inject
public DistributedSchedulerService(TimeScheduler timeScheduler, StreamSizeScheduler streamSizeScheduler,
        CConfiguration cConf, Store store) {
    super(timeScheduler, streamSizeScheduler, cConf, store);
    this.serviceDelegate = new RetryOnStartFailureService(new Supplier<Service>() {
        @Override//from w w w.j  a  v  a2 s. co m
        public Service get() {
            return new AbstractService() {
                @Override
                protected void doStart() {
                    try {
                        startSchedulers();
                        notifyStarted();
                    } catch (SchedulerException e) {
                        notifyFailed(e);
                    }
                }

                @Override
                protected void doStop() {
                    try {
                        stopScheduler();
                        notifyStopped();
                    } catch (SchedulerException e) {
                        notifyFailed(e);
                    }
                }
            };
        }
    }, RetryStrategies.exponentialDelay(200, 5000, TimeUnit.MILLISECONDS));
}

From source file:org.helios.tsdb.plugins.rpc.AbstractRPCService.java

/**
 * Creates a new AbstractRPCService//w  w  w .j  a v a  2 s.c om
 * @param tsdb The parent TSDB instance
 * @param config The extracted configuration
 */
public AbstractRPCService(TSDB tsdb, Properties config) {
    this.tsdb = tsdb;
    this.config = config;
    if (rpcExecutor == null) {
        synchronized (lock) {
            if (rpcExecutor == null) {
                rpcExecutor = new AsyncDispatcherExecutor("RPCService", config);
            }
        }
    }
    final AbstractRPCService thisAbstractService = this;
    abstractService = new AbstractService() {
        @Override
        protected void doStart() {
            try {
                thisAbstractService.doStart();
                notifyStarted();
            } catch (Exception ex) {
                log.error("Failed to start RPC Service", ex);
                notifyFailed(ex);
            }
        }

        @Override
        protected void doStop() {
            thisAbstractService.doStop();
            notifyStopped();
        }
    };

}

From source file:co.cask.tigon.internal.app.runtime.flow.FlowletProgramRunner.java

/**
 * Create a initializer to be executed during the flowlet driver initialization.
 *///from   w  w  w . ja  v a2 s.  c  o m
private Service createServiceHook(String flowletName, Iterable<ConsumerSupplier<?>> consumerSuppliers,
        AtomicReference<FlowletProgramController> controller) {
    final List<String> streams = Lists.newArrayList();
    for (ConsumerSupplier<?> consumerSupplier : consumerSuppliers) {
        QueueName queueName = consumerSupplier.getQueueName();
        if (queueName.isStream()) {
            streams.add(queueName.getSimpleName());
        }
    }

    // If no stream, returns a no-op Service
    if (streams.isEmpty()) {
        return new AbstractService() {
            @Override
            protected void doStart() {
                notifyStarted();
            }

            @Override
            protected void doStop() {
                notifyStopped();
            }
        };
    }
    return new FlowletServiceHook(flowletName, streams, controller);
}

From source file:co.cask.cdap.internal.app.runtime.flow.FlowletProgramRunner.java

/**
 * Create a initializer to be executed during the flowlet driver initialization.
 *//*from   ww  w  .j a  v  a  2 s . c  o m*/
private Service createServiceHook(String flowletName, Iterable<ConsumerSupplier<?>> consumerSuppliers,
        AtomicReference<FlowletProgramController> controller) {
    final List<Id.Stream> streams = Lists.newArrayList();
    for (ConsumerSupplier<?> consumerSupplier : consumerSuppliers) {
        QueueName queueName = consumerSupplier.getQueueName();
        if (queueName.isStream()) {
            streams.add(queueName.toStreamId());
        }
    }

    // If no stream, returns a no-op Service
    if (streams.isEmpty()) {
        return new AbstractService() {
            @Override
            protected void doStart() {
                notifyStarted();
            }

            @Override
            protected void doStop() {
                notifyStopped();
            }
        };
    }
    return new FlowletServiceHook(flowletName, streamCoordinatorClient, streams, controller);
}