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:org.apache.pulsar.compaction.CompactorTool.java

public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander(arguments);
    jcommander.setProgramName("PulsarTopicCompactor");

    // parse args by JCommander
    jcommander.parse(args);//from   www.  java  2s. c o  m
    if (arguments.help) {
        jcommander.usage();
        System.exit(-1);
    }

    // init broker config
    ServiceConfiguration brokerConfig;
    if (isBlank(arguments.brokerConfigFile)) {
        jcommander.usage();
        throw new IllegalArgumentException("Need to specify a configuration file for broker");
    } else {
        brokerConfig = PulsarConfigurationLoader.create(arguments.brokerConfigFile, ServiceConfiguration.class);
    }

    ClientBuilder clientBuilder = PulsarClient.builder();

    if (isNotBlank(brokerConfig.getBrokerClientAuthenticationPlugin())) {
        clientBuilder.authentication(brokerConfig.getBrokerClientAuthenticationPlugin(),
                brokerConfig.getBrokerClientAuthenticationParameters());
    }

    if (brokerConfig.getBrokerServicePortTls().isPresent()) {
        clientBuilder.serviceUrl(PulsarService.brokerUrlTls(brokerConfig))
                .allowTlsInsecureConnection(brokerConfig.isTlsAllowInsecureConnection())
                .tlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());

    } else {
        clientBuilder.serviceUrl(PulsarService.brokerUrl(brokerConfig));
    }

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());

    OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
    ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);

    ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(),
            ZooKeeperClientFactory.SessionType.ReadWrite, (int) brokerConfig.getZooKeeperSessionTimeoutMillis())
            .get();
    BookKeeperClientFactory bkClientFactory = new BookKeeperClientFactoryImpl();
    BookKeeper bk = bkClientFactory.create(brokerConfig, zk);
    try (PulsarClient pulsar = clientBuilder.build()) {
        Compactor compactor = new TwoPhaseCompactor(brokerConfig, pulsar, bk, scheduler);
        long ledgerId = compactor.compact(arguments.topic).get();
        log.info("Compaction of topic {} complete. Compacted to ledger {}", arguments.topic, ledgerId);
    } finally {
        bk.close();
        bkClientFactory.close();
        zk.close();
        scheduler.shutdownNow();
        executor.shutdown();
    }
}

From source file:Main.java

/**
 * Wrapper over newSingleThreadScheduledExecutor.
 *//* w  ww  .  j  a v  a2  s  . c  o  m*/
public static ScheduledExecutorService newDaemonSingleThreadScheduledExecutor(String threadName) {
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadName).build();
    return Executors.newSingleThreadScheduledExecutor(threadFactory);
}

From source file:Main.java

public static ScheduledExecutorService newSingleThreadScheduledExecutor(String processName) {
    return Executors.newSingleThreadScheduledExecutor(newThreadFactory(processName));
}

From source file:Main.java

/**
 * Instantiates a new single threaded scheduled executor whose thread has the specified name.
 *
 * @param threadName The name of the thread.
 * @return The executor.//from   w w  w  .  java 2s .  c  o  m
 */
public static ScheduledExecutorService newSingleThreadScheduledExecutor(final String threadName) {
    return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, threadName);
        }
    });
}

From source file:Main.java

public static ScheduledExecutorService newSingleThreadScheduledExecutor(String processName, boolean isDaemon) {
    return Executors.newSingleThreadScheduledExecutor(newThreadFactory(processName, isDaemon));
}

From source file:org.jactr.core.concurrent.ExecutorServices.java

static protected void initialize() {
    addExecutor(INLINE, INLINE_EXECUTOR);
    addExecutor(BACKGROUND, Executors.newSingleThreadExecutor(new GeneralThreadFactory("jACTR-Background")));
    addExecutor(PERIODIC,//from www.ja  v a 2  s  . c o m
            Executors.newSingleThreadScheduledExecutor(new GeneralThreadFactory("jACT-R Periodic")));
}

From source file:org.xdi.oxd.server.license.LicenseUpdateService.java

private void scheduleUpdatePinger() {
    final ScheduledExecutorService executorService = Executors
            .newSingleThreadScheduledExecutor(CoreUtils.daemonThreadFactory());
    executorService.scheduleAtFixedRate(new Runnable() {
        @Override//w w  w  .  j  a  va 2  s. c om
        public void run() {
            updateLicenseFromServer();
        }
    }, 1, conf.getLicenseCheckPeriodInHours(), TimeUnit.HOURS);
}

From source file:org.apache.synapse.aspects.flow.statistics.log.StatisticEventProcessor.java

public static void initializeCleaningThread() {
    //Thread to consume queue and update data structures for publishing
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("Mediation Statistic Stale Entry Cleaning Task");
            return t;
        }// w ww . j  ava 2s  . c om
    });
    Long eventCleanTime = Long.parseLong(
            SynapsePropertiesLoader.getPropertyValue(StatisticsConstants.FLOW_STATISTICS_EVENT_CLEAN_TIME,
                    StatisticsConstants.FLOW_STATISTICS_DEFAULT_EVENT_CLEAN_INTERVAL));
    StatisticCleaningThread statisticCleaningThread = new StatisticCleaningThread(runtimeStatistics);
    executor.scheduleAtFixedRate(statisticCleaningThread, 0, eventCleanTime, TimeUnit.MILLISECONDS);
}

From source file:com.xyxy.platform.examples.showcase.demos.schedule.JdkTimerJob.java

@PostConstruct
public void start() throws Exception {
    Validate.isTrue(period > 0);//from w w  w. java2  s  .c o  m

    // ?schedule, Spring TaskUtilsLOG_AND_SUPPRESS_ERROR_HANDLER?
    Runnable task = TaskUtils.decorateTaskWithErrorHandler(this, null, true);

    // ?SechdulerExecutor,guavaThreadFactoryBuilder???
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("JdkTimerJob-%1$d").build());

    // scheduleAtFixedRatefixRate() ?.
    // scheduleAtFixedDelay() ???.
    scheduledExecutorService.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
}

From source file:com.github.lburgazzoli.quickfixj.core.FIXContext.java

/**
 * c-tor/*from   w ww  .j a va 2s  . c  om*/
 *
 * @param id;
 */
public FIXContext(String id) {
    m_id = id;
    m_threadFactory = new NamedThreadFactory("QFJ_Timer");
    m_scheduler = Executors.newSingleThreadScheduledExecutor(m_threadFactory);
    m_sessions = Maps.newConcurrentMap();
}