Search in sources :

Example 1 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project jetty.project by eclipse.

the class WriteFlusherTest method testConcurrent.

@Test
public void testConcurrent() throws Exception {
    Random random = new Random();
    ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(100);
    try {
        String reason = "THE_CAUSE";
        ConcurrentWriteFlusher[] flushers = new ConcurrentWriteFlusher[50000];
        FutureCallback[] futures = new FutureCallback[flushers.length];
        for (int i = 0; i < flushers.length; ++i) {
            int size = 5 + random.nextInt(15);
            ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], size);
            ConcurrentWriteFlusher flusher = new ConcurrentWriteFlusher(endPoint, scheduler, random);
            flushers[i] = flusher;
            FutureCallback callback = new FutureCallback();
            futures[i] = callback;
            scheduler.schedule(() -> flusher.onFail(new Throwable(reason)), random.nextInt(75) + 1, TimeUnit.MILLISECONDS);
            flusher.write(callback, BufferUtil.toBuffer("How Now Brown Cow."), BufferUtil.toBuffer(" The quick brown fox jumped over the lazy dog!"));
        }
        int completed = 0;
        int failed = 0;
        for (int i = 0; i < flushers.length; ++i) {
            try {
                futures[i].get(15, TimeUnit.SECONDS);
                Assert.assertEquals("How Now Brown Cow. The quick brown fox jumped over the lazy dog!", flushers[i].getContent());
                completed++;
            } catch (ExecutionException x) {
                Assert.assertEquals(reason, x.getCause().getMessage());
                failed++;
            }
        }
        Assert.assertThat(completed, Matchers.greaterThan(0));
        Assert.assertThat(failed, Matchers.greaterThan(0));
        Assert.assertEquals(flushers.length, completed + failed);
    } finally {
        scheduler.shutdown();
    }
}
Also used : Random(java.util.Random) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test)

Example 2 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project jetty.project by eclipse.

the class ScheduledExecutorScheduler method doStart.

@Override
protected void doStart() throws Exception {
    scheduler = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = ScheduledExecutorScheduler.this.thread = new Thread(threadGroup, r, name);
            thread.setDaemon(daemon);
            thread.setContextClassLoader(classloader);
            return thread;
        }
    });
    scheduler.setRemoveOnCancelPolicy(true);
    super.doStart();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 3 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project hadoop by apache.

the class EntityGroupFSTimelineStore method serviceStart.

@Override
protected void serviceStart() throws Exception {
    super.serviceStart();
    LOG.info("Starting {}", getName());
    summaryStore.start();
    Configuration conf = getConfig();
    aclManager = new TimelineACLsManager(conf);
    aclManager.setTimelineStore(summaryStore);
    summaryTdm = new TimelineDataManager(summaryStore, aclManager);
    summaryTdm.init(conf);
    addService(summaryTdm);
    // start child services that aren't already started
    super.serviceStart();
    if (!fs.exists(activeRootPath)) {
        fs.mkdirs(activeRootPath);
        fs.setPermission(activeRootPath, ACTIVE_DIR_PERMISSION);
    }
    if (!fs.exists(doneRootPath)) {
        fs.mkdirs(doneRootPath);
        fs.setPermission(doneRootPath, DONE_DIR_PERMISSION);
    }
    objMapper = new ObjectMapper();
    objMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));
    jsonFactory = new MappingJsonFactory(objMapper);
    final long scanIntervalSecs = conf.getLong(YarnConfiguration.TIMELINE_SERVICE_ENTITYGROUP_FS_STORE_SCAN_INTERVAL_SECONDS, YarnConfiguration.TIMELINE_SERVICE_ENTITYGROUP_FS_STORE_SCAN_INTERVAL_SECONDS_DEFAULT);
    final long cleanerIntervalSecs = conf.getLong(YarnConfiguration.TIMELINE_SERVICE_ENTITYGROUP_FS_STORE_CLEANER_INTERVAL_SECONDS, YarnConfiguration.TIMELINE_SERVICE_ENTITYGROUP_FS_STORE_CLEANER_INTERVAL_SECONDS_DEFAULT);
    final int numThreads = conf.getInt(YarnConfiguration.TIMELINE_SERVICE_ENTITYGROUP_FS_STORE_THREADS, YarnConfiguration.TIMELINE_SERVICE_ENTITYGROUP_FS_STORE_THREADS_DEFAULT);
    LOG.info("Scanning active directory {} every {} seconds", activeRootPath, scanIntervalSecs);
    LOG.info("Cleaning logs every {} seconds", cleanerIntervalSecs);
    executor = new ScheduledThreadPoolExecutor(numThreads, new ThreadFactoryBuilder().setNameFormat("EntityLogPluginWorker #%d").build());
    executor.scheduleAtFixedRate(new EntityLogScanner(), 0, scanIntervalSecs, TimeUnit.SECONDS);
    executor.scheduleAtFixedRate(new EntityLogCleaner(), cleanerIntervalSecs, cleanerIntervalSecs, TimeUnit.SECONDS);
}
Also used : MappingJsonFactory(com.fasterxml.jackson.databind.MappingJsonFactory) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) TimelineACLsManager(org.apache.hadoop.yarn.server.timeline.security.TimelineACLsManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector)

Example 4 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project hive by apache.

the class DbTxnManager method initHeartbeatExecutorService.

private synchronized void initHeartbeatExecutorService() {
    if (heartbeatExecutorService != null && !heartbeatExecutorService.isShutdown() && !heartbeatExecutorService.isTerminated()) {
        return;
    }
    heartbeatExecutorService = Executors.newScheduledThreadPool(conf.getIntVar(HiveConf.ConfVars.HIVE_TXN_HEARTBEAT_THREADPOOL_SIZE), new ThreadFactory() {

        private final AtomicInteger threadCounter = new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            return new HeartbeaterThread(r, "Heartbeater-" + threadCounter.getAndIncrement());
        }
    });
    ((ScheduledThreadPoolExecutor) heartbeatExecutorService).setRemoveOnCancelPolicy(true);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 5 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project storm by apache.

the class Localizer method startCleaner.

public void startCleaner() {
    _cacheCleanupService = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder().setNameFormat("Localizer Cache Cleanup").build());
    _cacheCleanupService.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            handleCacheCleanup();
        }
    }, _cacheCleanupPeriod, _cacheCleanupPeriod, TimeUnit.MILLISECONDS);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)373 ExecutorService (java.util.concurrent.ExecutorService)61 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)53 Test (org.junit.Test)53 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)42 CountDownLatch (java.util.concurrent.CountDownLatch)41 ThreadFactory (java.util.concurrent.ThreadFactory)40 ArrayList (java.util.ArrayList)34 ScheduledFuture (java.util.concurrent.ScheduledFuture)33 Callable (java.util.concurrent.Callable)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 Before (org.junit.Before)23 Test (org.testng.annotations.Test)19 List (java.util.List)17 Future (java.util.concurrent.Future)17 IOException (java.io.IOException)16 HashMap (java.util.HashMap)13 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)12 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)12 File (java.io.File)11