Skip to content

Commit 40ba72b

Browse files
artembilangaryrussell
authored andcommitted
INT-4545: Fix RPMHWrapper registration for `@SA
JIRA: https://jira.spring.io/browse/INT-4545 Improve the logic in the `AbstractMethodAnnotationPostProcessor` to check for existing `MessageHandler` and be sure that we need to register a `ReplyProducingMessageHandlerWrapper` bean **Cherry-pick to 5.0.x**
1 parent 743afaf commit 40ba72b

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ public Object postProcess(Object bean, String beanName, Method method, List<Anno
135135
}
136136
}
137137

138-
boolean handlerExists = false;
138+
Object sourceHandler = null;
139139
if (beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
140-
handlerExists = MessageHandler.class.isAssignableFrom(method.getReturnType());
140+
if (MessageHandler.class.isAssignableFrom(method.getReturnType())) {
141+
sourceHandler = resolveTargetBeanFromMethodWithBeanAnnotation(method);
142+
}
141143
}
142144

143145
MessageHandler handler = createHandler(bean, method, annotations);
@@ -167,7 +169,7 @@ public Object postProcess(Object bean, String beanName, Method method, List<Anno
167169
}
168170
}
169171

170-
if (!handlerExists) {
172+
if (handler != sourceHandler) {
171173
String handlerBeanName = generateHandlerBeanName(beanName, method);
172174
if (handler instanceof ReplyProducingMessageHandlerWrapper
173175
&& StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) {

spring-integration-core/src/main/java/org/springframework/integration/handler/ReplyProducingMessageHandlerWrapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -19,12 +19,13 @@
1919
import org.springframework.context.Lifecycle;
2020
import org.springframework.messaging.Message;
2121
import org.springframework.messaging.MessageHandler;
22+
import org.springframework.util.Assert;
2223

2324
/**
2425
* The {@link AbstractReplyProducingMessageHandler} wrapper around raw {@link MessageHandler}
2526
* for request-reply scenarios, e.g. {@code @ServiceActivator} annotation configuration.
2627
* <p>
27-
* This class is used internally by Framework in cased when request-reply is important
28+
* This class is used internally by Framework in cases when request-reply is important
2829
* and there is no other way to apply advice chain.
2930
* <p>
3031
* The lifecycle control is delegated to the {@code target} {@link MessageHandler}.
@@ -39,6 +40,7 @@ public class ReplyProducingMessageHandlerWrapper extends AbstractReplyProducingM
3940
private final MessageHandler target;
4041

4142
public ReplyProducingMessageHandlerWrapper(MessageHandler target) {
43+
Assert.notNull(target, "'target' must not be null");
4244
this.target = target;
4345
}
4446

spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
import java.lang.annotation.RetentionPolicy;
4141
import java.lang.annotation.Target;
4242
import java.util.Arrays;
43+
import java.util.Date;
4344
import java.util.List;
4445
import java.util.Map;
4546
import java.util.concurrent.CountDownLatch;
4647
import java.util.concurrent.TimeUnit;
4748
import java.util.concurrent.atomic.AtomicInteger;
4849
import java.util.concurrent.atomic.AtomicReference;
4950

51+
import org.aopalliance.aop.Advice;
5052
import org.aopalliance.intercept.MethodInterceptor;
5153
import org.apache.commons.logging.Log;
5254
import org.hamcrest.BaseMatcher;
@@ -109,6 +111,7 @@
109111
import org.springframework.integration.endpoint.PollingConsumer;
110112
import org.springframework.integration.expression.SpelPropertyAccessorRegistrar;
111113
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
114+
import org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice;
112115
import org.springframework.integration.history.MessageHistory;
113116
import org.springframework.integration.history.MessageHistoryConfigurer;
114117
import org.springframework.integration.json.JsonPropertyAccessor;
@@ -718,6 +721,24 @@ public void testIntegrationEvaluationContextCustomization() {
718721
assertEquals(ClassUtils.getStaticMethod(TestSpelFunction.class, "bar", Object.class), testSpelFunction);
719722
}
720723

724+
@Autowired
725+
private MessageChannel myHandlerChannel;
726+
727+
@Autowired
728+
private PollableChannel myHandlerSuccessChannel;
729+
730+
@Test
731+
public void testAdvicedServiceActivator() {
732+
Date testDate = new Date();
733+
734+
this.myHandlerChannel.send(new GenericMessage<>(testDate));
735+
736+
Message<?> receive = this.myHandlerSuccessChannel.receive(10_000);
737+
738+
assertNotNull(receive);
739+
assertEquals(testDate, receive.getPayload());
740+
}
741+
721742
@Configuration
722743
@ComponentScan
723744
@IntegrationComponentScan
@@ -1124,6 +1145,25 @@ public SpelPropertyAccessorRegistrar spelPropertyAccessorRegistrar() {
11241145
return new SpelPropertyAccessorRegistrar(new JsonPropertyAccessor(), new EnvironmentAccessor());
11251146
}
11261147

1148+
@Bean
1149+
@ServiceActivator(inputChannel = "myHandlerChannel", adviceChain = "myHandlerAdvice")
1150+
public MessageHandler myHandler() {
1151+
return message -> { };
1152+
}
1153+
1154+
@Bean
1155+
public Advice myHandlerAdvice() {
1156+
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
1157+
advice.setOnSuccessExpressionString("payload");
1158+
advice.setSuccessChannel(myHandlerSuccessChannel());
1159+
return advice;
1160+
}
1161+
1162+
@Bean
1163+
public QueueChannel myHandlerSuccessChannel() {
1164+
return new QueueChannel();
1165+
}
1166+
11271167
}
11281168

11291169
@Configuration

0 commit comments

Comments
 (0)