use of java.util.concurrent.ArrayBlockingQueue in project sling by apache.
the class ManagerSubscriberTest method setUp.
@Before
public void setUp() throws Exception {
topicQueues = new HashMap<org.apache.sling.mom.Types.TopicName, Queue<QueueEntry>>();
messageQueues = new HashMap<org.apache.sling.mom.Types.QueueName, Queue<QueueEntry>>();
//noinspection unchecked
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
org.apache.sling.mom.Types.TopicName topic = (org.apache.sling.mom.Types.TopicName) invocationOnMock.getArguments()[0];
org.apache.sling.mom.Types.CommandName command = (org.apache.sling.mom.Types.CommandName) invocationOnMock.getArguments()[1];
@SuppressWarnings("unchecked") Map<String, Object> properties = (Map<String, Object>) invocationOnMock.getArguments()[2];
Queue<QueueEntry> queue = topicQueues.get(topic);
if (queue == null) {
queue = new ArrayBlockingQueue<QueueEntry>(100);
topicQueues.put(topic, queue);
}
queue.add(new QueueEntry(command, properties));
return null;
}
}).when(topicManager).publish(Mockito.any(org.apache.sling.mom.Types.TopicName.class), Mockito.any(org.apache.sling.mom.Types.CommandName.class), Mockito.any(Map.class));
//noinspection unchecked
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
org.apache.sling.mom.Types.QueueName topic = (org.apache.sling.mom.Types.QueueName) invocationOnMock.getArguments()[0];
@SuppressWarnings("unchecked") Map<String, Object> properties = (Map<String, Object>) invocationOnMock.getArguments()[1];
Queue<QueueEntry> queue = messageQueues.get(topic);
if (queue == null) {
queue = new ArrayBlockingQueue<QueueEntry>(100);
messageQueues.put(topic, queue);
}
queue.add(new QueueEntry(properties));
return null;
}
}).when(queueManager).add(Mockito.any(org.apache.sling.mom.Types.QueueName.class), Mockito.any(Map.class));
messageSender = new OutboundJobUpdateListener(topicManager, queueManager);
jobStorage = new InMemoryJobStorage();
jobManager = new JobManagerImpl(jobStorage, messageSender);
managerSubscriber = new ManagerSubscriber();
Field f = managerSubscriber.getClass().getDeclaredField("jobManager");
f.setAccessible(true);
f.set(managerSubscriber, jobManager);
}
use of java.util.concurrent.ArrayBlockingQueue in project android_frameworks_base by crdroidandroid.
the class LoaderTestCase method getLoaderResultSynchronously.
/**
* Runs a Loader synchronously and returns the result of the load. The loader will
* be started, stopped, and destroyed by this method so it cannot be reused.
*
* @param loader The loader to run synchronously
* @return The result from the loader
*/
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
// The test thread blocks on this queue until the loader puts it's result in
final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
// This callback runs on the "main" thread and unblocks the test thread
// when it puts the result into the blocking queue
final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {
@Override
public void onLoadComplete(Loader<T> completedLoader, T data) {
// Shut the loader down
completedLoader.unregisterListener(this);
completedLoader.stopLoading();
completedLoader.reset();
// Store the result, unblocking the test thread
queue.add(data);
}
};
// This handler runs on the "main" thread of the process since AsyncTask
// is documented as needing to run on the main thread and many Loaders use
// AsyncTask
final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
loader.registerListener(0, listener);
loader.startLoading();
}
};
// Ask the main thread to start the loading process
mainThreadHandler.sendEmptyMessage(0);
// Block on the queue waiting for the result of the load to be inserted
T result;
while (true) {
try {
result = queue.take();
break;
} catch (InterruptedException e) {
throw new RuntimeException("waiting thread interrupted", e);
}
}
return result;
}
use of java.util.concurrent.ArrayBlockingQueue in project rabbitmq-java-client by rabbitmq.
the class ConnectionFactoryTest method tryNextAddressIfTimeoutExceptionNoAutoRecovery.
// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException {
final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class);
final AMQConnection connectionThatSucceeds = mock(AMQConnection.class);
final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10);
connections.add(connectionThatThrowsTimeout);
connections.add(connectionThatSucceeds);
ConnectionFactory connectionFactory = new ConnectionFactory() {
@Override
protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
return connections.poll();
}
@Override
protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
return mock(FrameHandlerFactory.class);
}
};
connectionFactory.setAutomaticRecoveryEnabled(false);
doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
doNothing().when(connectionThatSucceeds).start();
Connection returnedConnection = connectionFactory.newConnection(new Address[] { new Address("host1"), new Address("host2") });
assertSame(connectionThatSucceeds, returnedConnection);
}
use of java.util.concurrent.ArrayBlockingQueue in project linuxtools by eclipse.
the class ImageTagSelectionPage method searchTags.
private void searchTags() {
try {
final BlockingQueue<List<DockerImageTagSearchResult>> searchResultQueue = new ArrayBlockingQueue<>(1);
ImageTagSelectionPage.this.getContainer().run(true, true, monitor -> {
monitor.beginTask(WizardMessages.getString(// $NON-NLS-1$
"ImageTagSelectionPage.searchTask"), 2);
final String selectedImageName = ImageTagSelectionPage.this.model.getSelectedImage().getName();
try {
final List<IRepositoryTag> repositoryTags = registry.getTags(selectedImageName);
// we have to convert to list of RepositoryTag which
// can be sorted
final List<RepositoryTag> tags = repositoryTags.stream().map(c -> (RepositoryTag) c).collect(Collectors.toList());
Collections.sort(tags);
monitor.worked(1);
final IDockerConnection connection = model.getSelectedConnection();
final List<DockerImageTagSearchResult> searchResults = repositoryTags.stream().map(t -> new DockerImageTagSearchResult(selectedImageName, t, connection.hasImage(selectedImageName, t.getName()))).collect(Collectors.toList());
monitor.worked(1);
searchResultQueue.offer(searchResults);
} catch (DockerException e) {
} finally {
monitor.done();
}
});
List<DockerImageTagSearchResult> res = searchResultQueue.poll(10, TimeUnit.SECONDS);
final List<DockerImageTagSearchResult> searchResult = (res == null) ? new ArrayList<>() : res;
Display.getCurrent().asyncExec(() -> {
ImageTagSelectionPage.this.model.setImageTagSearchResult(searchResult);
// refresh the wizard buttons
getWizard().getContainer().updateButtons();
});
// display a warning in the title area if the search result is empty
if (searchResult.isEmpty()) {
this.setMessage(WizardMessages.getString(// $NON-NLS-1$
"ImageTagSelectionPage.noTagWarning"), WARNING);
} else if (searchResult.size() == 1) {
this.setMessage(WizardMessages.getString(// $NON-NLS-1$
"ImageTagSelectionPage.oneTagMatched"), INFORMATION);
} else {
this.setMessage(WizardMessages.getFormattedString(// $NON-NLS-1$
"ImageTagSelectionPage.tagsMatched", Integer.toString(searchResult.size())), INFORMATION);
}
} catch (InvocationTargetException | InterruptedException e) {
Activator.log(e);
}
}
use of java.util.concurrent.ArrayBlockingQueue in project baseio by generallycloud.
the class ExecutorPoolEventLoop method startup.
@Override
public void startup(String threadName) throws Exception {
threadFactory = new NamedThreadFactory(threadName);
poolExecutor = new ThreadPoolExecutor(eventLoopSize, maxEventLoopSize, keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(maxEventQueueSize), threadFactory);
running = true;
}
Aggregations