private static int getTotalWaitingCommands() { int totalWaitingCommands = 0; final String hystrixPoolName = EXAMPLE_GROUP; HystrixThreadPoolKey key = HystrixThreadPoolKey.Factory.asKey(hystrixPoolName); HystrixThreadPoolMetrics metrics = HystrixThreadPoolMetrics.getInstance(key); if (metrics != null) { Number active = metrics.getCurrentActiveCount(); Number queueSize = metrics.getCurrentQueueSize(); totalWaitingCommands = active.intValue() + queueSize.intValue(); LOG.debug("{} has {} commands waiting", hystrixPoolName, totalWaitingCommands); } return totalWaitingCommands; }
/** * Get or create the {@link HystrixCommandMetrics} instance for a given {@link HystrixCommandKey}. * * <p>This is thread-safe and ensures only 1 {@link HystrixCommandMetrics} per {@link * HystrixCommandKey}. * * @param key {@link HystrixCommandKey} of {@link HystrixCommand} instance requesting the {@link * HystrixCommandMetrics} * @param commandGroup Pass-thru to {@link HystrixCommandMetrics} instance on first time when * constructed * @param properties Pass-thru to {@link HystrixCommandMetrics} instance on first time when * constructed * @return {@link HystrixCommandMetrics} */ public static HystrixCommandMetrics getInstance( HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixThreadPoolKey threadPoolKey, HystrixCommandProperties properties) { // attempt to retrieve from cache first HystrixCommandMetrics commandMetrics = metrics.get(key.name()); if (commandMetrics != null) { return commandMetrics; } // it doesn't exist so we need to create it // now check to see if we need to create a synthetic threadPoolKey HystrixThreadPoolKey nonNullThreadPoolKey; if (threadPoolKey == null) { nonNullThreadPoolKey = HystrixThreadPoolKey.Factory.asKey(commandGroup.name()); } else { nonNullThreadPoolKey = threadPoolKey; } commandMetrics = new HystrixCommandMetrics( key, commandGroup, nonNullThreadPoolKey, properties, HystrixPlugins.getInstance().getEventNotifier()); // attempt to store it (race other threads) HystrixCommandMetrics existing = metrics.putIfAbsent(key.name(), commandMetrics); if (existing == null) { // we won the thread-race to store the instance we created return commandMetrics; } else { // we lost so return 'existing' and let the one we created be garbage collected return existing; } }