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);
}
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());
}
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");
}
};
}
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);
}
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);
}
Aggregations