Example usage for java.util.concurrent Executors newSingleThreadScheduledExecutor

List of usage examples for java.util.concurrent Executors newSingleThreadScheduledExecutor

Introduction

In this page you can find the example usage for java.util.concurrent Executors newSingleThreadScheduledExecutor.

Prototype

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) 

Source Link

Document

Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.

Usage

From source file:com.mustardgrain.solr.SolrClient.java

private void startStatsExecutor() {
    statsExecutor = Executors.newSingleThreadScheduledExecutor(new SolrjNamedThreadFactory("statsExecutor"));
    statsExecutor.scheduleAtFixedRate(getStatsRunner(new WeakReference<SolrClient>(this)), this.statsInterval,
            this.statsInterval, TimeUnit.MILLISECONDS);
}

From source file:org.apache.pulsar.broker.PulsarService.java

protected synchronized ScheduledExecutorService getCompactorExecutor() {
    if (this.compactorExecutor == null) {
        compactorExecutor = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("compaction"));
    }//  w w w .j av a2 s.  com
    return this.compactorExecutor;
}

From source file:net.centro.rtb.monitoringcenter.MonitoringCenter.java

private static synchronized void configure(MonitoringCenterConfig config, boolean internalCall) {
    if (configured.get()) {
        if (internalCall) {
            return;
        } else {// ww w  .ja va2s . c om
            throw new IllegalStateException("MonitoringCenter has already been configured!");
        }
    }

    Preconditions.checkNotNull(config, "config cannot be null");

    NamingConfig namingConfig = config.getNamingConfig();
    prefix = new StringBuilder().append(namingConfig.getApplicationName()).append(MetricNamingUtil.SEPARATOR)
            .append(namingConfig.getDatacenterName()).append(MetricNamingUtil.SEPARATOR)
            .append(namingConfig.getNodeGroupName()).append(MetricNamingUtil.SEPARATOR)
            .append(namingConfig.getNodeId()).toString();

    initialConfig = config;
    currentConfig = config;
    metricRegistry = new MetricRegistry();
    healthCheckRegistry = new HealthCheckRegistry();

    // Set up default metric sets
    MetricCollectionConfig metricCollectionConfig = config.getMetricCollectionConfig();
    if (metricCollectionConfig != null) {
        if (metricCollectionConfig.getEnableSystemMetrics()) {
            systemMetricSet = new SystemMetricSet();
            metricRegistry.register(SYSTEM_METRIC_NAMESPACE, systemMetricSet);
        }

        if (metricCollectionConfig.getEnableTomcatMetrics()) {
            tomcatMetricSet = new TomcatMetricSet();
            metricRegistry.register(TOMCAT_METRIC_NAMESPACE, tomcatMetricSet);
        }
    }

    // Configure reporters
    if (config.getMetricReportingConfig() != null) {
        GraphiteReporterConfig graphiteReporterConfig = config.getMetricReportingConfig()
                .getGraphiteReporterConfig();
        if (graphiteReporterConfig != null && graphiteReporterConfig.getEnableReporter()) {
            initGraphiteReporter(graphiteReporterConfig);
            logger.info("Started GraphiteReporter: {}", graphiteReporterConfig.toString());
        }
    }

    // Init infos
    systemInfo = SystemInfo.create();
    nodeInfo = NodeInfo.create(namingConfig);
    appInfo = AppInfo.create(namingConfig.getApplicationName());

    // Set up config file reloading
    if (config.getConfigFile() != null && config.getConfigFile().exists()) {
        ConfigFileUtil.createEffectiveConfigFile(config);

        executorService = Executors.newSingleThreadScheduledExecutor(
                new ThreadFactoryBuilder().setNameFormat("MonitoringCenter-%d").build());
        executorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    reloadConfig();
                } catch (Throwable e) {
                    logger.error("Uncaught exception occurred while reloading the MonitoringCenter config", e);
                }
            }
        }, CONFIG_RELOAD_INTERVAL_IN_SECONDS, CONFIG_RELOAD_INTERVAL_IN_SECONDS, TimeUnit.SECONDS);
    }

    logger.info("MonitoringCenter has been configured: {}", initialConfig.toString());
    configured.set(true);
}

From source file:org.hyperledger.fabric.sdk.ServiceDiscovery.java

void run() {

    if (channel.isShutdown() || SERVICE_DISCOVER_FREQ_SECONDS < 1) {
        return;// w w w.j  a v  a  2 s  .  c  o  m
    }

    if (seviceDiscovery == null) {

        seviceDiscovery = Executors.newSingleThreadScheduledExecutor(r -> {
            Thread t = Executors.defaultThreadFactory().newThread(r);
            t.setDaemon(true);
            return t;
        }).scheduleAtFixedRate(() -> {

            logger.debug(format("Channel %s starting service rediscovery after %d seconds.", channelName,
                    SERVICE_DISCOVER_FREQ_SECONDS));
            fullNetworkDiscovery(true);

        }, SERVICE_DISCOVER_FREQ_SECONDS, SERVICE_DISCOVER_FREQ_SECONDS, TimeUnit.SECONDS);
    }

}

From source file:org.apache.lens.server.query.QueryExecutionServiceImpl.java

private void startQueryExpirer() {
    ThreadFactory factory = new BasicThreadFactory.Builder().namingPattern("QueryExpirer-%d").daemon(true)
            .priority(Thread.NORM_PRIORITY).build();
    queryExpirer = Executors.newSingleThreadScheduledExecutor(factory);
    long expiryRunInterval = conf.getLong(QUERY_EXPIRY_INTERVAL_MILLIS, DEFAULT_QUERY_EXPIRY_INTERVAL_MILLIS);
    queryExpirer.scheduleWithFixedDelay(new Runnable() {
        @Override/*from  w ww .  ja v a2 s  .  co m*/
        public void run() {
            try {
                expireQueries();
            } catch (Exception e) {
                incrCounter(QUERY_EXPIRY_FAILURE_COUNTER);
                log.error("Unable to expire queries", e);
            }
        }
    }, expiryRunInterval, expiryRunInterval, TimeUnit.MILLISECONDS);
    log.info("Enabled periodic exipry of queries at {} millis interval", expiryRunInterval);
}

From source file:org.hyperledger.fabric.sdk.Channel.java

void runSweeper() {

    if (shutdown || DELTA_SWEEP < 1) {
        return;//from  w w w  .ja  v a2 s  .  co  m
    }

    if (sweeper == null) {

        sweeperExecutorService = Executors.newSingleThreadScheduledExecutor(r -> {
            Thread t = Executors.defaultThreadFactory().newThread(r);
            t.setDaemon(true);
            return t;
        });
        sweeper = sweeperExecutorService.scheduleAtFixedRate(() -> {
            try {

                if (txListeners != null) {

                    synchronized (txListeners) {

                        for (Iterator<Map.Entry<String, LinkedList<TL>>> it = txListeners.entrySet()
                                .iterator(); it.hasNext();) {

                            Map.Entry<String, LinkedList<TL>> es = it.next();

                            LinkedList<TL> tlLinkedList = es.getValue();
                            tlLinkedList.removeIf(TL::sweepMe);
                            if (tlLinkedList.isEmpty()) {
                                it.remove();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                logger.warn("Sweeper got error:" + e.getMessage(), e);
            }

        }, 0, DELTA_SWEEP, TimeUnit.MILLISECONDS);
    }

}