Search in sources :

Example 31 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project wildfly by wildfly.

the class RemoveOnCancelScheduledExecutorServiceBuilder method build.

@Override
public ServiceBuilder<ScheduledExecutorService> build(ServiceTarget target) {
    Function<ScheduledExecutorService, ScheduledExecutorService> mapper = executor -> JBossExecutors.protectedScheduledExecutorService(executor);
    Supplier<ScheduledExecutorService> supplier = () -> {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(this.size, this.factory);
        executor.setRemoveOnCancelPolicy(true);
        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        return executor;
    };
    Service<ScheduledExecutorService> service = new SuppliedValueService<>(mapper, supplier, ScheduledExecutorService::shutdown);
    return new AsynchronousServiceBuilder<>(this.name, service).startSynchronously().build(target).setInitialMode(ServiceController.Mode.ON_DEMAND);
}
Also used : Service(org.jboss.msc.service.Service) ServiceBuilder(org.jboss.msc.service.ServiceBuilder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) AsynchronousServiceBuilder(org.wildfly.clustering.service.AsynchronousServiceBuilder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SuppliedValueService(org.wildfly.clustering.service.SuppliedValueService) ServiceController(org.jboss.msc.service.ServiceController) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ServiceName(org.jboss.msc.service.ServiceName) ServiceTarget(org.jboss.msc.service.ServiceTarget) JBossExecutors(org.jboss.threads.JBossExecutors) ThreadFactory(java.util.concurrent.ThreadFactory) Builder(org.wildfly.clustering.service.Builder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SuppliedValueService(org.wildfly.clustering.service.SuppliedValueService)

Example 32 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project wildfly by wildfly.

the class ScheduledThreadPoolBuilder method configure.

@Override
public Builder<ThreadPoolConfiguration> configure(OperationContext context, ModelNode model) throws OperationFailedException {
    int maxThreads = this.definition.getMaxThreads().resolveModelAttribute(context, model).asInt();
    long keepAliveTime = this.definition.getKeepAliveTime().resolveModelAttribute(context, model).asLong();
    ThreadPoolExecutorFactory<?> factory = new ThreadPoolExecutorFactory<ScheduledExecutorService>() {

        @Override
        public ScheduledExecutorService createExecutor(ThreadFactory factory) {
            // Use fixed size, based on maxThreads
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(maxThreads, new ClassLoaderThreadFactory(factory, ClassLoaderThreadFactory.class.getClassLoader()));
            executor.setKeepAliveTime(keepAliveTime, TimeUnit.MILLISECONDS);
            executor.setRemoveOnCancelPolicy(true);
            executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
            return executor;
        }

        @Override
        public void validate() {
        // Do nothing
        }
    };
    this.builder.threadPoolFactory(factory);
    return this;
}
Also used : ClassLoaderThreadFactory(org.wildfly.clustering.service.concurrent.ClassLoaderThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ThreadPoolExecutorFactory(org.infinispan.commons.executors.ThreadPoolExecutorFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ClassLoaderThreadFactory(org.wildfly.clustering.service.concurrent.ClassLoaderThreadFactory)

Example 33 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project distributedlog by twitter.

the class TestDynamicConfigurationFactory method getConfigFactory.

private DynamicConfigurationFactory getConfigFactory(File configFile) {
    String streamConfigPath = configFile.getParent();
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);
    ConcurrentBaseConfiguration defaultConf = new ConcurrentConstConfiguration(new DistributedLogConfiguration());
    return new DynamicConfigurationFactory(executorService, 100, TimeUnit.MILLISECONDS);
}
Also used : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 34 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project distributedlog by twitter.

the class TestStreamManager method testCreateStream.

@Test
public void testCreateStream() throws Exception {
    Stream mockStream = mock(Stream.class);
    final String streamName = "stream1";
    when(mockStream.getStreamName()).thenReturn(streamName);
    StreamFactory mockStreamFactory = mock(StreamFactory.class);
    StreamPartitionConverter mockPartitionConverter = mock(StreamPartitionConverter.class);
    StreamConfigProvider mockStreamConfigProvider = mock(StreamConfigProvider.class);
    when(mockStreamFactory.create((String) any(), (DynamicDistributedLogConfiguration) any(), (StreamManager) any())).thenReturn(mockStream);
    DistributedLogNamespace dlNamespace = mock(DistributedLogNamespace.class);
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);
    StreamManager streamManager = new StreamManagerImpl("", new DistributedLogConfiguration(), executorService, mockStreamFactory, mockPartitionConverter, mockStreamConfigProvider, dlNamespace);
    assertTrue(Await.ready(streamManager.createStreamAsync(streamName)).isReturn());
    verify(dlNamespace).createLog(streamName);
}
Also used : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) DynamicDistributedLogConfiguration(com.twitter.distributedlog.config.DynamicDistributedLogConfiguration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DistributedLogNamespace(com.twitter.distributedlog.namespace.DistributedLogNamespace) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) StreamConfigProvider(com.twitter.distributedlog.service.config.StreamConfigProvider) StreamPartitionConverter(com.twitter.distributedlog.service.streamset.StreamPartitionConverter) Test(org.junit.Test)

Example 35 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project lucene-solr by apache.

the class DocExpirationUpdateProcessorFactory method initDeleteExpiredDocsScheduler.

private void initDeleteExpiredDocsScheduler(SolrCore core) {
    executor = new ScheduledThreadPoolExecutor(1, new DefaultSolrThreadFactory("autoExpireDocs"), new RejectedExecutionHandler() {

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            log.warn("Skipping execution of '{}' using '{}'", r, e);
        }
    });
    core.addCloseHook(new CloseHook() {

        public void postClose(SolrCore core) {
            if (executor.isTerminating()) {
                log.info("Waiting for close of DocExpiration Executor");
                ExecutorUtil.shutdownAndAwaitTermination(executor);
            }
        }

        public void preClose(SolrCore core) {
            log.info("Triggering Graceful close of DocExpiration Executor");
            executor.shutdown();
        }
    });
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
    // we don't want this firing right away, since the core may not be ready
    final long initialDelay = deletePeriodSeconds;
    // TODO: should we make initialDelay configurable
    // TODO: should we make initialDelay some fraction of the period?
    executor.scheduleAtFixedRate(new DeleteExpiredDocsRunnable(this), deletePeriodSeconds, deletePeriodSeconds, TimeUnit.SECONDS);
}
Also used : CloseHook(org.apache.solr.core.CloseHook) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SolrCore(org.apache.solr.core.SolrCore) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Aggregations

ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)298 Test (org.junit.Test)52 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)51 ThreadFactory (java.util.concurrent.ThreadFactory)36 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)35 ExecutorService (java.util.concurrent.ExecutorService)34 Before (org.junit.Before)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Test (org.testng.annotations.Test)19 IOException (java.io.IOException)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ArrayList (java.util.ArrayList)15 List (java.util.List)15 HashMap (java.util.HashMap)12 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)11 File (java.io.File)11 ScheduledFuture (java.util.concurrent.ScheduledFuture)10 ServerInstance (com.linkedin.pinot.common.response.ServerInstance)8 NettyClientMetrics (com.linkedin.pinot.transport.metrics.NettyClientMetrics)8 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)8