Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implemented configuration via system property to configure thread poo…
…l size + test case
  • Loading branch information
tine2k committed Dec 30, 2015
commit d6cbb711930bbc9890003e29f1696dbc5bfb1229
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
*/
public class HystrixTimer {

static final String SYS_PROP_TIMEOUT = "com.netflix.hystrix.util.HystrixTimer.timeoutThreadPoolSize";

private static final Logger logger = LoggerFactory.getLogger(HystrixTimer.class);

private static HystrixTimer INSTANCE = new HystrixTimer();
Expand Down Expand Up @@ -147,7 +149,17 @@ protected void startThreadIfNeeded() {
* We want this only done once when created in compareAndSet so use an initialize method
*/
public void initialize() {
executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {

// System property can override the default ThreadPool size
final String coreSizePropertyValue = System.getProperty(SYS_PROP_TIMEOUT);
int coreSize;
if (coreSizePropertyValue != null) {
coreSize = Integer.valueOf(coreSizePropertyValue);
} else {
coreSize = Runtime.getRuntime().availableProcessors();
}

executor = new ScheduledThreadPoolExecutor(coreSize, new ThreadFactory() {
final AtomicInteger counter = new AtomicInteger();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@
*/
package com.netflix.hystrix.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.netflix.hystrix.util.HystrixTimer.ScheduledExecutor;
import com.netflix.hystrix.util.HystrixTimer.TimerListener;
import org.junit.Before;
import org.junit.Test;

import java.lang.ref.Reference;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import com.netflix.hystrix.util.HystrixTimer.ScheduledExecutor;
import com.netflix.hystrix.util.HystrixTimer.TimerListener;
import static org.junit.Assert.*;


public class HystrixTimerTest {

@Before
public void setUp() {
HystrixTimer timer = HystrixTimer.getInstance();
HystrixTimer.reset();
System.clearProperty(HystrixTimer.SYS_PROP_TIMEOUT);
}

@Test
public void testSingleCommandSingleInterval() {
HystrixTimer timer = HystrixTimer.getInstance();
Expand Down Expand Up @@ -163,6 +167,25 @@ public void testReset() {
HystrixTimer.reset();
}

@Test
public void testThreadPoolSizeSystemProperty() {

System.setProperty(HystrixTimer.SYS_PROP_TIMEOUT, "42");
HystrixTimer hystrixTimer = HystrixTimer.getInstance();
hystrixTimer.startThreadIfNeeded();
System.clearProperty(HystrixTimer.SYS_PROP_TIMEOUT);

assertEquals(42, hystrixTimer.executor.get().getThreadPool().getCorePoolSize());
}

@Test
public void testThreadPoolSizeDefault() {

HystrixTimer hystrixTimer = HystrixTimer.getInstance();
hystrixTimer.startThreadIfNeeded();
assertEquals(Runtime.getRuntime().availableProcessors(), hystrixTimer.executor.get().getThreadPool().getCorePoolSize());
}

private static class TestListener implements TimerListener {

private final int interval;
Expand Down Expand Up @@ -235,5 +258,5 @@ public int getIntervalTimeInMilliseconds() {

}


}