Search in sources :

Example 56 with ArrayBlockingQueue

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);
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceReference(org.osgi.framework.ServiceReference) LoginModuleFactory(org.apache.felix.jaas.LoginModuleFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 57 with ArrayBlockingQueue

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);
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 58 with ArrayBlockingQueue

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);
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Node(org.w3c.dom.Node)

Example 59 with ArrayBlockingQueue

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));
    }
}
Also used : RecursiveParserWrapperFSConsumer(org.apache.tika.batch.fs.RecursiveParserWrapperFSConsumer) BasicContentHandlerFactory(org.apache.tika.sax.BasicContentHandlerFactory) TikaConfig(org.apache.tika.config.TikaConfig) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) IOException(java.io.IOException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) TikaTest(org.apache.tika.TikaTest)

Example 60 with ArrayBlockingQueue

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;
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AvMessage(dvoraka.avservice.common.data.AvMessage)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)440 Test (org.junit.Test)158 ArrayList (java.util.ArrayList)75 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)74 IOException (java.io.IOException)66 CountDownLatch (java.util.concurrent.CountDownLatch)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 BlockingQueue (java.util.concurrent.BlockingQueue)34 ExecutorService (java.util.concurrent.ExecutorService)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 List (java.util.List)29 LocalAddress (io.netty.channel.local.LocalAddress)27 HashMap (java.util.HashMap)25 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)25 Subscription (rx.Subscription)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)23 File (java.io.File)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 LinkedList (java.util.LinkedList)21