use of java.util.concurrent.BlockingQueue in project roof-im by madfroglx.
the class RedisBlockingQueueLoadBalanceMessagePublisher method createQueues.
@Override
protected List<BlockingQueue> createQueues(String serverName) {
List<BlockingQueue> list = new ArrayList<>();
for (RedisTemplate redisTemplate : redisTemplates) {
BoundListOperations boundListOperations = redisTemplate.boundListOps(serverName);
BlockingQueue blockingQueue = new DefaultRedisList(boundListOperations);
list.add(blockingQueue);
}
return list;
}
use of java.util.concurrent.BlockingQueue in project opennms by OpenNMS.
the class LegacyScheduler method run.
/**
* The main method of the scheduler. This method is responsible for checking
* the runnable queues for ready objects and then enqueuing them into the
* thread pool for execution.
*/
@Override
public void run() {
synchronized (this) {
m_status = RUNNING;
}
LOG.debug("run: scheduler running");
/*
* Loop until a fatal exception occurs or until
* the thread is interrupted.
*/
for (; ; ) {
/*
* Block if there is nothing in the queue(s).
* When something is added to the queue it
* signals us to wakeup.
*/
synchronized (this) {
if (m_status != RUNNING && m_status != PAUSED && m_status != PAUSE_PENDING && m_status != RESUME_PENDING) {
LOG.debug("run: status = {}, time to exit", m_status);
break;
}
// if paused or pause pending then block
while (m_status == PAUSE_PENDING || m_status == PAUSED) {
if (m_status == PAUSE_PENDING) {
LOG.debug("run: pausing.");
}
m_status = PAUSED;
try {
wait();
} catch (InterruptedException ex) {
// exit
break;
}
}
if (m_status == RESUME_PENDING) {
LOG.debug("run: resuming.");
m_status = RUNNING;
}
if (m_scheduled == 0) {
try {
LOG.debug("run: no ready runnables scheduled, waiting...");
wait();
} catch (InterruptedException ex) {
break;
}
}
}
/*
* Cycle through the queues checking for
* what's ready to run. The queues are keyed
* by the interval, but the mapped elements
* are peekable fifo queues.
*/
int runned = 0;
synchronized (m_queues) {
/*
* Get an iterator so that we can cycle
* through the queue elements.
*/
for (Entry<Long, BlockingQueue<ReadyRunnable>> entry : m_queues.entrySet()) {
/*
* Peak for Runnable objects until
* there are no more ready runnables.
*
* Also, only go through each queue once!
* if we didn't add a count then it would
* be possible to starve other queues.
*/
BlockingQueue<ReadyRunnable> in = entry.getValue();
ReadyRunnable readyRun = null;
int maxLoops = in.size();
do {
try {
readyRun = in.peek();
if (readyRun != null && readyRun.isReady()) {
LOG.debug("run: found ready runnable {}", readyRun);
/*
* Pop the interface/readyRunnable from the
* queue for execution.
*/
in.take();
// Add runnable to the execution queue
m_runner.execute(readyRun);
++runned;
// Increment the execution counter
++m_numTasksExecuted;
// Thread Pool Statistics
if (m_runner instanceof ThreadPoolExecutor) {
ThreadPoolExecutor e = (ThreadPoolExecutor) m_runner;
String ratio = String.format("%.3f", e.getTaskCount() > 0 ? new Double(e.getCompletedTaskCount()) / new Double(e.getTaskCount()) : 0);
LOG.debug("thread pool statistics: activeCount={}, taskCount={}, completedTaskCount={}, completedRatio={}, poolSize={}", e.getActiveCount(), e.getTaskCount(), e.getCompletedTaskCount(), ratio, e.getPoolSize());
}
}
} catch (InterruptedException e) {
// jump all the way out
return;
} catch (RejectedExecutionException e) {
throw new UndeclaredThrowableException(e);
}
} while (readyRun != null && readyRun.isReady() && --maxLoops > 0);
}
}
/*
* Wait for 1 second if there were no runnables
* executed during this loop, otherwise just
* start over.
*/
synchronized (this) {
m_scheduled -= runned;
if (runned == 0) {
try {
wait(1000);
} catch (InterruptedException ex) {
// exit for loop
break;
}
}
}
}
LOG.debug("run: scheduler exiting, state = STOPPED");
synchronized (this) {
m_status = STOPPED;
}
}
use of java.util.concurrent.BlockingQueue in project jetty.project by eclipse.
the class ServerTimeoutsTest method testNoBlockingTimeoutBlockingWriteIdleTimeoutFires.
@Test
public void testNoBlockingTimeoutBlockingWriteIdleTimeoutFires() throws Exception {
httpConfig.setBlockingTimeout(-1);
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new BlockingWriteHandler(handlerLatch));
long idleTimeout = 2500;
setServerIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
// Do not succeed the callback so the server will block writing.
callbacks.offer(callback);
}).send(result -> {
if (result.isFailed())
resultLatch.countDown();
});
// Blocking write should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// After the server stopped sending, consume on the client to read the early EOF.
while (true) {
Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
if (callback == null)
break;
callback.succeeded();
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
}
use of java.util.concurrent.BlockingQueue in project hbase by apache.
the class TestAsyncTable method testSimpleMultiple.
@Test
public void testSimpleMultiple() throws Exception {
AsyncTableBase table = getTable.get();
int count = 100;
CountDownLatch putLatch = new CountDownLatch(count);
IntStream.range(0, count).forEach(i -> table.put(new Put(concat(row, i)).addColumn(FAMILY, QUALIFIER, concat(VALUE, i))).thenAccept(x -> putLatch.countDown()));
putLatch.await();
BlockingQueue<Boolean> existsResp = new ArrayBlockingQueue<>(count);
IntStream.range(0, count).forEach(i -> table.exists(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> existsResp.add(x)));
for (int i = 0; i < count; i++) {
assertTrue(existsResp.take());
}
BlockingQueue<Pair<Integer, Result>> getResp = new ArrayBlockingQueue<>(count);
IntStream.range(0, count).forEach(i -> table.get(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> getResp.add(Pair.newPair(i, x))));
for (int i = 0; i < count; i++) {
Pair<Integer, Result> pair = getResp.take();
assertArrayEquals(concat(VALUE, pair.getFirst()), pair.getSecond().getValue(FAMILY, QUALIFIER));
}
CountDownLatch deleteLatch = new CountDownLatch(count);
IntStream.range(0, count).forEach(i -> table.delete(new Delete(concat(row, i))).thenAccept(x -> deleteLatch.countDown()));
deleteLatch.await();
IntStream.range(0, count).forEach(i -> table.exists(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> existsResp.add(x)));
for (int i = 0; i < count; i++) {
assertFalse(existsResp.take());
}
IntStream.range(0, count).forEach(i -> table.get(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> getResp.add(Pair.newPair(i, x))));
for (int i = 0; i < count; i++) {
Pair<Integer, Result> pair = getResp.take();
assertTrue(pair.getSecond().isEmpty());
}
}
use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.
the class BlockingQueueTest method testTimedPollFromEmptyBlocksInterruptibly.
/**
* timed poll() blocks interruptibly when empty
*/
public void testTimedPollFromEmptyBlocksInterruptibly() {
final BlockingQueue q = emptyCollection();
final CountDownLatch threadStarted = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
threadStarted.countDown();
try {
q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
await(threadStarted);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
Aggregations