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();
}
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());
}
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"));
}
}
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());
}
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"));
}
}
Aggregations