Skip to content

Commit 76fa64f

Browse files
committed
GH-4468: Fix no-op ack detection for non-null Acknowledgment
The built-in DLT logging handler declares its Acknowledgment parameter with the JSpecify @nonnull, which is a TYPE_USE annotation that MethodParameter.getParameterAnnotation() does not see. So noOpAck stayed false and, under a non-manual ack mode, DLT consumption failed with "No Acknowledgment available ... MANUAL AckMode". Use Nullness.forMethodParameter() instead, which understands TYPE_USE and @NullMarked nullness. Add a test for the non-null case. Signed-off-by: Abhishek Moondra <abhishek.moondra@sixt.com>
1 parent 01755d3 commit 76fa64f

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

spring-kafka/src/main/java/org/springframework/kafka/listener/adapter/MessagingMessageListenerAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
import org.apache.kafka.clients.consumer.ConsumerRecords;
4141
import org.apache.kafka.clients.consumer.ShareConsumer;
4242
import org.apache.kafka.common.TopicPartition;
43-
import org.jspecify.annotations.NonNull;
4443
import org.jspecify.annotations.Nullable;
4544
import reactor.core.publisher.Mono;
4645

4746
import org.springframework.core.MethodParameter;
47+
import org.springframework.core.Nullness;
4848
import org.springframework.core.log.LogAccessor;
4949
import org.springframework.expression.BeanResolver;
5050
import org.springframework.expression.Expression;
@@ -883,7 +883,7 @@ protected Type determineInferredType(@Nullable Method method) { // NOSONAR compl
883883
}
884884
this.hasAckParameter |= isAck;
885885
if (isAck) {
886-
this.noOpAck |= methodParameter.getParameterAnnotation(NonNull.class) != null;
886+
this.noOpAck |= Nullness.forMethodParameter(methodParameter) == Nullness.NON_NULL;
887887
}
888888
isNotConvertible |= isAck;
889889
boolean isConsumer = parameterIsType(parameterType, Consumer.class);

spring-kafka/src/test/java/org/springframework/kafka/listener/adapter/MessagingMessageListenerAdapterTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.concurrent.CompletableFuture;
2121

2222
import org.apache.kafka.clients.consumer.ConsumerRecord;
23+
import org.jspecify.annotations.NonNull;
24+
import org.jspecify.annotations.Nullable;
2325
import org.junit.jupiter.api.Test;
2426
import reactor.core.publisher.Mono;
2527

@@ -31,6 +33,7 @@
3133
import org.springframework.messaging.support.GenericMessage;
3234

3335
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
36+
import static org.assertj.core.api.Assertions.assertThatNoException;
3437
import static org.mockito.ArgumentMatchers.any;
3538
import static org.mockito.ArgumentMatchers.anyBoolean;
3639
import static org.mockito.BDDMockito.willReturn;
@@ -118,7 +121,24 @@ void testMissingAck() throws NoSuchMethodException, SecurityException {
118121
.withStackTraceContaining("MANUAL");
119122
}
120123

121-
public void test(Acknowledgment ack) {
124+
@Test
125+
void noOpAckWhenAcknowledgmentParameterIsNonNull() throws NoSuchMethodException {
126+
KafkaListenerAnnotationBeanPostProcessor<String, String> bpp = new KafkaListenerAnnotationBeanPostProcessor<>();
127+
Method method = getClass().getDeclaredMethod("testNonNullAck", Acknowledgment.class);
128+
RecordMessagingMessageListenerAdapter<String, String> adapter =
129+
new RecordMessagingMessageListenerAdapter<>(this, method);
130+
adapter.setHandlerMethod(
131+
new HandlerAdapter(bpp.getMessageHandlerMethodFactory().createInvocableHandlerMethod(this, method)));
132+
// A non-null Acknowledgment parameter must substitute a no-op ack, not fail with "No Acknowledgment available"
133+
assertThatNoException().isThrownBy(() -> adapter.onMessage(
134+
new ConsumerRecord<>("foo", 0, 0L, null, "foo"), null, null));
135+
}
136+
137+
public void test(@Nullable Acknowledgment ack) {
138+
139+
}
140+
141+
public void testNonNullAck(@NonNull Acknowledgment ack) {
122142

123143
}
124144

0 commit comments

Comments
 (0)