Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit 1beaadd

Browse files
Fix duplicate error logging when processing exception thrown in SQS listener.
This commit fixes this issue by delegating exception processing to AbstractMethodMessageHandler only when there is a configured handler. If there is none, exception is logged. Fixes gh-394 Closes gh-465 Co-authored-by: Maciej Walkowiak <[email protected]>
1 parent 2e4449c commit 1beaadd

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

spring-cloud-aws-messaging/src/main/java/org/springframework/cloud/aws/messaging/listener/QueueMessageHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler;
5353
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
5454
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
55+
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
5556
import org.springframework.util.ClassUtils;
5657
import org.springframework.util.comparator.ComparableComparator;
5758
import org.springframework.validation.Errors;
@@ -233,7 +234,15 @@ protected void handleNoMatch(Set<MappingInformation> ts, String lookupDestinatio
233234
@Override
234235
protected void processHandlerMethodException(HandlerMethod handlerMethod,
235236
Exception ex, Message<?> message) {
236-
super.processHandlerMethodException(handlerMethod, ex, message);
237+
InvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(
238+
handlerMethod, ex);
239+
if (exceptionHandlerMethod != null) {
240+
super.processHandlerMethodException(handlerMethod, ex, message);
241+
}
242+
else {
243+
this.logger.error("An exception occurred while invoking the handler method",
244+
ex);
245+
}
237246
throw new MessagingException(
238247
"An exception occurred while invoking the handler method", ex);
239248
}

spring-cloud-aws-messaging/src/main/java/org/springframework/cloud/aws/messaging/listener/SimpleMessageListenerContainer.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public void run() {
418418
applyDeletionPolicyOnSuccess(receiptHandle);
419419
}
420420
catch (MessagingException messagingException) {
421-
applyDeletionPolicyOnError(receiptHandle, messagingException);
421+
applyDeletionPolicyOnError(receiptHandle);
422422
}
423423
}
424424

@@ -430,17 +430,12 @@ private void applyDeletionPolicyOnSuccess(String receiptHandle) {
430430
}
431431
}
432432

433-
private void applyDeletionPolicyOnError(String receiptHandle,
434-
MessagingException messagingException) {
433+
private void applyDeletionPolicyOnError(String receiptHandle) {
435434
if (this.deletionPolicy == SqsMessageDeletionPolicy.ALWAYS
436435
|| (this.deletionPolicy == SqsMessageDeletionPolicy.NO_REDRIVE
437436
&& !this.hasRedrivePolicy)) {
438437
deleteMessage(receiptHandle);
439438
}
440-
else if (this.deletionPolicy == SqsMessageDeletionPolicy.ON_SUCCESS) {
441-
getLogger().error("Exception encountered while processing message.",
442-
messagingException);
443-
}
444439
}
445440

446441
private void deleteMessage(String receiptHandle) {

spring-cloud-aws-messaging/src/test/java/org/springframework/cloud/aws/messaging/listener/QueueMessageHandlerTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.springframework.messaging.support.MessageBuilder;
7171

7272
import static org.assertj.core.api.Assertions.assertThat;
73+
import static org.junit.Assert.fail;
7374
import static org.mockito.ArgumentMatchers.any;
7475
import static org.mockito.ArgumentMatchers.anyString;
7576
import static org.mockito.ArgumentMatchers.eq;
@@ -566,6 +567,40 @@ public void getMappingForMethod_methodWithExpressionProducingMultipleQueueNames_
566567
.containsAll(Arrays.asList("queueOne", "queueTwo"))).isTrue();
567568
}
568569

570+
@Test
571+
public void processHandlerMethodException_invocableHandlerMethodNotAvailable_errorMustBeLogged() {
572+
// Arrange
573+
StaticApplicationContext applicationContext = new StaticApplicationContext();
574+
applicationContext.registerSingleton("sqsListenerWithoutMessageExceptionHandler",
575+
SqsListenerWithoutMessageExceptionHandler.class);
576+
applicationContext.registerBeanDefinition("queueMessageHandler",
577+
getQueueMessageHandlerBeanDefinition());
578+
applicationContext.refresh();
579+
MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
580+
581+
LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
582+
ListAppender<ILoggingEvent> appender = new ListAppender<>();
583+
appender.start();
584+
Logger log = logContext.getLogger(QueueMessageHandler.class);
585+
log.setLevel(Level.ERROR);
586+
log.addAppender(appender);
587+
appender.setContext(log.getLoggerContext());
588+
589+
// Act
590+
try {
591+
messageHandler.handleMessage(MessageBuilder.withPayload("testContent")
592+
.setHeader(QueueMessageHandler.LOGICAL_RESOURCE_ID, "receive")
593+
.build());
594+
fail();
595+
}
596+
catch (MessagingException e) {
597+
// ignore
598+
}
599+
600+
// Assert
601+
assertThat(appender.list).hasSize(1);
602+
}
603+
569604
@SuppressWarnings("UnusedDeclaration")
570605
private static class IncomingMessageHandler {
571606

@@ -604,6 +639,15 @@ private String getLastReceivedMessage() {
604639

605640
}
606641

642+
private static class SqsListenerWithoutMessageExceptionHandler {
643+
644+
@SqsListener("receive")
645+
public String receive(String value) {
646+
throw new RuntimeException("test exception");
647+
}
648+
649+
}
650+
607651
private static class IncomingMessageHandlerWithMultipleQueueNames {
608652

609653
private String lastReceivedMessage;

0 commit comments

Comments
 (0)