Skip to content

Fix KafkaItemReader ExecutionContext deserialization error when using Jackson2ExecutionContextStringSerializer #4863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,8 @@ public class KafkaItemReader<K, V> extends AbstractItemStreamItemReader<V> {

private static final long DEFAULT_POLL_TIMEOUT = 30L;

private final String topicName;

private final List<TopicPartition> topicPartitions;

private Map<TopicPartition, Long> partitionOffsets;
Expand Down Expand Up @@ -110,6 +112,7 @@ public KafkaItemReader(Properties consumerProperties, String topicName, List<Int
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG + " property must be provided");
this.consumerProperties = consumerProperties;
Assert.hasLength(topicName, "Topic name must not be null or empty");
this.topicName = topicName;
Assert.isTrue(!partitions.isEmpty(), "At least one partition must be provided");
this.topicPartitions = new ArrayList<>();
for (Integer partition : partitions) {
Expand Down Expand Up @@ -174,10 +177,10 @@ public void open(ExecutionContext executionContext) {
}
}
if (this.saveState && executionContext.containsKey(TOPIC_PARTITION_OFFSETS)) {
Map<TopicPartition, Long> offsets = (Map<TopicPartition, Long>) executionContext
.get(TOPIC_PARTITION_OFFSETS);
for (Map.Entry<TopicPartition, Long> entry : offsets.entrySet()) {
this.partitionOffsets.put(entry.getKey(), entry.getValue() == 0 ? 0 : entry.getValue() + 1);
Map<String, Long> offsets = (Map<String, Long>) executionContext.get(TOPIC_PARTITION_OFFSETS);
for (Map.Entry<String, Long> entry : offsets.entrySet()) {
this.partitionOffsets.put(new TopicPartition(this.topicName, Integer.parseInt(entry.getKey())),
entry.getValue() == 0 ? 0 : entry.getValue() + 1);
}
}
this.kafkaConsumer.assign(this.topicPartitions);
Expand All @@ -203,7 +206,11 @@ public V read() {
@Override
public void update(ExecutionContext executionContext) {
if (this.saveState) {
executionContext.put(TOPIC_PARTITION_OFFSETS, new HashMap<>(this.partitionOffsets));
Map<String, Long> offsets = new HashMap<>();
for (Map.Entry<TopicPartition, Long> entry : this.partitionOffsets.entrySet()) {
offsets.put(String.valueOf(entry.getKey().partition()), entry.getValue());
}
executionContext.put(TOPIC_PARTITION_OFFSETS, offsets);
}
this.kafkaConsumer.commitSync();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -256,8 +256,8 @@ void testReadFromSinglePartitionAfterRestart() throws ExecutionException, Interr
future.get();
}
ExecutionContext executionContext = new ExecutionContext();
Map<TopicPartition, Long> offsets = new HashMap<>();
offsets.put(new TopicPartition("topic3", 0), 1L);
Map<String, Long> offsets = new HashMap<>();
offsets.put("0", 1L);
executionContext.put("topic.partition.offsets", offsets);

// topic3-0: val0, val1, val2, val3, val4
Expand Down Expand Up @@ -297,9 +297,9 @@ void testReadFromMultiplePartitionsAfterRestart() throws ExecutionException, Int
}

ExecutionContext executionContext = new ExecutionContext();
Map<TopicPartition, Long> offsets = new HashMap<>();
offsets.put(new TopicPartition("topic4", 0), 1L);
offsets.put(new TopicPartition("topic4", 1), 2L);
Map<String, Long> offsets = new HashMap<>();
offsets.put("0", 1L);
offsets.put("1", 2L);
executionContext.put("topic.partition.offsets", offsets);

// topic4-0: val0, val2, val4, val6
Expand Down