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

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

Introduction

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

Prototype

void awaitRunning();

Source Link

Document

Waits for the Service to reach the State#RUNNING running state .

Usage

From source file:com.torodb.standalone.Main.java

public static void main(String[] args) throws Exception {
    Console console = JCommander.getConsole();

    ResourceBundle cliBundle = PropertyResourceBundle.getBundle("CliMessages");
    final CliConfig cliConfig = new CliConfig();
    JCommander jCommander = new JCommander(cliConfig, cliBundle, args);
    jCommander.setColumnSize(Integer.MAX_VALUE);

    if (cliConfig.isHelp()) {
        jCommander.usage();/*  w w  w  .  j  a v a  2  s  .  c  o  m*/
        System.exit(0);
    }

    if (cliConfig.isHelpParam()) {
        console.println(cliBundle.getString("help-param-header"));
        ResourceBundle configBundle = PropertyResourceBundle.getBundle("ConfigMessages");
        ConfigUtils.printParamDescriptionFromConfigSchema(Config.class, configBundle, console, 0);
        System.exit(0);
    }

    final Config config = CliConfigUtils.readConfig(cliConfig);

    if (cliConfig.isPrintConfig()) {
        ConfigUtils.printYamlConfig(config, console);

        System.exit(0);
    }

    if (cliConfig.isPrintXmlConfig()) {
        ConfigUtils.printXmlConfig(config, console);

        System.exit(0);
    }

    configureLogger(cliConfig, config);

    config.getBackend().getBackendImplementation().accept(new BackendImplementationVisitor() {
        @Override
        public void visit(AbstractDerby value) {
            parseToropassFile(value);
        }

        @Override
        public void visit(AbstractPostgres value) {
            parseToropassFile(value);
        }

        public void parseToropassFile(BackendPasswordConfig value) {
            try {
                ConfigUtils.parseToropassFile(value);
            } catch (Exception ex) {
                throw new SystemException(ex);
            }
        }
    });
    if (config.getProtocol().getMongo().getReplication() != null) {
        for (AbstractReplication replication : config.getProtocol().getMongo().getReplication()) {
            if (replication.getAuth().getUser() != null) {
                HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource())
                        .withDefaultPort(27017);
                ConfigUtils.parseMongopassFile(new MongoPasswordConfig() {

                    @Override
                    public void setPassword(String password) {
                        replication.getAuth().setPassword(password);
                    }

                    @Override
                    public String getUser() {
                        return replication.getAuth().getUser();
                    }

                    @Override
                    public Integer getPort() {
                        return syncSource.getPort();
                    }

                    @Override
                    public String getPassword() {
                        return replication.getAuth().getPassword();
                    }

                    @Override
                    public String getMongopassFile() {
                        return config.getProtocol().getMongo().getMongopassFile();
                    }

                    @Override
                    public String getHost() {
                        return syncSource.getHostText();
                    }

                    @Override
                    public String getDatabase() {
                        return replication.getAuth().getSource();
                    }
                });
            }
        }
    }

    if (config.getBackend().isLike(AbstractPostgres.class)) {
        AbstractPostgres postgres = config.getBackend().as(AbstractPostgres.class);

        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + postgres.getUser() + " password:");
            postgres.setPassword(readPwd());
        }
    } else if (config.getBackend().isLike(AbstractDerby.class)) {
        AbstractDerby derby = config.getBackend().as(AbstractDerby.class);

        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + derby.getUser() + " password:");
            derby.setPassword(readPwd());
        }
    }

    try {
        Clock clock = Clock.systemDefaultZone();
        Service server;
        if (config.getProtocol().getMongo().getReplication() == null
                || config.getProtocol().getMongo().getReplication().isEmpty()) {
            Service toroDbServer = ToroDbBootstrap.createStandaloneService(config, clock);

            toroDbServer.startAsync();
            toroDbServer.awaitRunning();

            server = toroDbServer;
        } else {
            throw new UnsupportedOperationException("Replication not supported yet!");
        }

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            server.stopAsync();
            server.awaitTerminated();
        }));
    } catch (CreationException ex) {
        ex.getErrorMessages().stream().forEach(m -> {
            if (m.getCause() != null) {
                LOGGER.error(m.getCause().getMessage());
            } else {
                LOGGER.error(m.getMessage());
            }
        });
        System.exit(1);
    } catch (Throwable ex) {
        LOGGER.error("Fatal error on initialization", ex);
        Throwable rootCause = Throwables.getRootCause(ex);
        String causeMessage = rootCause.getMessage();
        JCommander.getConsole().println("Fatal error while ToroDB was starting: " + causeMessage);
        System.exit(1);
    }
}

From source file:co.runrightfast.core.utils.ServiceUtils.java

static void start(@NonNull final Service service) {
    switch (service.state()) {
    case NEW://from   w  ww. java  2  s. co  m
        service.startAsync();
        awaitRunning(service);
        return;
    case STARTING:
        service.awaitRunning();
        awaitRunning(service);
        return;
    case RUNNING:
        return;
    default:
        throw new IllegalStateException(
                "Service cannot be started because the service state is :" + service.state());
    }

}

From source file:co.runrightfast.commons.utils.ServiceUtils.java

public static void start(@NonNull final Service service) {
    switch (service.state()) {
    case NEW:/*ww w . j a v  a2  s  .  c  o m*/
        service.startAsync();
        awaitRunning(service);
        return;
    case STARTING:
        service.awaitRunning();
        awaitRunning(service);
        return;
    case RUNNING:
        return;
    default:
        throw new IllegalServiceStateTransitionException(service.state(), RUNNING);
    }

}

From source file:com.torodb.core.bundle.DependenciesBundle.java

@Override
protected void postDependenciesStartUp() throws Exception {
    for (Service managedDependency : getManagedDependencies()) {
        managedDependency.startAsync();/*  w  w  w  .  ja va2s  .  co  m*/
        managedDependency.awaitRunning();
    }
}

From source file:com.torodb.stampede.StampedeService.java

private void startBundle(Service service) {
    service.startAsync();
    service.awaitRunning();

    shutdowner.addStopShutdownListener(service);
}