Search in sources :

Example 1 with MessageListenerStrategy

use of org.sdase.commons.server.kafka.consumer.strategies.MessageListenerStrategy in project sda-dropwizard-commons by SDA-SE.

the class KafkaBundleWithConfigIT method kafkaConsumerReceivesMessagesAsyncCommit.

@Test
public void kafkaConsumerReceivesMessagesAsyncCommit() {
    String topic = "kafkaConsumerReceivesMessagesAsyncCommit";
    StringDeserializer deserializer = new StringDeserializer();
    KAFKA.getKafkaTestUtils().createTopic(topic, 1, (short) 1);
    // register adhoc implementations
    assertThat(kafkaBundle).isNotNull();
    kafkaBundle.createMessageListener(MessageListenerRegistration.builder().withListenerConfig(LISTENER_CONFIG_ASYNC).forTopic(topic).withConsumerConfig(ConsumerConfig.builder().withGroup(UUID.randomUUID().toString()).withClientId(CLIENT_ID_ASYNC).build()).withKeyDeserializer(deserializer).withValueDeserializer(deserializer).withListenerStrategy(new MessageListenerStrategy<String, String>() {

        @Override
        public void processRecords(ConsumerRecords<String, String> records, KafkaConsumer<String, String> consumer) {
            records.forEach(r -> resultsString.add(r.value()));
            consumer.commitSync();
            callbackCount++;
        }

        @Override
        public void commitOnClose(KafkaConsumer<String, String> consumer) {
        }

        @Override
        public void verifyConsumerConfig(Map<String, String> config) {
        }
    }).build());
    List<String> checkMessages = new ArrayList<>();
    // pass in messages
    for (int i = 0; i < KafkaBundleConsts.N_MESSAGES; i++) {
        String message = UUID.randomUUID().toString();
        checkMessages.add(message);
        stringStringProducer.send(new ProducerRecord<>(topic, message));
    }
    await().atMost(KafkaBundleConsts.N_MAX_WAIT_MS, MILLISECONDS).untilAsserted(() -> {
        assertThat(resultsString).containsExactlyInAnyOrderElementsOf(checkMessages);
        assertThat(callbackCount).isPositive();
    });
}
Also used : AutocommitMLS(org.sdase.commons.server.kafka.consumer.strategies.autocommit.AutocommitMLS) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MessageListenerRegistration(org.sdase.commons.server.kafka.builder.MessageListenerRegistration) ListenerConfig(org.sdase.commons.server.kafka.config.ListenerConfig) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) KafkaHelper(org.sdase.commons.server.kafka.consumer.KafkaHelper) MessageListenerStrategy(org.sdase.commons.server.kafka.consumer.strategies.MessageListenerStrategy) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Map(java.util.Map) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) ClassRule(org.junit.ClassRule) KafkaJsonSerializer(org.sdase.commons.server.kafka.serializers.KafkaJsonSerializer) TopicPartition(org.apache.kafka.common.TopicPartition) Awaitility.await(org.awaitility.Awaitility.await) AfterClass(org.junit.AfterClass) Set(java.util.Set) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) UUID(java.util.UUID) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) List(java.util.List) ConsumerConfig(org.sdase.commons.server.kafka.config.ConsumerConfig) ConfigOverride.config(io.dropwizard.testing.ConfigOverride.config) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) IgnoreAndProceedErrorHandler(org.sdase.commons.server.kafka.consumer.IgnoreAndProceedErrorHandler) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ProducerConfig(org.sdase.commons.server.kafka.config.ProducerConfig) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) BeforeClass(org.junit.BeforeClass) TestRule(org.junit.rules.TestRule) SharedKafkaTestResource(com.salesforce.kafka.test.junit4.SharedKafkaTestResource) KafkaJsonDeserializer(org.sdase.commons.server.kafka.serializers.KafkaJsonDeserializer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WrappedNoSerializationErrorDeserializer(org.sdase.commons.server.kafka.serializers.WrappedNoSerializationErrorDeserializer) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) DropwizardAppRule(io.dropwizard.testing.junit.DropwizardAppRule) KafkaTestApplication(org.sdase.commons.server.kafka.dropwizard.KafkaTestApplication) ConfigurationException(org.sdase.commons.server.kafka.exception.ConfigurationException) ProducerRegistration(org.sdase.commons.server.kafka.builder.ProducerRegistration) SyncCommitMLS(org.sdase.commons.server.kafka.consumer.strategies.synccommit.SyncCommitMLS) Before(org.junit.Before) ErrorHandler(org.sdase.commons.server.kafka.consumer.ErrorHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) MessageHandler(org.sdase.commons.server.kafka.consumer.MessageHandler) RuleChain(org.junit.rules.RuleChain) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageProducer(org.sdase.commons.server.kafka.producer.MessageProducer) ResourceHelpers.resourceFilePath(io.dropwizard.testing.ResourceHelpers.resourceFilePath) MessageListener(org.sdase.commons.server.kafka.consumer.MessageListener) KafkaTestConfiguration(org.sdase.commons.server.kafka.dropwizard.KafkaTestConfiguration) Collections(java.util.Collections) SimpleEntity(org.sdase.commons.server.kafka.serializers.SimpleEntity) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SharedKafkaTestResource (com.salesforce.kafka.test.junit4.SharedKafkaTestResource)1 ConfigOverride.config (io.dropwizard.testing.ConfigOverride.config)1 ResourceHelpers.resourceFilePath (io.dropwizard.testing.ResourceHelpers.resourceFilePath)1 DropwizardAppRule (io.dropwizard.testing.junit.DropwizardAppRule)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 TopicPartition (org.apache.kafka.common.TopicPartition)1