1818import com .netflix .hystrix .HystrixCommand ;
1919import com .netflix .hystrix .HystrixThreadPool ;
2020import com .netflix .hystrix .HystrixThreadPoolKey ;
21+ import com .netflix .hystrix .HystrixThreadPoolProperties ;
2122import com .netflix .hystrix .strategy .HystrixPlugins ;
2223import com .netflix .hystrix .strategy .properties .HystrixProperty ;
2324import com .netflix .hystrix .util .PlatformSpecific ;
@@ -75,9 +76,49 @@ public abstract class HystrixConcurrencyStrategy {
7576 * @return instance of {@link ThreadPoolExecutor}
7677 */
7778 public ThreadPoolExecutor getThreadPool (final HystrixThreadPoolKey threadPoolKey , HystrixProperty <Integer > corePoolSize , HystrixProperty <Integer > maximumPoolSize , HystrixProperty <Integer > keepAliveTime , TimeUnit unit , BlockingQueue <Runnable > workQueue ) {
79+ final ThreadFactory threadFactory = getThreadFactory (threadPoolKey );
80+
81+ final int dynamicCoreSize = corePoolSize .get ();
82+ final int dynamicMaximumSize = maximumPoolSize .get ();
83+
84+ if (dynamicCoreSize > dynamicMaximumSize ) {
85+ logger .error ("Hystrix ThreadPool configuration at startup for : " + threadPoolKey .name () + " is trying to set coreSize = " +
86+ dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " +
87+ dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value" );
88+ return new ThreadPoolExecutor (dynamicCoreSize , dynamicCoreSize , keepAliveTime .get (), unit , workQueue , threadFactory );
89+ } else {
90+ return new ThreadPoolExecutor (dynamicCoreSize , dynamicMaximumSize , keepAliveTime .get (), unit , workQueue , threadFactory );
91+ }
92+ }
93+
94+ public ThreadPoolExecutor getThreadPool (final HystrixThreadPoolKey threadPoolKey , HystrixThreadPoolProperties threadPoolProperties ) {
95+ final ThreadFactory threadFactory = getThreadFactory (threadPoolKey );
96+
97+ final boolean allowMaximumSizeToDivergeFromCoreSize = threadPoolProperties .getAllowMaximumSizeToDivergeFromCoreSize ().get ();
98+ final int dynamicCoreSize = threadPoolProperties .coreSize ().get ();
99+ final int keepAliveTime = threadPoolProperties .keepAliveTimeMinutes ().get ();
100+ final int maxQueueSize = threadPoolProperties .maxQueueSize ().get ();
101+ final BlockingQueue <Runnable > workQueue = getBlockingQueue (maxQueueSize );
102+
103+ if (allowMaximumSizeToDivergeFromCoreSize ) {
104+ final int dynamicMaximumSize = threadPoolProperties .maximumSize ().get ();
105+ if (dynamicCoreSize > dynamicMaximumSize ) {
106+ logger .error ("Hystrix ThreadPool configuration at startup for : " + threadPoolKey .name () + " is trying to set coreSize = " +
107+ dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " +
108+ dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value" );
109+ return new ThreadPoolExecutor (dynamicCoreSize , dynamicCoreSize , keepAliveTime , TimeUnit .MINUTES , workQueue , threadFactory );
110+ } else {
111+ return new ThreadPoolExecutor (dynamicCoreSize , dynamicMaximumSize , keepAliveTime , TimeUnit .MINUTES , workQueue , threadFactory );
112+ }
113+ } else {
114+ return new ThreadPoolExecutor (dynamicCoreSize , dynamicCoreSize , keepAliveTime , TimeUnit .MINUTES , workQueue , threadFactory );
115+ }
116+ }
117+
118+ private static ThreadFactory getThreadFactory (final HystrixThreadPoolKey threadPoolKey ) {
78119 ThreadFactory threadFactory = null ;
79120 if (!PlatformSpecific .isAppEngineStandardEnvironment ()) {
80- threadFactory = new ThreadFactory () {
121+ return new ThreadFactory () {
81122 protected final AtomicInteger threadNumber = new AtomicInteger (0 );
82123
83124 @ Override
@@ -89,19 +130,7 @@ public Thread newThread(Runnable r) {
89130
90131 };
91132 } else {
92- threadFactory = PlatformSpecific .getAppEngineThreadFactory ();
93- }
94-
95- final int dynamicCoreSize = corePoolSize .get ();
96- final int dynamicMaximumSize = maximumPoolSize .get ();
97-
98- if (dynamicCoreSize > dynamicMaximumSize ) {
99- logger .error ("Hystrix ThreadPool configuration at startup for : " + threadPoolKey .name () + " is trying to set coreSize = " +
100- dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ". Maximum size will be set to " +
101- dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value" );
102- return new ThreadPoolExecutor (dynamicCoreSize , dynamicCoreSize , keepAliveTime .get (), unit , workQueue , threadFactory );
103- } else {
104- return new ThreadPoolExecutor (dynamicCoreSize , dynamicMaximumSize , keepAliveTime .get (), unit , workQueue , threadFactory );
133+ return PlatformSpecific .getAppEngineThreadFactory ();
105134 }
106135 }
107136
0 commit comments