Skip to content

Commit 5c912b1

Browse files
committed
Fix KafkaMessageSource for the latest SK
* Add `mockito-inline` dep to be able to mock `final` classes, e.g. `record`
1 parent 488831d commit 5c912b1

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

build.gradle

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ ext {
8484
log4jVersion = '2.17.1'
8585
mailVersion = '2.0.1'
8686
micrometerVersion = '2.0.0-SNAPSHOT'
87-
mockitoVersion = '4.2.0'
87+
mockitoVersion = '4.3.1'
8888
mongoDriverVersion = '4.5.0'
8989
mysqlVersion = '8.0.28'
9090
pahoMqttClientVersion = '1.2.5'
@@ -158,6 +158,7 @@ allprojects {
158158
mavenBom "io.projectreactor:reactor-bom:$reactorVersion"
159159
mavenBom "org.apache.logging.log4j:log4j-bom:$log4jVersion"
160160
mavenBom "org.springframework.data:spring-data-bom:$springDataVersion"
161+
mavenBom "org.mockito:mockito-bom:$mockitoVersion"
161162
}
162163

163164
}
@@ -445,7 +446,8 @@ project('spring-integration-test-support') {
445446
dependencies {
446447
compileOnly 'org.apiguardian:apiguardian-api:1.0.0'
447448
api "org.hamcrest:hamcrest-library:$hamcrestVersion"
448-
api "org.mockito:mockito-core:$mockitoVersion"
449+
api 'org.mockito:mockito-core'
450+
api 'org.mockito:mockito-inline'
449451
api "org.assertj:assertj-core:$assertjVersion"
450452
api 'org.springframework:spring-context'
451453
api 'org.springframework:spring-messaging'

spring-integration-kafka/src/main/java/org/springframework/integration/kafka/inbound/KafkaMessageSource.java

+17-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 the original author or authors.
2+
* Copyright 2018-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,10 +55,10 @@
5555
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
5656
import org.springframework.kafka.listener.ConsumerAwareRebalanceListener;
5757
import org.springframework.kafka.listener.ConsumerProperties;
58-
import org.springframework.kafka.listener.ListenerUtils;
5958
import org.springframework.kafka.listener.LoggingCommitCallback;
6059
import org.springframework.kafka.support.Acknowledgment;
6160
import org.springframework.kafka.support.KafkaHeaders;
61+
import org.springframework.kafka.support.KafkaUtils;
6262
import org.springframework.kafka.support.LogIfLevelEnabled;
6363
import org.springframework.kafka.support.TopicPartitionOffset;
6464
import org.springframework.kafka.support.converter.KafkaMessageHeaders;
@@ -656,21 +656,13 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
656656

657657
/**
658658
* AcknowledgmentCallbackFactory for KafkaAckInfo.
659+
* @param consumerProperties the properties.
659660
* @param <K> the key type.
660661
* @param <V> the value type.
661662
*
662663
*/
663-
public static class KafkaAckCallbackFactory<K, V> implements AcknowledgmentCallbackFactory<KafkaAckInfo<K, V>> {
664-
665-
private final ConsumerProperties consumerProperties;
666-
667-
/**
668-
* Construct an instance with the provided properties.
669-
* @param consumerProperties the properties.
670-
*/
671-
public KafkaAckCallbackFactory(ConsumerProperties consumerProperties) {
672-
this.consumerProperties = consumerProperties;
673-
}
664+
public record KafkaAckCallbackFactory<K, V>(ConsumerProperties consumerProperties)
665+
implements AcknowledgmentCallbackFactory<KafkaAckInfo<K, V>> {
674666

675667
@Override
676668
public AcknowledgmentCallback createCallback(KafkaAckInfo<K, V> info) {
@@ -737,15 +729,10 @@ public void acknowledge(Status status) {
737729
try {
738730
ConsumerRecord<K, V> record = this.ackInfo.getRecord();
739731
switch (status) {
740-
case ACCEPT:
741-
case REJECT:
742-
commitIfPossible(record);
743-
break;
744-
case REQUEUE:
745-
rollback(record);
746-
break;
747-
default:
748-
break;
732+
case ACCEPT, REJECT -> commitIfPossible(record);
733+
case REQUEUE -> rollback(record);
734+
default -> {
735+
}
749736
}
750737
}
751738
catch (WakeupException e) {
@@ -774,7 +761,8 @@ private void rollback(ConsumerRecord<K, V> record) {
774761
})
775762
.collect(Collectors.toList());
776763
if (rewound.size() > 0) {
777-
this.logger.warn(() -> "Rolled back " + ListenerUtils.recordToString(record, this.logOnlyMetadata)
764+
KafkaUtils.setLogOnlyMetadata(this.logOnlyMetadata);
765+
this.logger.warn(() -> "Rolled back " + KafkaUtils.format(record)
778766
+ " later in-flight offsets "
779767
+ rewound + " will also be re-fetched");
780768
}
@@ -783,9 +771,10 @@ private void rollback(ConsumerRecord<K, V> record) {
783771
}
784772

785773
private void commitIfPossible(ConsumerRecord<K, V> record) { // NOSONAR
774+
KafkaUtils.setLogOnlyMetadata(this.logOnlyMetadata);
786775
if (this.ackInfo.isRolledBack()) {
787776
this.logger.warn(() -> "Cannot commit offset for "
788-
+ ListenerUtils.recordToString(record, this.logOnlyMetadata)
777+
+ KafkaUtils.format(record)
789778
+ "; an earlier offset was rolled back");
790779
}
791780
else {
@@ -809,16 +798,14 @@ private void commitIfPossible(ConsumerRecord<K, V> record) { // NOSONAR
809798
ackInformation = toCommit.get(toCommit.size() - 1);
810799
KafkaAckInfo<K, V> ackInformationToLog = ackInformation;
811800
this.commitLogger.log(() -> "Committing pending offsets for "
812-
+ ListenerUtils.recordToString(record, this.logOnlyMetadata)
801+
+ KafkaUtils.format(record)
813802
+ " and all deferred to "
814-
+ ListenerUtils.recordToString(ackInformationToLog.getRecord(),
815-
this.logOnlyMetadata));
816-
candidates.removeAll(toCommit);
803+
+ KafkaUtils.format(ackInformationToLog.getRecord()));
804+
toCommit.forEach(candidates::remove);
817805
}
818806
else {
819807
ackInformation = this.ackInfo;
820-
this.commitLogger.log(() -> "Committing offset for "
821-
+ ListenerUtils.recordToString(record, this.logOnlyMetadata));
808+
this.commitLogger.log(() -> "Committing offset for " + KafkaUtils.format(record));
822809
}
823810
}
824811
else { // earlier offsets present

0 commit comments

Comments
 (0)