use of com.codahale.metrics.InstrumentedThreadFactory in project dropwizard by dropwizard.
the class DefaultHealthFactory method createScheduledExecutorForHealthChecks.
private ScheduledExecutorService createScheduledExecutorForHealthChecks(final int numberOfScheduledHealthChecks, final MetricRegistry metrics, final LifecycleEnvironment lifecycle, final String fullName) {
final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(fullName + "-%d").setDaemon(true).setUncaughtExceptionHandler((t, e) -> LOGGER.error("Thread={} died due to uncaught exception", t, e)).build();
final InstrumentedThreadFactory instrumentedThreadFactory = new InstrumentedThreadFactory(threadFactory, metrics);
final ScheduledExecutorService scheduledExecutorService = lifecycle.scheduledExecutorService(fullName + "-scheduled-executor", instrumentedThreadFactory).threads(numberOfScheduledHealthChecks).build();
return new InstrumentedScheduledExecutorService(scheduledExecutorService, metrics);
}
use of com.codahale.metrics.InstrumentedThreadFactory in project dropwizard by dropwizard.
the class ScheduledExecutorServiceBuilder method build.
public ScheduledExecutorService build() {
final InstrumentedThreadFactory instrumentedThreadFactory = new InstrumentedThreadFactory(threadFactory, environment.getMetricRegistry(), nameFormat);
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(poolSize, instrumentedThreadFactory, handler);
executor.setRemoveOnCancelPolicy(removeOnCancel);
environment.manage(new ExecutorServiceManager(executor, shutdownTime, nameFormat));
return executor;
}
use of com.codahale.metrics.InstrumentedThreadFactory in project janusgraph by JanusGraph.
the class ExecutorServiceInstrumentationTest method shouldCreateInstrumentedThreadFactory.
@Test
public void shouldCreateInstrumentedThreadFactory() throws InterruptedException {
MetricRegistry registry = new MetricRegistry();
InstrumentedThreadFactory threadFactory = ExecutorServiceInstrumentation.instrument("org.janusgraph", "test", new ThreadFactoryBuilder().build(), registry);
ExecutorService executorService = Executors.newSingleThreadExecutor(threadFactory);
try {
Lock lock = new ReentrantLock();
lock.lock();
try {
// Spawn a thread that will wait for the lock to become available
executorService.submit(lock::lock);
// Wait for the thread to start
Thread.sleep(100);
assertEquals(1L, registry.getMeters().get("org.janusgraph.threadpools.test.threadFactory.created").getCount());
assertEquals(1L, registry.getCounters().get("org.janusgraph.threadpools.test.threadFactory.running").getCount());
assertEquals(0L, registry.getMeters().get("org.janusgraph.threadpools.test.threadFactory.terminated").getCount());
} finally {
lock.unlock();
}
} finally {
executorService.shutdown();
}
// Wait for the thread to stop
Thread.sleep(100);
assertEquals(1L, registry.getMeters().get("org.janusgraph.threadpools.test.threadFactory.terminated").getCount());
}
use of com.codahale.metrics.InstrumentedThreadFactory in project dropwizard by dropwizard.
the class ExecutorServiceBuilder method build.
public ExecutorService build() {
if (corePoolSize != maximumPoolSize && maximumPoolSize > 1 && !isBoundedQueue()) {
log.warn("Parameter 'maximumPoolSize' is conflicting with unbounded work queues");
}
final String nameWithoutFormat = getNameWithoutFormat(nameFormat);
final ThreadFactory instrumentedThreadFactory = new InstrumentedThreadFactory(threadFactory, environment.getMetricRegistry(), nameWithoutFormat);
final ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime.getQuantity(), keepAliveTime.getUnit(), workQueue, instrumentedThreadFactory, handler);
executor.allowCoreThreadTimeOut(allowCoreThreadTimeOut);
environment.manage(new ExecutorServiceManager(executor, shutdownTime, nameFormat));
return executor;
}
use of com.codahale.metrics.InstrumentedThreadFactory in project graylog2-server by Graylog2.
the class JobWorkerPool method buildExecutor.
private static ExecutorService buildExecutor(String name, int poolSize, MetricRegistry metricRegistry) {
final ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(NAME_PREFIX + "[" + name + "]-%d").setUncaughtExceptionHandler((t, e) -> LOG.error("Unhandled exception", e)).build();
final InstrumentedThreadFactory itf = new InstrumentedThreadFactory(threadFactory, metricRegistry, name(JobWorkerPool.class, name));
final SynchronousQueue<Runnable> workQueue = new SynchronousQueue<>();
final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, poolSize, 60L, TimeUnit.SECONDS, workQueue, itf);
return new InstrumentedExecutorService(executor, metricRegistry, name(EXECUTOR_NAME, name));
}
Aggregations