Search in sources :

Example 26 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project camel by apache.

the class KafkaIdempotentRepository method doStart.

@Override
protected void doStart() throws Exception {
    ObjectHelper.notNull(camelContext, "camelContext");
    StringHelper.notEmpty(topic, "topic");
    this.cache = Collections.synchronizedMap(new LRUCache<>(maxCacheSize));
    if (consumerConfig == null) {
        consumerConfig = new Properties();
        StringHelper.notEmpty(bootstrapServers, "bootstrapServers");
        consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    }
    if (producerConfig == null) {
        producerConfig = new Properties();
        StringHelper.notEmpty(bootstrapServers, "bootstrapServers");
        producerConfig.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    }
    ObjectHelper.notNull(consumerConfig, "consumerConfig");
    ObjectHelper.notNull(producerConfig, "producerConfig");
    // each consumer instance must have control over its own offset, so assign a groupID at random
    String groupId = UUID.randomUUID().toString();
    log.debug("Creating consumer with {}[{}]", ConsumerConfig.GROUP_ID_CONFIG, groupId);
    consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    consumerConfig.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, Boolean.TRUE.toString());
    consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    consumer = new KafkaConsumer<>(consumerConfig);
    producerConfig.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    producerConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    // set up the producer to remove all batching on send, we want all sends to be fully synchronous
    producerConfig.putIfAbsent(ProducerConfig.ACKS_CONFIG, "1");
    producerConfig.putIfAbsent(ProducerConfig.BATCH_SIZE_CONFIG, "0");
    producer = new KafkaProducer<>(producerConfig);
    cacheReadyLatch = new CountDownLatch(1);
    topicPoller = new TopicPoller(consumer, cacheReadyLatch, pollDurationMs);
    // warm up the cache
    executorService = camelContext.getExecutorServiceManager().newSingleThreadExecutor(this, "KafkaIdempotentRepository");
    executorService.submit(topicPoller);
    log.info("Warming up cache from topic {}", topic);
    try {
        if (cacheReadyLatch.await(30, TimeUnit.SECONDS)) {
            log.info("Cache OK");
        } else {
            log.warn("Timeout waiting for cache warm-up from topic {}. Proceeding anyway. " + "Duplicate records may not be detected.", topic);
        }
    } catch (InterruptedException e) {
        log.warn("Interrupted while warming up cache. This exception is ignored.", e.getMessage());
    }
}
Also used : LRUCache(org.apache.camel.util.LRUCache) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) StringSerializer(org.apache.kafka.common.serialization.StringSerializer)

Example 27 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project camel by apache.

the class KestrelConsumer method doStart.

@Override
protected void doStart() throws Exception {
    log.info("Starting consumer for " + endpoint.getEndpointUri());
    int poolSize = endpoint.getConfiguration().getConcurrentConsumers();
    shutdownPending = false;
    if (poolSize > 1) {
        // We'll set the shutdown latch to poolSize + 1, since we'll also
        // wait for the poller thread when shutting down.
        shutdownLatch = new CountDownLatch(poolSize + 1);
        // Fire up the handler thread pool
        handlerExecutor = endpoint.getCamelContext().getExecutorServiceManager().newFixedThreadPool(this, "Handlers-" + endpoint.getEndpointUri(), poolSize);
        for (int k = 0; k < poolSize; ++k) {
            handlerExecutor.execute(new Handler());
        }
    } else {
        // Since we only have concurrentConsumers=1, we'll do the handling
        // inside the poller thread, so there will only be one thread to
        // wait for on this latch.
        shutdownLatch = new CountDownLatch(1);
    }
    // Fire up the single poller thread
    pollerExecutor = endpoint.getCamelContext().getExecutorServiceManager().newSingleThreadExecutor(this, "Poller-" + endpoint.getEndpointUri());
    pollerExecutor.submit(new Poller(poolSize > 1));
    super.doStart();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch)

Example 28 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project camel by apache.

the class MailProducerConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    Mailbox.clearAll();
    NotifyBuilder builder = new NotifyBuilder(context).whenDone(files).create();
    getMockEndpoint("mock:result").expectedMessageCount(files);
    getMockEndpoint("mock:result").expectsNoDuplicates(body());
    final CountDownLatch latch = new CountDownLatch(files);
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    for (int i = 0; i < files; i++) {
        final int index = i;
        executor.submit(new Callable<Object>() {

            public Object call() throws Exception {
                template.sendBodyAndHeader("direct:start", "Message " + index, "To", "someone@localhost");
                latch.countDown();
                return null;
            }
        });
    }
    // wait first for all the exchanges above to be thoroughly sent asynchronously
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertMockEndpointsSatisfied();
    assertTrue(builder.matchesMockWaitTime());
    Mailbox box = Mailbox.get("someone@localhost");
    assertEquals(files, box.size());
    // as we use concurrent producers the mails can arrive out of order
    Set<Object> bodies = new HashSet<Object>();
    for (int i = 0; i < files; i++) {
        bodies.add(box.get(i).getContent());
    }
    assertEquals("There should be " + files + " unique mails", files, bodies.size());
    executor.shutdownNow();
}
Also used : NotifyBuilder(org.apache.camel.builder.NotifyBuilder) Mailbox(org.jvnet.mock_javamail.Mailbox) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet)

Example 29 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project camel by apache.

the class Mina2Producer method doProcess.

@SuppressWarnings("deprecation")
protected void doProcess(Exchange exchange) throws Exception {
    if (session == null && !lazySessionCreation) {
        throw new IllegalStateException("Not started yet!");
    }
    if (session == null || !session.isConnected()) {
        openConnection();
    }
    // set the exchange encoding property
    if (getEndpoint().getConfiguration().getCharsetName() != null) {
        exchange.setProperty(Exchange.CHARSET_NAME, IOConverter.normalizeCharset(getEndpoint().getConfiguration().getCharsetName()));
    }
    Object body = Mina2PayloadHelper.getIn(getEndpoint(), exchange);
    if (body == null) {
        noReplyLogger.log("No payload to send for exchange: " + exchange);
        // exit early since nothing to write
        return;
    }
    // if textline enabled then covert to a String which must be used for textline
    if (getEndpoint().getConfiguration().isTextline()) {
        body = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, exchange, body);
    }
    // if sync is true then we should also wait for a response (synchronous mode)
    if (sync) {
        // only initialize responseLatch if we should get a response
        responseLatch = new CountDownLatch(1);
        // reset handler if we expect a response
        handler.reset();
    }
    // log what we are writing
    if (LOG.isDebugEnabled()) {
        Object out = body;
        if (body instanceof byte[]) {
            // byte arrays is not readable so convert to string
            out = exchange.getContext().getTypeConverter().convertTo(String.class, body);
        }
        LOG.debug("Writing body: {}", out);
    }
    // write the body
    Mina2Helper.writeBody(session, body, exchange);
    if (sync) {
        // wait for response, consider timeout
        LOG.debug("Waiting for response using timeout {} millis.", timeout);
        boolean done = responseLatch.await(timeout, TimeUnit.MILLISECONDS);
        if (!done) {
            throw new ExchangeTimedOutException(exchange, timeout);
        }
        // did we get a response
        if (handler.getCause() != null) {
            throw new CamelExchangeException("Error occurred in ResponseHandler", exchange, handler.getCause());
        } else if (!handler.isMessageReceived()) {
            // no message received
            throw new ExchangeTimedOutException(exchange, timeout);
        } else {
            // set the result on either IN or OUT on the original exchange depending on its pattern
            if (ExchangeHelper.isOutCapable(exchange)) {
                Mina2PayloadHelper.setOut(exchange, handler.getMessage());
            } else {
                Mina2PayloadHelper.setIn(exchange, handler.getMessage());
            }
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

Example 30 with CountDownLatch

use of java.util.concurrent.CountDownLatch in project camel by apache.

the class Mina2Producer method closeSessionIfNeededAndAwaitCloseInHandler.

private void closeSessionIfNeededAndAwaitCloseInHandler(IoSession sessionToBeClosed) throws InterruptedException {
    closeLatch = new CountDownLatch(1);
    if (!sessionToBeClosed.isClosing()) {
        CloseFuture closeFuture = sessionToBeClosed.closeNow();
        closeFuture.await(timeout, TimeUnit.MILLISECONDS);
        closeLatch.await(timeout, TimeUnit.MILLISECONDS);
    }
}
Also used : CloseFuture(org.apache.mina.core.future.CloseFuture) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)5355 Test (org.junit.Test)2594 IOException (java.io.IOException)631 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)550 AtomicReference (java.util.concurrent.atomic.AtomicReference)501 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)475 ArrayList (java.util.ArrayList)471 QuickTest (com.hazelcast.test.annotation.QuickTest)375 ParallelTest (com.hazelcast.test.annotation.ParallelTest)355 ExecutorService (java.util.concurrent.ExecutorService)322 Test (org.testng.annotations.Test)310 HazelcastInstance (com.hazelcast.core.HazelcastInstance)251 List (java.util.List)212 HashMap (java.util.HashMap)207 HttpServletResponse (javax.servlet.http.HttpServletResponse)207 ExecutionException (java.util.concurrent.ExecutionException)203 HttpServletRequest (javax.servlet.http.HttpServletRequest)189 Ignite (org.apache.ignite.Ignite)188 ServletException (javax.servlet.ServletException)183 TimeoutException (java.util.concurrent.TimeoutException)168