Skip to content

Commit b39ca72

Browse files
committed
Refactor simplify partitionSubset logic
Signed-off-by: Su Ko <[email protected]>
1 parent b1d8836 commit b39ca72

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/ConcurrentMessageListenerContainer.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Tomaz Fernandes
6161
* @author Wang Zhiyang
6262
* @author Lokesh Alamuri
63+
* @author Su Ko
6364
*/
6465
public class ConcurrentMessageListenerContainer<K, V> extends AbstractMessageListenerContainer<K, V> {
6566

@@ -320,26 +321,24 @@ private KafkaMessageListenerContainer<K, V> constructContainer(ContainerProperti
320321
if (topicPartitions == null) {
321322
return null;
322323
}
324+
323325
if (this.concurrency == 1) {
324326
return topicPartitions;
325327
}
326-
else {
327-
int numPartitions = topicPartitions.length;
328-
if (numPartitions == this.concurrency) {
329-
return new TopicPartitionOffset[] { topicPartitions[index] };
330-
}
331-
else {
332-
int perContainer = numPartitions / this.concurrency;
333-
TopicPartitionOffset[] subset;
334-
if (index == this.concurrency - 1) {
335-
subset = Arrays.copyOfRange(topicPartitions, index * perContainer, topicPartitions.length);
336-
}
337-
else {
338-
subset = Arrays.copyOfRange(topicPartitions, index * perContainer, (index + 1) * perContainer);
339-
}
340-
return subset;
341-
}
328+
329+
int numPartitions = topicPartitions.length;
330+
331+
if (numPartitions == this.concurrency) {
332+
return new TopicPartitionOffset[] { topicPartitions[index] };
342333
}
334+
335+
int perContainer = numPartitions / this.concurrency;
336+
int start = index * perContainer;
337+
int end = (index == this.concurrency - 1)
338+
? numPartitions
339+
: start + perContainer;
340+
341+
return Arrays.copyOfRange(topicPartitions, start, end);
343342
}
344343

345344
/*

0 commit comments

Comments
 (0)