import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.StickyAssignor;
import org.apache.kafka.common.serialization.StringDeserializer;
-import org.springframework.kafka.support.serializer.JsonDeserializer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
{
@Bean
public ExampleConsumer exampleConsumer(
- Consumer<String, Message> kafkaConsumer,
+ Consumer<String, String> kafkaConsumer,
ApplicationProperties properties,
ConfigurableApplicationContext applicationContext)
{
}
@Bean(destroyMethod = "")
- public KafkaConsumer<String, Message> kafkaConsumer(ApplicationProperties properties)
+ public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
{
Properties props = new Properties();
props.put("bootstrap.servers", properties.getBootstrapServer());
props.put("metadata.maxage.ms", 5000); // 5 Sekunden
props.put("partition.assignment.strategy", StickyAssignor.class.getName());
props.put("key.deserializer", StringDeserializer.class.getName());
- props.put("value.deserializer", JsonDeserializer.class.getName());
+ props.put("value.deserializer", StringDeserializer.class.getName());
props.put("spring.json.trusted.packages", "de.juplo.messages");
return new KafkaConsumer<>(props);
{
private final String id;
private final String topic;
- private final Consumer<String, Message> consumer;
+ private final Consumer<String, String> consumer;
private final Thread workerThread;
private final Runnable closeCallback;
public ExampleConsumer(
String clientId,
String topic,
- Consumer<String, Message> consumer,
+ Consumer<String, String> consumer,
Runnable closeCallback)
{
this.id = clientId;
while (running)
{
- ConsumerRecords<String, Message> records =
+ ConsumerRecords<String, String> records =
consumer.poll(Duration.ofSeconds(1));
log.info("{} - Received {} messages", id, records.count());
- for (ConsumerRecord<String, Message> record : records)
+ for (ConsumerRecord<String, String> record : records)
{
handleRecord(
record.topic(),
Integer partition,
Long offset,
String key,
- Message value)
+ String value)
{
consumed++;
log.info("{} - partition={}-{}, offset={}: {}={}", id, topic, partition, offset, key, value);
- switch (value.getType())
- {
- case ADD -> addNumber((Add)value);
- case CALC -> calcSum((Calculate)value);
- default -> log.error("{} - Ignoring message of unknown typ {}", id, value.getType());
- }
+ // switch (value.getType())
+ // {
+ // case ADD -> addNumber((Add)value);
+ // case CALC -> calcSum((Calculate)value);
+ // default -> log.error("{} - Ignoring message of unknown typ {}", id, value.getType());
+ // }
}
private void addNumber(Add add)