Skip to content

Instantly share code, notes, and snippets.

@lfmunoz
Created July 24, 2020 16:02
Show Gist options
  • Save lfmunoz/6cc9cb147a2631ba43602fa31fa5f9b7 to your computer and use it in GitHub Desktop.
Save lfmunoz/6cc9cb147a2631ba43602fa31fa5f9b7 to your computer and use it in GitHub Desktop.
Monitor Thread Executor Service
import io.micrometer.core.instrument.Metrics
import io.micrometer.core.instrument.Tags
import com.google.common.util.concurrent.ThreadFactoryBuilder
import java.util.concurrent.ExecutorService
import java.util.concurrent.SynchronousQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
@Bean(destroyMethod = "shutdown")
fun connectionExecutorService(): ExecutorService {
val maxThreads = (applicationProperties.availableCores * 2).coerceAtLeast(2)
val threadFactory = ThreadFactoryBuilder().setNameFormat("rabbitConn-%s").setDaemon(true).build()
val threadPoolExec = ThreadPoolExecutor(
1,
maxThreads,
60L,
TimeUnit.SECONDS,
SynchronousQueue(),
threadFactory,
ThreadPoolExecutor.CallerRunsPolicy()
)
Metrics.gauge(RABBIT_THREADS_CONNECTION, Tags.of(TAG_KEY_TYPE, "queueSize"), threadPoolExec, { it.queue.size.toDouble() })
Metrics.gauge(RABBIT_THREADS_CONNECTION, Tags.of(TAG_KEY_TYPE, "activeCount"), threadPoolExec, { it.activeCount.toDouble() })
Metrics.gauge(RABBIT_THREADS_CONNECTION, Tags.of(TAG_KEY_TYPE, "poolSize"), threadPoolExec, { it.poolSize.toDouble() })
Metrics.gauge(RABBIT_THREADS_CONNECTION, Tags.of(TAG_KEY_TYPE, "taskCount"), threadPoolExec, { it.taskCount.toDouble() })
return threadPoolExec
}
@lfmunoz
Copy link
Author

lfmunoz commented Aug 18, 2020

ArrayBlockingQueue queue = new ArrayBlockingQueue(2);

LinkedBlockingQueue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment