Example usage for java.util.concurrent Executors newScheduledThreadPool

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

Introduction

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

Prototype

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) 

Source Link

Document

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.

Usage

From source file:org.apache.solr.cloud.autoscaling.ScheduledTriggers.java

public ScheduledTriggers(SolrResourceLoader loader, SolrCloudManager dataProvider) {
    // todo make the core pool size configurable
    // it is important to use more than one because a time taking trigger can starve other scheduled triggers
    // ideally we should have as many core threads as the number of triggers but firstly, we don't know beforehand
    // how many triggers we have and secondly, that many threads will always be instantiated and kept around idle
    // so it is wasteful as well. Hopefully 4 is a good compromise.
    scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(4,
            new DefaultSolrThreadFactory("ScheduledTrigger"));
    scheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true);
    scheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    actionExecutor = ExecutorUtil/*  ww  w  . ja  v  a  2  s .  co m*/
            .newMDCAwareSingleThreadExecutor(new DefaultSolrThreadFactory("AutoscalingActionExecutor"));
    // todo make the wait time configurable
    actionThrottle = new ActionThrottle("action", DEFAULT_MIN_MS_BETWEEN_ACTIONS);
    this.dataProvider = dataProvider;
    this.stateManager = dataProvider.getDistribStateManager();
    this.loader = loader;
    queueStats = new Stats();
    listeners = new TriggerListeners();
    // initialize cooldown timer
    // todo: make the cooldownPeriod configurable
    cooldownStart.set(System.nanoTime() - cooldownPeriod.get());
}

From source file:com.github.mrstampy.gameboot.concurrent.GameBootConcurrentConfiguration.java

/**
 * Task scheduler./*from   www.  j a  v  a2  s  .c o m*/
 *
 * @return the task scheduler
 */
@Bean(name = GAME_BOOT_TASK_SCHEDULER)
public TaskScheduler taskScheduler() {
    String name = isEmpty(taskSchedulerName) ? "GameBoot Task Scheduler" : taskSchedulerName;

    GameBootThreadFactory factory = new GameBootThreadFactory(name);

    ScheduledExecutorService exe = Executors.newScheduledThreadPool(taskSchedulerPoolSize, factory);

    return new ConcurrentTaskScheduler(exe);
}

From source file:org.apache.flume.sink.RollingFileSink.java

@Override
public void start() {
    logger.info("Starting {}...", this);
    sinkCounter.start();/* w w  w  . j av a  2  s  . c om*/
    super.start();

    pathController.setBaseDirectory(directory);
    if (rollInterval > 0) {

        rollService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
                .setNameFormat("rollingFileSink-roller-" + Thread.currentThread().getId() + "-%d").build());

        /*
         * Every N seconds, mark that it's time to rotate. We purposefully
         * do NOT touch anything other than the indicator flag to avoid
         * error handling issues (e.g. IO exceptions occuring in two
         * different threads. Resist the urge to actually perform rotation
         * in a separate thread!
         */
        rollService.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                logger.debug("Marking time to rotate file {}",
                        pathController.getCurrentFile(filePrefix, fileSuffix));
                shouldRotate = true;
            }

        }, rollInterval, rollInterval, TimeUnit.SECONDS);
    } else {
        logger.info("RollInterval is not valid, file rolling will not happen.");
    }
    logger.info("RollingFileSink {} started.", getName());
}

From source file:org.rhq.metrics.simulator.Simulator.java

/**
 * Run a multi-threaded simulation where multiple threads
 * collect metrics and run aggregation./*w w  w  . j av  a2  s. c  o  m*/
 * The scheduling is done based on intervalType please review the
 * simulation plan for the timing.
 */
private void runThreadedSimulation(SimulationPlan plan) {
    this.initializeMetricsServer(plan);
    final ConsoleReporter consoleReporter = createConsoleReporter(metrics, plan.getMetricsReportInterval());

    final ScheduledExecutorService aggregators = Executors.newScheduledThreadPool(1,
            new SimulatorThreadFactory());
    final ScheduledExecutorService collectors = Executors
            .newScheduledThreadPool(plan.getNumMeasurementCollectors(), new SimulatorThreadFactory());
    final ExecutorService aggregationQueue = Executors.newSingleThreadExecutor(new SimulatorThreadFactory());
    final ScheduledExecutorService readers = Executors.newScheduledThreadPool(plan.getReaderThreadPoolSize(),
            new SimulatorThreadFactory());

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            shutdown(collectors, "collectors", 5);
            shutdown(readers, "readers", 5);
            shutdown(aggregators, "aggregators", 1);
            shutdown(aggregationQueue, "aggregationQueue", Integer.MAX_VALUE);
            metricsServer.shutdown();
            log.info("Wait for console reporter...");
            try {
                Thread.sleep(181000);
            } catch (InterruptedException e) {
            }
            consoleReporter.stop();
        }
    });

    MeasurementAggregator measurementAggregator = new MeasurementAggregator(metricsServer, this, metrics,
            aggregationQueue, plan.getNumMeasurementCollectors() * plan.getBatchSize());

    for (int i = 0; i < plan.getNumMeasurementCollectors(); ++i) {
        collectors
                .scheduleAtFixedRate(
                        new MeasurementCollector(plan.getBatchSize(), plan.getBatchSize() * i, metrics,
                                metricsServer, plan.getDateTimeService()),
                        0, plan.getCollectionInterval(), TimeUnit.MILLISECONDS);
    }

    if (plan.isAggregationEnabled()) {
        aggregators.scheduleAtFixedRate(measurementAggregator, 0, plan.getAggregationInterval(),
                TimeUnit.MILLISECONDS);
    }

    for (int i = 0; i < plan.getNumReaders(); ++i) {
        MeasurementReader reader = new MeasurementReader(plan.getSimulationRate(), metrics, metricsServer,
                plan.getBatchSize() * i, plan.getBatchSize());
        readers.scheduleAtFixedRate(reader, 30, 30, TimeUnit.SECONDS);
    }

    try {
        Thread.sleep(Minutes.minutes(plan.getSimulationTime()).toStandardDuration().getMillis());
    } catch (InterruptedException e) {
    }
    log.info("Simulation has completed. Initiating shutdown...");
    shutdown(0);
}

From source file:com.twitter.distributedlog.service.DistributedLogServer.java

public void runServer() throws ConfigurationException, IllegalArgumentException, IOException {
    if (!uri.isPresent()) {
        throw new IllegalArgumentException("No distributedlog uri provided.");
    }/*from   w w  w. j  a  v  a2  s.com*/
    URI dlUri = URI.create(uri.get());
    DistributedLogConfiguration dlConf = new DistributedLogConfiguration();
    if (conf.isPresent()) {
        String configFile = conf.get();
        try {
            dlConf.loadConf(new File(configFile).toURI().toURL());
        } catch (ConfigurationException e) {
            throw new IllegalArgumentException(
                    "Failed to load distributedlog configuration from " + configFile + ".");
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(
                    "Failed to load distributedlog configuration from malformed " + configFile + ".");
        }
    }

    this.configExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
            .setNameFormat("DistributedLogService-Dyncfg-%d").setDaemon(true).build());

    // server configuration and dynamic configuration
    ServerConfiguration serverConf = new ServerConfiguration();
    serverConf.loadConf(dlConf);

    // overwrite the shard id if it is provided in the args
    if (shardId.isPresent()) {
        serverConf.setServerShardId(shardId.get());
    }

    serverConf.validate();

    DynamicDistributedLogConfiguration dynDlConf = getServiceDynConf(dlConf);

    logger.info("Starting stats provider : {}", statsProvider.getClass());
    statsProvider.start(dlConf);

    if (announceServerSet.isPresent() && announceServerSet.get()) {
        announcer = new ServerSetAnnouncer(dlUri, port.or(0), statsPort.or(0), shardId.or(0));
    } else {
        announcer = new NOPAnnouncer();
    }

    // Build the stream partition converter
    StreamPartitionConverter converter;
    try {
        converter = ReflectionUtils.newInstance(serverConf.getStreamPartitionConverterClass());
    } catch (ConfigurationException e) {
        logger.warn("Failed to load configured stream-to-partition converter. Fallback to use {}",
                IdentityStreamPartitionConverter.class.getName());
        converter = new IdentityStreamPartitionConverter();
    }

    StreamConfigProvider streamConfProvider = getStreamConfigProvider(dlConf, converter);

    // pre-run
    preRun(dlConf, serverConf);

    Pair<DistributedLogServiceImpl, Server> serverPair = runServer(serverConf, dlConf, dynDlConf, dlUri,
            converter, statsProvider, port.or(0), keepAliveLatch, statsReceiver, thriftmux.isPresent(),
            streamConfProvider);

    this.dlService = serverPair.getLeft();
    this.server = serverPair.getRight();

    // announce the service
    announcer.announce();
}

From source file:org.hyperic.hq.measurement.agent.server.TopNScheduler.java

private void createScheduler() {
    scheduler = Executors.newScheduledThreadPool(1, new ThreadFactory() {
        private final AtomicLong i = new AtomicLong(0);

        public Thread newThread(Runnable r) {
            return new Thread(r, "TopNScheduler" + i.getAndIncrement());
        }/*from  w w  w  . ja va 2 s.  co m*/
    });
}

From source file:org.apache.ambari.server.agent.HeartbeatProcessor.java

@Inject
public HeartbeatProcessor(Clusters clusterFsm, ActionManager am, HeartbeatMonitor heartbeatMonitor,
        Injector injector) {//from  w  ww.  java2 s .c  o  m
    injector.injectMembers(this);

    this.injector = injector;
    this.heartbeatMonitor = heartbeatMonitor;
    this.clusterFsm = clusterFsm;
    actionManager = am;
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("ambari-heartbeat-processor-%d")
            .build();
    executor = Executors.newScheduledThreadPool(poolSize, threadFactory);
}

From source file:org.apache.distributedlog.service.DistributedLogServer.java

public void runServer()
        throws ConfigurationException, IllegalArgumentException, IOException, ClassNotFoundException {
    if (!uri.isPresent()) {
        throw new IllegalArgumentException("No distributedlog uri provided.");
    }/*from  ww  w  .  j a v a 2s . c o m*/
    URI dlUri = URI.create(uri.get());
    DistributedLogConfiguration dlConf = new DistributedLogConfiguration();
    if (conf.isPresent()) {
        String configFile = conf.get();
        try {
            dlConf.loadConf(new File(configFile).toURI().toURL());
        } catch (ConfigurationException e) {
            throw new IllegalArgumentException(
                    "Failed to load distributedlog configuration from " + configFile + ".");
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(
                    "Failed to load distributedlog configuration from malformed " + configFile + ".");
        }
    }

    this.configExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
            .setNameFormat("DistributedLogService-Dyncfg-%d").setDaemon(true).build());

    // server configuration and dynamic configuration
    ServerConfiguration serverConf = new ServerConfiguration();
    serverConf.loadConf(dlConf);

    // overwrite the shard id if it is provided in the args
    if (shardId.isPresent()) {
        serverConf.setServerShardId(shardId.get());
    }

    serverConf.validate();

    DynamicDistributedLogConfiguration dynDlConf = getServiceDynConf(dlConf);

    logger.info("Starting stats provider : {}", statsProvider.getClass());
    statsProvider.start(dlConf);

    if (announceServerSet.isPresent() && announceServerSet.get()) {
        announcer = new ServerSetAnnouncer(dlUri, port.or(0), statsPort.or(0), shardId.or(0));
    } else {
        announcer = new NOPAnnouncer();
    }

    // Build the stream partition converter
    StreamPartitionConverter converter;
    try {
        converter = ReflectionUtils.newInstance(serverConf.getStreamPartitionConverterClass());
    } catch (ConfigurationException e) {
        logger.warn("Failed to load configured stream-to-partition converter. Fallback to use {}",
                IdentityStreamPartitionConverter.class.getName());
        converter = new IdentityStreamPartitionConverter();
    }
    Class loadAppraiserClass = Class.forName(loadAppraiserClassStr.or(DEFAULT_LOAD_APPRIASER));
    LoadAppraiser loadAppraiser = (LoadAppraiser) ReflectionUtils.newInstance(loadAppraiserClass);
    logger.info("Supplied load appraiser class is " + loadAppraiserClassStr.get() + " Instantiated "
            + loadAppraiser.getClass().getCanonicalName());

    StreamConfigProvider streamConfProvider = getStreamConfigProvider(dlConf, converter);

    // pre-run
    preRun(dlConf, serverConf);

    Pair<DistributedLogServiceImpl, Server> serverPair = runServer(serverConf, dlConf, dynDlConf, dlUri,
            converter, routingService, statsProvider, port.or(0), keepAliveLatch, statsReceiver,
            thriftmux.isPresent(), streamConfProvider, loadAppraiser);

    this.dlService = serverPair.getLeft();
    this.server = serverPair.getRight();

    // announce the service
    announcer.announce();
    // start the routing service after announced
    routingService.startService();
    logger.info("Started the routing service.");
    dlService.startPlacementPolicy();
    logger.info("Started the placement policy.");
}

From source file:org.wso2.andes.kernel.subscription.AndesSubscriptionManager.java

/**
 * Create a AndesSubscription manager instance. This is a static class managing
 * subscriptions.//  w  ww .j a  v  a  2  s . c  o  m
 *
 * @param subscriptionRegistry Registry storing subscriptions
 * @param andesContextStore    Persistent store storing message router, queue, binding
 *                             and subscription information
 */
public AndesSubscriptionManager(SubscriptionRegistry subscriptionRegistry, AndesContextStore andesContextStore)
        throws AndesException {
    this.subscriptionRegistry = subscriptionRegistry;
    this.isNetworkPartitioned = false;
    this.subscriptionFactory = new AndesSubscriptionFactory();
    this.storageQueueRegistry = AndesContext.getInstance().getStorageQueueRegistry();
    this.andesContextStore = andesContextStore;
    this.localNodeId = ClusterResourceHolder.getInstance().getClusterManager().getMyNodeID();
    storeUnavailable = false;
    executorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
            .setNameFormat("AndesSubscriptionManager-SubscriptionDisconnectTask").build());

    CoordinationComponentFactory coordinationComponentFactory = new CoordinationComponentFactory();
    this.clusterNotificationAgent = coordinationComponentFactory.createClusterNotificationAgent();

    if (AndesContext.getInstance().isClusteringEnabled()) {
        // network partition detection works only when clustered.
        AndesContext.getInstance().getClusterAgent().addNetworkPartitionListener(10, this);
    }

    //Add subscribers gauge to metrics manager
    MetricManager.gauge(MetricsConstants.QUEUE_SUBSCRIBERS, Level.INFO, new QueueSubscriberGauge());
    //Add topic gauge to metrics manager
    MetricManager.gauge(MetricsConstants.TOPIC_SUBSCRIBERS, Level.INFO, new TopicSubscriberGauge());

    FailureObservingStoreManager.registerStoreHealthListener(this);
}