package de.juplo.kafka;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
-import org.apache.kafka.common.serialization.LongDeserializer;
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.util.Optional;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.function.Consumer;
@Configuration
-@EnableConfigurationProperties(ApplicationProperties.class)
+@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
public class ApplicationConfiguration
{
@Bean
- public Consumer<ConsumerRecord<String, ClientMessage>> consumer()
+ public ApplicationRecordHandler recordHandler(
+ AdderResults adderResults,
+ KafkaProperties kafkaProperties,
+ ApplicationProperties applicationProperties)
{
- return (record) ->
- {
- // Handle record
- };
+ return new ApplicationRecordHandler(
+ adderResults,
+ Optional.ofNullable(applicationProperties.getThrottle()),
+ kafkaProperties.getClientId());
}
@Bean
- public EndlessConsumer<String, ClientMessage> endlessConsumer(
- KafkaConsumer<String, ClientMessage> kafkaConsumer,
+ public AdderResults adderResults()
+ {
+ return new AdderResults();
+ }
+
+ @Bean
+ public ApplicationRebalanceListener rebalanceListener(
+ ApplicationRecordHandler recordHandler,
+ AdderResults adderResults,
+ StateRepository stateRepository,
+ KafkaProperties kafkaProperties,
+ ApplicationProperties applicationProperties)
+ {
+ return new ApplicationRebalanceListener(
+ recordHandler,
+ adderResults,
+ stateRepository,
+ kafkaProperties.getClientId());
+ }
+
+ @Bean
+ public EndlessConsumer<String, Message> endlessConsumer(
+ KafkaConsumer<String, Message> kafkaConsumer,
ExecutorService executor,
- Consumer<ConsumerRecord<String, ClientMessage>> handler,
- ApplicationProperties properties)
+ ApplicationRebalanceListener rebalanceListener,
+ ApplicationRecordHandler recordHandler,
+ KafkaProperties kafkaProperties,
+ ApplicationProperties applicationProperties)
{
return
new EndlessConsumer<>(
executor,
- properties.getClientId(),
- properties.getTopic(),
+ kafkaProperties.getClientId(),
+ applicationProperties.getTopic(),
kafkaConsumer,
- handler);
+ rebalanceListener,
+ recordHandler);
}
@Bean
}
@Bean(destroyMethod = "close")
- public KafkaConsumer<String, ClientMessage> kafkaConsumer(ApplicationProperties properties)
+ public KafkaConsumer<String, Message> kafkaConsumer(KafkaProperties kafkaProperties)
{
Properties props = new Properties();
- props.put("bootstrap.servers", properties.getBootstrapServer());
- props.put("group.id", properties.getGroupId());
- props.put("client.id", properties.getClientId());
- props.put("auto.offset.reset", properties.getAutoOffsetReset());
- props.put("metadata.max.age.ms", "1000");
+ props.put("bootstrap.servers", kafkaProperties.getBootstrapServers());
+ props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.StickyAssignor");
+ props.put("group.id", kafkaProperties.getConsumer().getGroupId());
+ props.put("client.id", kafkaProperties.getClientId());
+ props.put("auto.offset.reset", kafkaProperties.getConsumer().getAutoOffsetReset());
+ props.put("auto.commit.interval.ms", (int)kafkaProperties.getConsumer().getAutoCommitInterval().toMillis());
+ 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.VALUE_DEFAULT_TYPE, ClientMessage.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);
}