Search in sources :

Example 21 with CountDownLatch

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

the class JmsDefaultTaskExecutorTypeTest method doSendMessages.

private void doSendMessages(final String queueName, int messages, int poolSize, final DefaultTaskExecutorType defaultTaskExecutorType) throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    final CountDownLatch latch = new CountDownLatch(messages);
    for (int i = 0; i < messages; i++) {
        final int index = i;
        executor.submit(new Callable<Object>() {

            public Object call() throws Exception {
                String options = defaultTaskExecutorType == null ? "" : "?defaultTaskExecutorType=" + defaultTaskExecutorType.toString();
                template.requestBody("activemq:queue:" + queueName + options, "Message " + index);
                latch.countDown();
                return null;
            }
        });
    }
    latch.await();
    executor.shutdown();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with CountDownLatch

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

the class AbstractJpaMethodTest method consumeEntity.

@Test
public void consumeEntity() throws Exception {
    setUp("jpa://" + Customer.class.getName() + "?usePersist=" + (usePersist() ? "true" : "false"));
    final Customer customer = createDefaultCustomer();
    save(customer);
    final CountDownLatch latch = new CountDownLatch(1);
    consumer = endpoint.createConsumer(new Processor() {

        public void process(Exchange e) {
            receivedExchange = e;
            assertNotNull(e.getIn().getHeader(JpaConstants.ENTITYMANAGER, EntityManager.class));
            latch.countDown();
        }
    });
    consumer.start();
    assertTrue(latch.await(50, TimeUnit.SECONDS));
    consumer.stop();
    Thread.sleep(1000);
    assertNotNull(receivedExchange);
    Customer receivedCustomer = receivedExchange.getIn().getBody(Customer.class);
    assertEquals(customer.getName(), receivedCustomer.getName());
    assertEquals(customer.getId(), receivedCustomer.getId());
    assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
    assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
    assertEquals(customer.getAddress().getId(), receivedCustomer.getAddress().getId());
    // give a bit time for consumer to delete after done
    Thread.sleep(1000);
    assertEntitiesInDatabase(0, Customer.class.getName());
    assertEntitiesInDatabase(0, Address.class.getName());
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) Processor(org.apache.camel.Processor) Address(org.apache.camel.examples.Address) Customer(org.apache.camel.examples.Customer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with CountDownLatch

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

the class KafkaProducerFullTest method producedStringMessageIsReceivedByKafka.

@Test
public void producedStringMessageIsReceivedByKafka() throws InterruptedException, IOException {
    int messageInTopic = 10;
    int messageInOtherTopic = 5;
    CountDownLatch messagesLatch = new CountDownLatch(messageInTopic + messageInOtherTopic);
    sendMessagesInRoute(messageInTopic, stringsTemplate, "IT test message", KafkaConstants.PARTITION_KEY, "1");
    sendMessagesInRoute(messageInOtherTopic, stringsTemplate, "IT test message in other topic", KafkaConstants.PARTITION_KEY, "1", KafkaConstants.TOPIC, TOPIC_STRINGS_IN_HEADER);
    createKafkaMessageConsumer(stringsConsumerConn, TOPIC_STRINGS, TOPIC_STRINGS_IN_HEADER, messagesLatch);
    boolean allMessagesReceived = messagesLatch.await(200, TimeUnit.MILLISECONDS);
    assertTrue("Not all messages were published to the kafka topics. Not received: " + messagesLatch.getCount(), allMessagesReceived);
    List<Exchange> exchangeList = mockEndpoint.getExchanges();
    assertEquals("Fifteen Exchanges are expected", exchangeList.size(), 15);
    for (Exchange exchange : exchangeList) {
        @SuppressWarnings("unchecked") List<RecordMetadata> recordMetaData1 = (List<RecordMetadata>) (exchange.getIn().getHeader(KafkaConstants.KAFKA_RECORDMETA));
        assertEquals("One RecordMetadata is expected.", recordMetaData1.size(), 1);
        assertTrue("Offset is positive", recordMetaData1.get(0).offset() >= 0);
        assertTrue("Topic Name start with 'test'", recordMetaData1.get(0).topic().startsWith("test"));
    }
}
Also used : Exchange(org.apache.camel.Exchange) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 24 with CountDownLatch

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

the class KafkaProducerFullTest method producedStringMessageIsIntercepted.

@Test
public void producedStringMessageIsIntercepted() throws InterruptedException, IOException {
    int messageInTopic = 10;
    int messageInOtherTopic = 5;
    CountDownLatch messagesLatch = new CountDownLatch(messageInTopic + messageInOtherTopic);
    sendMessagesInRoute(messageInTopic, interceptedTemplate, "IT test message", KafkaConstants.PARTITION_KEY, "1");
    sendMessagesInRoute(messageInOtherTopic, interceptedTemplate, "IT test message in other topic", KafkaConstants.PARTITION_KEY, "1", KafkaConstants.TOPIC, TOPIC_STRINGS_IN_HEADER);
    createKafkaMessageConsumer(stringsConsumerConn, TOPIC_INTERCEPTED, TOPIC_STRINGS_IN_HEADER, messagesLatch);
    boolean allMessagesReceived = messagesLatch.await(200, TimeUnit.MILLISECONDS);
    assertTrue("Not all messages were published to the kafka topics. Not received: " + messagesLatch.getCount(), allMessagesReceived);
    assertEquals(messageInTopic + messageInOtherTopic, MockProducerInterceptor.recordsCaptured.size());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 25 with CountDownLatch

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

the class KafkaProducerFullTest method producedString2MessageIsReceivedByKafka.

@Test
public void producedString2MessageIsReceivedByKafka() throws InterruptedException, IOException {
    int messageInTopic = 10;
    int messageInOtherTopic = 5;
    CountDownLatch messagesLatch = new CountDownLatch(messageInTopic + messageInOtherTopic);
    sendMessagesInRoute(messageInTopic, stringsTemplate2, "IT test message", (String[]) null);
    sendMessagesInRoute(messageInOtherTopic, stringsTemplate2, "IT test message in other topic", KafkaConstants.PARTITION_KEY, "1", KafkaConstants.TOPIC, TOPIC_STRINGS_IN_HEADER);
    createKafkaMessageConsumer(stringsConsumerConn, TOPIC_STRINGS, TOPIC_STRINGS_IN_HEADER, messagesLatch);
    boolean allMessagesReceived = messagesLatch.await(200, TimeUnit.MILLISECONDS);
    assertTrue("Not all messages were published to the kafka topics. Not received: " + messagesLatch.getCount(), allMessagesReceived);
    List<Exchange> exchangeList = mockEndpoint.getExchanges();
    assertEquals("Fifteen Exchanges are expected", exchangeList.size(), 15);
    for (Exchange exchange : exchangeList) {
        @SuppressWarnings("unchecked") List<RecordMetadata> recordMetaData1 = (List<RecordMetadata>) (exchange.getIn().getHeader(KafkaConstants.KAFKA_RECORDMETA));
        assertEquals("One RecordMetadata is expected.", recordMetaData1.size(), 1);
        assertTrue("Offset is positive", recordMetaData1.get(0).offset() >= 0);
        assertTrue("Topic Name start with 'test'", recordMetaData1.get(0).topic().startsWith("test"));
    }
}
Also used : Exchange(org.apache.camel.Exchange) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

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