Search in sources :

Example 41 with ScheduledThreadPoolExecutor

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

the class DataNode method startMetricsLogger.

/**
   * Start a timer to periodically write DataNode metrics to the log file. This
   * behavior can be disabled by configuration.
   *
   */
protected void startMetricsLogger() {
    long metricsLoggerPeriodSec = getConf().getInt(DFS_DATANODE_METRICS_LOGGER_PERIOD_SECONDS_KEY, DFS_DATANODE_METRICS_LOGGER_PERIOD_SECONDS_DEFAULT);
    if (metricsLoggerPeriodSec <= 0) {
        return;
    }
    MetricsLoggerTask.makeMetricsLoggerAsync(METRICS_LOG);
    // Schedule the periodic logging.
    metricsLoggerTimer = new ScheduledThreadPoolExecutor(1);
    metricsLoggerTimer.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    metricsLoggerTimer.scheduleWithFixedDelay(new MetricsLoggerTask(METRICS_LOG, "DataNode", (short) 0), metricsLoggerPeriodSec, metricsLoggerPeriodSec, TimeUnit.SECONDS);
}
Also used : MetricsLoggerTask(org.apache.hadoop.hdfs.server.common.MetricsLoggerTask) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 42 with ScheduledThreadPoolExecutor

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

the class SizedScheduledExecutorServiceTest method testSizedScheduledExecutorService.

public void testSizedScheduledExecutorService() throws Exception {
    ScheduledThreadPoolExecutor delegate = new ScheduledThreadPoolExecutor(5);
    SizedScheduledExecutorService sized = new SizedScheduledExecutorService(delegate, 2);
    Runnable task = new Runnable() {

        @Override
        public void run() {
        // noop
        }
    };
    sized.schedule(task, 2, TimeUnit.SECONDS);
    sized.schedule(task, 3, TimeUnit.SECONDS);
    try {
        sized.schedule(task, 4, TimeUnit.SECONDS);
        fail("Should have thrown exception");
    } catch (RejectedExecutionException e) {
        assertEquals("Task rejected due queue size limit reached", e.getMessage());
    }
    sized.shutdownNow();
    assertTrue("Should be shutdown", sized.isShutdown() || sized.isTerminating());
    assertTrue("Should be shutdown", delegate.isShutdown() || sized.isTerminating());
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 43 with ScheduledThreadPoolExecutor

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

the class ManagedThrottlerTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    final ScheduledExecutorService badService = new ScheduledThreadPoolExecutor(1) {

        @Override
        public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
            throw new RejectedExecutionException();
        }
    };
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("log:foo").throttle(10).id("mythrottler").to("mock:result");
            from("seda:throttleCount").throttle(1).timePeriodMillis(250).id("mythrottler2").to("mock:end");
            from("seda:throttleCountAsync").throttle(1).asyncDelayed().timePeriodMillis(250).id("mythrottler3").to("mock:endAsync");
            from("seda:throttleCountAsyncException").throttle(1).asyncDelayed().timePeriodMillis(250).id("mythrottler4").to("mock:endAsyncException").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    throw new RuntimeException("Fail me");
                }
            });
            from("seda:throttleCountRejectExecutionCallerRuns").onException(RejectedExecutionException.class).to("mock:rejectedExceptionEndpoint1").end().throttle(1).timePeriodMillis(250).asyncDelayed().executorService(badService).callerRunsWhenRejected(true).id("mythrottler5").to("mock:endAsyncRejectCallerRuns");
            from("seda:throttleCountRejectExecution").onException(RejectedExecutionException.class).to("mock:rejectedExceptionEndpoint1").end().throttle(1).timePeriodMillis(250).asyncDelayed().executorService(badService).callerRunsWhenRejected(false).id("mythrottler6").to("mock:endAsyncReject");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TimeUnit(java.util.concurrent.TimeUnit) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 44 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQPullConsumerScheduleService method start.

public void start() throws MQClientException {
    final String group = this.defaultMQPullConsumer.getConsumerGroup();
    this.scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(this.pullThreadNums, new ThreadFactoryImpl("PullMsgThread-" + group));
    this.defaultMQPullConsumer.setMessageQueueListener(this.messageQueueListener);
    this.defaultMQPullConsumer.start();
    log.info("MQPullConsumerScheduleService start OK, {} {}", this.defaultMQPullConsumer.getConsumerGroup(), this.callbackTable);
}
Also used : ThreadFactoryImpl(org.apache.rocketmq.common.ThreadFactoryImpl) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 45 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project springside4 by springside.

the class ThreadPoolUtilTest method wrapException.

@Test
public void wrapException() {
    ScheduledThreadPoolExecutor executor = ThreadPoolBuilder.scheduledPool().build();
    ExceptionTask task = new ExceptionTask();
    executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS);
    ThreadUtil.sleep(500);
    // 线程第一次跑就被中断
    assertThat(task.counter.get()).isEqualTo(1);
    ThreadPoolUtil.gracefulShutdown(executor, 1000);
    // //////
    executor = ThreadPoolBuilder.scheduledPool().build();
    ExceptionTask newTask = new ExceptionTask();
    Runnable wrapTask = ThreadPoolUtil.safeRunnable(newTask);
    executor.scheduleAtFixedRate(wrapTask, 0, 100, TimeUnit.MILLISECONDS);
    ThreadUtil.sleep(500);
    assertThat(newTask.counter.get()).isGreaterThan(2);
    System.out.println("-------actual run:" + task.counter.get());
    ThreadPoolUtil.gracefulShutdown(executor, 1000);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Test(org.junit.Test)

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