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) 

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:com.toedter.chatty.server.boot.message.web.ChatMessageRepositoryListener.java

private void notifySubscribers(ChatMessage chatMessage) {
    shouldBroadcast.set(true);/*from w w w  .  j a  va2 s.  c o  m*/
    if (!isBroadcasterInitialized.getAndSet(true)) {
        logger.info("Broadcasting initialized");
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        Runnable broadcasterRunnable = new Runnable() {
            public void run() {
                // logger.info("Broadcasting REFRESH: " + shouldBroadcast.get());
                if (shouldBroadcast.getAndSet(false)) {
                    if (broadcaster == null) {
                        ServletContext servletContext = ServletContextFactory.getDefault().getServletContext();
                        BroadcasterFactory factory = (BroadcasterFactory) servletContext
                                .getAttribute("org.atmosphere.cpr.BroadcasterFactory");
                        broadcaster = factory.lookup("/chatty/atmos/messages");
                    }
                    broadcaster.broadcast("{\"command\":\"reloadChatMessages\"}");
                }
            }
        };
        scheduledExecutorService.scheduleAtFixedRate(broadcasterRunnable, 0, 300, TimeUnit.MILLISECONDS);
    }
}

From source file:org.cruxframework.crux.core.declarativeui.hotdeploy.HotDeploymentScanner.java

/**
 * 
 * @param poolSize
 */
private HotDeploymentScanner(int poolSize) {
    threadPool = Executors.newScheduledThreadPool(poolSize);
}

From source file:net.orzo.service.TaskManager.java

/**
 *//* w w w. j  ava 2s. c o m*/
@Inject
public TaskManager(ServiceConfig conf) {
    this.conf = conf;
    this.tasks = new HashMap<>();
    this.scheduler = Executors.newScheduledThreadPool(1); // TODO size
    this.schedules = new HashMap<>();
    this.execLog = new TaskLog();
    this.sharedServices = new SharedServices(this.conf.getGeoipDbPath());
}

From source file:com.meltmedia.dropwizard.etcd.junit.EtcdJsonRule.java

@Override
public Statement apply(Statement base, Description description) {
    return new Statement() {
        @Override/* w  w w.j a  v a  2  s .  c  om*/
        public void evaluate() throws Throwable {
            ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
            ObjectMapper mapper = new ObjectMapper().registerModule(new JodaModule());

            try {
                try {
                    clientSupplier.get().deleteDir(directory).recursive().send().get();
                } catch (Exception e) {
                    System.out.printf("could not delete %s from service rule", directory);
                    e.printStackTrace();
                }

                factory = EtcdJson.builder().withClient(clientSupplier).withBaseDirectory(directory)
                        .withExecutor(executor).withMapper(mapper).withMetricRegistry(new MetricRegistry())
                        .build();

                factory.start();

                try {

                    base.evaluate();
                } finally {
                    try {
                        factory.stop();
                    } catch (Throwable ioe) {
                        ioe.printStackTrace(System.err);
                    }
                    factory = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                executor.shutdown();
            }

        }
    };
}

From source file:com.ejisto.modules.executor.TaskManager.java

public TaskManager() {
    this.registry = new ConcurrentHashMap<>();
    this.executorService = Executors.newCachedThreadPool();
    this.scheduler = Executors.newScheduledThreadPool(5);
    this.scheduler.scheduleAtFixedRate(this::refreshTasksList, 500L, 500L, MILLISECONDS);
}

From source file:com.guang.eunormia.common.cache.DefaultLocalCache.java

@SuppressWarnings("unchecked")
private void init() {
    caches = new ConcurrentHashMap[moduleSize];

    for (int i = 0; i < moduleSize; i++) {
        caches[i] = new ConcurrentHashMap<K, SoftReference<V>>();
    }//from  w  w  w.  ja va2  s .  c  o  m

    expiryCache = new ConcurrentHashMap<K, Long>();

    scheduleService = Executors.newScheduledThreadPool(1);

    scheduleService.scheduleAtFixedRate(new CheckOutOfDateSchedule(caches, expiryCache), 0, expiryInterval * 60,
            TimeUnit.SECONDS);

    if (Logger.isInfoEnabled()) {
        Logger.info("DefaultCache CheckService is start!");
    }
}

From source file:com.acmutv.ontoqa.tool.runtime.RuntimeManager.java

/**
 * Registers a periodic task.// w  w w .  j av  a 2 s . c  om
 * @param task the task to execute.
 * @param delay the delay to first execution.
 * @param period the period between executions.
 * @param timeout the time to interruption.
 * @param unit the time unit.
 */
private static void registerPeriodic(Runnable task, long delay, long period, long timeout, TimeUnit unit) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    final ScheduledFuture<?> handler = scheduler.scheduleAtFixedRate(task, delay, period, unit);

    if (timeout > 0) {
        Runnable interrupt = () -> handler.cancel(true);
        scheduler.schedule(interrupt, timeout, TimeUnit.SECONDS);
    }
}

From source file:com.netflix.curator.framework.imps.TestTempFramework.java

@Test
public void testInactivity() throws Exception {
    final CuratorTempFrameworkImpl client = (CuratorTempFrameworkImpl) CuratorFrameworkFactory.builder()
            .connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1))
            .buildTemp(1, TimeUnit.SECONDS);
    try {/*from w ww. j a  va  2 s. com*/
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        Runnable command = new Runnable() {
            @Override
            public void run() {
                client.updateLastAccess();
            }
        };
        service.scheduleAtFixedRate(command, 10, 10, TimeUnit.MILLISECONDS);
        client.inTransaction().create().forPath("/foo", "data".getBytes()).and().commit();
        service.shutdownNow();
        Thread.sleep(2000);

        Assert.assertNull(client.getCleanup());
        Assert.assertNull(client.getClient());
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:interactivespaces.workbench.devinfrastructure.StandaloneDevelopmentInfrastructure.java

@Override
public void startup() {
    log = new Jdk14Logger("InteractiveSpaces-Development-Infrastructure");
    try {//  ww  w . j  a va 2s.  c  o m
        executorService = Executors.newScheduledThreadPool(NUMBER_THREADS_IN_POOL);

        initializeRosEnvironment();
        initializeRosMasterController();

    } catch (Throwable e) {
        InteractiveSpacesException.throwFormattedException(e,
                "Could not start up the Interactive Spaces Development Infrastructure");
    }
}

From source file:org.apache.ranger.service.RangerTransactionService.java

@PostConstruct
public void init() {
    scheduler = Executors.newScheduledThreadPool(1);
}