This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevents the queues to grow indefinitely. See stagemonitor/stagemonitor-mailinglist#16
- Loading branch information
Felix Barnsteiner
committed
Oct 1, 2015
1 parent
d318e97
commit 76308e1
Showing
5 changed files
with
112 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
stagemonitor-core/src/main/java/org/stagemonitor/core/util/ExecutorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.stagemonitor.core.util; | ||
|
||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.stagemonitor.core.CorePlugin; | ||
|
||
public final class ExecutorUtils { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ExecutorUtils.class); | ||
|
||
private ExecutorUtils() { | ||
// don't instantiate | ||
} | ||
|
||
public static ThreadPoolExecutor createSingleThreadDeamonPool(final String threadName, int queueCapacity) { | ||
final ThreadFactory daemonThreadFactory = new ThreadFactory() { | ||
@Override | ||
public Thread newThread(Runnable r) { | ||
Thread thread = new Thread(r); | ||
thread.setDaemon(true); | ||
thread.setName(threadName); | ||
return thread; | ||
} | ||
}; | ||
return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(queueCapacity), daemonThreadFactory) { | ||
@Override | ||
public String toString() { | ||
return super.toString() + "(thread name = " + threadName + ")"; | ||
} | ||
}; | ||
} | ||
|
||
public static void logRejectionWarning(RejectedExecutionException e) { | ||
logger.warn("The limit of pending tasks for the executor is reached. " + | ||
"This could be due to a unreachable service such as elasticsearch or due to a spike in incoming requests. " + | ||
"Consider increasing the default capacity limit with the configuration key '" + CorePlugin.POOLS_QUEUE_CAPACITY_LIMIT_KEY + "'\n" | ||
+ e.getMessage()); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
stagemonitor-core/src/test/java/org/stagemonitor/core/util/ExecutorUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.stagemonitor.core.util; | ||
|
||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
import org.junit.Test; | ||
|
||
public class ExecutorUtilsTest { | ||
|
||
final ThreadPoolExecutor lowCapacityPool = ExecutorUtils.createSingleThreadDeamonPool("test-pool", 1); | ||
private Runnable sleepABit = new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
Thread.sleep(10); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}; | ||
|
||
@Test(expected = RejectedExecutionException.class) | ||
public void testRejectedExecution() throws Exception { | ||
for (int i = 0; i < 10; i++) { | ||
lowCapacityPool.submit(sleepABit); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters