Example usage for com.google.common.util.concurrent Service toString

List of usage examples for com.google.common.util.concurrent Service toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:gobblin.scheduler.Worker.java

/**
 * Start this worker./*from  ww  w .jav a 2  s . c  o m*/
 */
public void start() {
    this.serviceManager.addListener(new ServiceManager.Listener() {

        @Override
        public void stopped() {
            LOG.info("Worker has been stopped");
        }

        @Override
        public void healthy() {
            LOG.info("All services are health and running");
            // Report services' uptimes
            Map<Service, Long> startupTimes = serviceManager.startupTimes();
            for (Map.Entry<Service, Long> entry : startupTimes.entrySet()) {
                LOG.info(String.format("Service %s is healthy with an uptime of %dms",
                        entry.getKey().toString(), entry.getValue()));
            }
        }

        @Override
        public void failure(Service service) {
            LOG.error(String.format("Service %s failed for the following reason:%n\t%s", service.toString(),
                    service.failureCause().toString()));
            System.exit(1);
        }
    }, Executors.newSingleThreadExecutor(ExecutorsUtils.newThreadFactory(Optional.of(LOG))));

    // Add a shutdown hook so the task scheduler gets properly shutdown
    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            // Give the services 5 seconds to stop to ensure that we are
            // responsive to shutdown requests
            LOG.info("Shutting down the worker");
            try {
                serviceManager.stopAsync().awaitStopped(5, TimeUnit.SECONDS);
            } catch (TimeoutException te) {
                LOG.error("Timeout in stopping the service manager", te);
            }
        }
    });

    LOG.info("Starting the worker with configured services");
    // Start the worker
    this.serviceManager.startAsync();
}