use of java.util.concurrent.ArrayBlockingQueue in project felix by apache.
the class ITConcurrentLoginModuleFactoryTest method concurrentLoginFactoryRegs.
@Test
public void concurrentLoginFactoryRegs() throws Exception {
Logger log = new Logger(context.bundleContext());
BundleContext mock = spy(context.bundleContext());
doReturn(mock(LoginModuleFactory.class)).when(mock).getService(any(ServiceReference.class));
ConfigSpiOsgi spi = new ConfigSpiOsgi(mock, log);
int numOfServices = 20;
Queue<ServiceReference> references = new ArrayBlockingQueue<ServiceReference>(numOfServices);
for (int i = 0; i < numOfServices; i++) {
references.add(newReference());
}
CountDownLatch latch = new CountDownLatch(1);
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < 3; i++) {
Thread t = new Thread(new ServiceAdder(latch, references, spi));
threads.add(t);
t.start();
}
ConfigModifier cm = new ConfigModifier(latch, references, spi);
Thread cmt = new Thread(cm);
threads.add(cmt);
cmt.start();
latch.countDown();
for (Thread t : threads) {
t.join();
}
Map<String, ConfigSpiOsgi.Realm> configs = spi.getAllConfiguration();
assertFalse(configs.isEmpty());
for (ConfigSpiOsgi.Realm r : configs.values()) {
assertEquals(numOfServices, r.engineGetAppConfigurationEntry().length);
}
assertEquals(1, context.getServices(ConfigurationSpi.class, null).length);
}
use of java.util.concurrent.ArrayBlockingQueue in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubSnippets method testPublisherSubscriberHelper.
private void testPublisherSubscriberHelper(TopicName topicName, SubscriptionName subscriptionName) throws Exception {
String messageToPublish = "my-message";
Publisher publisher = null;
try {
publisher = Publisher.defaultBuilder(topicName).build();
PublisherSnippets snippets = new PublisherSnippets(publisher);
final SettableApiFuture<Void> done = SettableApiFuture.create();
ApiFutures.addCallback(snippets.publish(messageToPublish), new ApiFutureCallback<String>() {
public void onSuccess(String messageId) {
done.set(null);
}
public void onFailure(Throwable t) {
done.setException(t);
}
});
done.get();
} finally {
if (publisher != null) {
publisher.shutdown();
}
}
final BlockingQueue<PubsubMessage> queue = new ArrayBlockingQueue<>(1);
final SettableApiFuture<Void> done = SettableApiFuture.create();
final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
SubscriberSnippets snippets = new SubscriberSnippets(subscriptionName, new MessageReceiverSnippets(queue).messageReceiver(), done, MoreExecutors.directExecutor());
new Thread(new Runnable() {
@Override
public void run() {
try {
received.set(queue.poll(10, TimeUnit.MINUTES));
} catch (InterruptedException e) {
received.set(null);
}
// signal the subscriber to clean up
done.set(null);
}
}).start();
// blocks until done is set
snippets.startAndWait();
PubsubMessage message = received.get();
assertNotNull(message);
assertEquals(message.getData().toStringUtf8(), messageToPublish);
}
use of java.util.concurrent.ArrayBlockingQueue in project tika by apache.
the class BatchProcessBuilder method buildQueue.
//tries to get maxQueueSize from main element
private ArrayBlockingQueue<FileResource> buildQueue(Node docElement, Map<String, String> runtimeAttributes) {
int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
String szString = runtimeAttributes.get(MAX_QUEUE_SIZE_KEY);
if (szString == null) {
Node szNode = docElement.getAttributes().getNamedItem(MAX_QUEUE_SIZE_KEY);
if (szNode != null) {
szString = szNode.getNodeValue();
}
}
if (szString != null) {
try {
maxQueueSize = Integer.parseInt(szString);
} catch (NumberFormatException e) {
//swallow
}
}
if (maxQueueSize < 0) {
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
}
return new ArrayBlockingQueue<FileResource>(maxQueueSize);
}
use of java.util.concurrent.ArrayBlockingQueue in project tika by apache.
the class RecursiveParserWrapperFSConsumerTest method testEmbeddedWithNPE.
@Test
public void testEmbeddedWithNPE() throws Exception {
final String path = "/test-documents/embedded_with_npe.xml";
final Metadata metadata = new Metadata();
metadata.add(Metadata.RESOURCE_NAME_KEY, "embedded_with_npe.xml");
ArrayBlockingQueue<FileResource> queue = new ArrayBlockingQueue<FileResource>(2);
queue.add(new FileResource() {
@Override
public String getResourceId() {
return "testFile";
}
@Override
public Metadata getMetadata() {
return metadata;
}
@Override
public InputStream openInputStream() throws IOException {
return this.getClass().getResourceAsStream(path);
}
});
queue.add(new PoisonFileResource());
MockOSFactory mockOSFactory = new MockOSFactory();
RecursiveParserWrapperFSConsumer consumer = new RecursiveParserWrapperFSConsumer(queue, new AutoDetectParserFactory(), new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.TEXT, -1), mockOSFactory, new TikaConfig());
IFileProcessorFutureResult result = consumer.call();
mockOSFactory.getStreams().get(0).flush();
byte[] bytes = mockOSFactory.getStreams().get(0).toByteArray();
List<Metadata> results = JsonMetadataList.fromJson(new InputStreamReader(new ByteArrayInputStream(bytes), UTF_8));
assertEquals(4, results.size());
assertContains("another null pointer", results.get(2).get(RecursiveParserWrapper.EMBEDDED_EXCEPTION));
assertEquals("Nikolai Lobachevsky", results.get(0).get("author"));
for (int i = 1; i < 4; i++) {
assertEquals("embeddedAuthor" + i, results.get(i).get("author"));
assertContains("some_embedded_content" + i, results.get(i).get(RecursiveParserWrapper.TIKA_CONTENT));
}
}
use of java.util.concurrent.ArrayBlockingQueue in project av-service by dvoraka.
the class BufferedPerformanceTester method start.
@Override
public void start() {
running = true;
final long loops = testProperties.getMsgCount();
System.out.println("Load test start for " + loops + " messages...");
final int bufferSize = 6;
BlockingQueue<AvMessage> buffer = new ArrayBlockingQueue<>(bufferSize);
long start = System.currentTimeMillis();
for (int loop = 0; loop <= loops; loop++) {
AvMessage message = Utils.genInfectedMessage();
try {
buffer.offer(message, getTimeout(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
log.warn("Interrupted.", e);
Thread.currentThread().interrupt();
}
avServiceClient.checkMessage(message);
long start2 = System.currentTimeMillis();
while (buffer.size() >= bufferSize && (System.currentTimeMillis() - start2) < getTimeout()) {
getMessage(buffer);
}
}
long start3 = System.currentTimeMillis();
while (buffer.size() > 0 && (System.currentTimeMillis() - start3) < getTimeout()) {
getMessage(buffer);
}
long duration = System.currentTimeMillis() - start;
System.out.println("Load test end.");
float durationSeconds = duration / MS_PER_SECOND;
setResult(loops / durationSeconds);
System.out.println("\nDuration: " + durationSeconds + " s");
System.out.println("Messages: " + result + "/s");
running = false;
}
Aggregations