Search in sources :

Example 1 with ProducerConfig

use of org.sdase.commons.server.kafka.config.ProducerConfig in project sda-dropwizard-commons by SDA-SE.

the class ProducerRegistrationTest method customProducerCanBeUsed.

@Test
public void customProducerCanBeUsed() {
    Map<String, String> test = new HashMap<>();
    test.put("key.serializer", StringSerializer.class.getName());
    test.put("value.serializer", StringSerializer.class.getName());
    test.put("bootstrap.servers", "localhost:9092");
    ProducerConfig producerConfig = new ProducerConfig();
    producerConfig.getConfig().putAll(test);
    ProducerRegistration<String, String> producerRegistration = ProducerRegistration.<String, String>builder().forTopic("TOPIC").withProducerConfig(producerConfig).build();
    assertThat(producerRegistration).isNotNull();
    assertThat(producerRegistration.getTopic().getTopicName()).isEqualTo("TOPIC");
    assertThat(producerRegistration.getProducerConfig()).isEqualTo(producerConfig);
}
Also used : HashMap(java.util.HashMap) ProducerConfig(org.sdase.commons.server.kafka.config.ProducerConfig) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 2 with ProducerConfig

use of org.sdase.commons.server.kafka.config.ProducerConfig in project sda-dropwizard-commons by SDA-SE.

the class KafkaBundleWithConfigIT method multiTest.

@Test
public void multiTest() {
    final String TOPIC_CREATE = "create";
    final String TOPIC_DELETE = "delete";
    KAFKA.getKafkaTestUtils().createTopic(TOPIC_CREATE, 1, (short) 1);
    KAFKA.getKafkaTestUtils().createTopic(TOPIC_DELETE, 1, (short) 1);
    kafkaBundle.createMessageListener(MessageListenerRegistration.builder().withDefaultListenerConfig().forTopic(TOPIC_CREATE).withDefaultConsumer().withKeyDeserializer(new LongDeserializer()).withListenerStrategy(new SyncCommitMLS<Long, String>(record -> resultsString.add(record.value()), new IgnoreAndProceedErrorHandler<>())).build());
    kafkaBundle.createMessageListener(MessageListenerRegistration.builder().withDefaultListenerConfig().forTopic(TOPIC_DELETE).withDefaultConsumer().withListenerStrategy(new SyncCommitMLS<String, String>(record -> resultsString.add(record.value()), new IgnoreAndProceedErrorHandler<>())).build());
    MessageProducer<Long, String> createProducer = kafkaBundle.registerProducer(ProducerRegistration.<Long, String>builder().forTopic(TOPIC_CREATE).withProducerConfig(new ProducerConfig()).withKeySerializer(new LongSerializer()).build());
    MessageProducer<String, String> deleteProducer = kafkaBundle.registerProducer(ProducerRegistration.<String, String>builder().forTopic(TOPIC_DELETE).withDefaultProducer().build());
    createProducer.send(1L, "test1");
    deleteProducer.send("key", "test2");
    await().atMost(KafkaBundleConsts.N_MAX_WAIT_MS, MILLISECONDS).until(() -> resultsString.size() == 2);
    assertThat(resultsString).containsExactlyInAnyOrder("test1", "test2");
}
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) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) IgnoreAndProceedErrorHandler(org.sdase.commons.server.kafka.consumer.IgnoreAndProceedErrorHandler) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProducerConfig(org.sdase.commons.server.kafka.config.ProducerConfig) Test(org.junit.Test)

Aggregations

StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 Test (org.junit.Test)2 ProducerConfig (org.sdase.commons.server.kafka.config.ProducerConfig)2 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 HashMap (java.util.HashMap)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