package de.juplo.kafka;
import org.apache.kafka.clients.consumer.Consumer;
-import org.apache.kafka.clients.consumer.KafkaConsumer;
-import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.kafka.support.serializer.JsonDeserializer;
import java.time.Clock;
import java.util.Optional;
-import java.util.Properties;
+import org.springframework.kafka.core.ConsumerFactory;
+
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Bean
public EndlessConsumer<String, Message> endlessConsumer(
- KafkaConsumer<String, Message> kafkaConsumer,
+ Consumer<String, Message> kafkaConsumer,
ExecutorService executor,
ApplicationRebalanceListener rebalanceListener,
ApplicationRecordHandler recordHandler,
}
@Bean(destroyMethod = "close")
- public KafkaConsumer<String, Message> kafkaConsumer(KafkaProperties kafkaProperties)
+ public Consumer<String, Message> kafkaConsumer(ConsumerFactory<String, Message> factory)
{
- Properties props = new Properties();
-
- props.put("bootstrap.servers", kafkaProperties.getBootstrapServers());
- props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
- props.put("group.id", kafkaProperties.getConsumer().getGroupId());
- props.put("client.id", kafkaProperties.getClientId());
- props.put("enable.auto.commit", false);
- props.put("auto.offset.reset", kafkaProperties.getConsumer().getAutoOffsetReset());
- props.put("metadata.max.age.ms", kafkaProperties.getConsumer().getProperties().get("metadata.max.age.ms"));
- props.put("key.deserializer", StringDeserializer.class.getName());
- props.put("value.deserializer", JsonDeserializer.class.getName());
- props.put(JsonDeserializer.TRUSTED_PACKAGES, "de.juplo.kafka");
- props.put(JsonDeserializer.TYPE_MAPPINGS,
- Message.Type.ADD + ":" + MessageAddNumber.class.getName() + "," +
- Message.Type.CALC + ":" + MessageCalculateSum.class.getName());
-
- return new KafkaConsumer<>(props);
+ return factory.createConsumer();
}
}
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import static org.awaitility.Awaitility.*;
-@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
+@SpringJUnitConfig(
+ initializers = ConfigDataApplicationContextInitializer.class,
+ classes = {
+ KafkaAutoConfiguration.class,
+ ApplicationTests.Configuration.class })
@TestPropertySource(
properties = {
"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
@Autowired
- KafkaConsumer<K, V> kafkaConsumer;
+ org.apache.kafka.clients.consumer.Consumer<K, V> kafkaConsumer;
@Autowired
Consumer<ConsumerRecord<K, V>> consumer;
@Autowired
ApplicationProperties applicationProperties;
- @Autowired
- KafkaProperties kafkaProperties;
+ @Autowired
+ KafkaProperties kafkaProperties;
@Autowired
ExecutorService executor;
@Autowired