-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* GH-3183: Add ReactiveRequestHandlerAdvice Fixes #3183 * Introduce a `ReactiveRequestHandlerAdvice` with a `BiFunction<Message<?>, Mono<?>, Publisher<?>>` logic to apply a `Mono.transform()` operator for a returned from the handler `Mono` reply * Fix `WebFluxRequestExecutingMessageHandler` to return a `Mono.then()` instead of an explicit subscription - it happens downstream anyway during reply producing with a proper error handling, too * Demonstrate `ReactiveRequestHandlerAdvice` in the `RSocketDslTests` - without `retry()` it fails * Add `ConsumerEndpointSpec.customizeMonoReply()` for convenience * Document `ReactiveRequestHandlerAdvice` feature * * Fix language in docs
- Loading branch information
1 parent
8a38a5e
commit c84d264
Showing
10 changed files
with
169 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...in/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ain/java/org/springframework/integration/handler/advice/ReactiveRequestHandlerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.handler.advice; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.reactivestreams.Publisher; | ||
|
||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* A {@link MethodInterceptor} for message handlers producing a {@link Mono} as a payload for reply. | ||
* The returned {@link Mono} is customized via {@link Mono#transform(Function)} operator | ||
* calling provided {@code replyCustomizer} {@link BiFunction} with request message as a context. | ||
* | ||
* A customization assumes to use supporting reactive operators like {@link Mono#timeout}, | ||
* {@link Mono#retry}, {@link Mono#tag} etc. | ||
* A {@link Mono#transform(Function)} also can be used for further customization like reactive circuit breaker. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 5.3 | ||
*/ | ||
public class ReactiveRequestHandlerAdvice implements MethodInterceptor { | ||
|
||
private static final Log LOGGER = LogFactory.getLog(ReactiveRequestHandlerAdvice.class); | ||
|
||
private final BiFunction<Message<?>, Mono<?>, Publisher<?>> replyCustomizer; | ||
|
||
/** | ||
* Instantiate advice based on a provided {@link BiFunction} customizer. | ||
* @param replyCustomizer the {@link BiFunction} to customize produced {@link Mono}. | ||
*/ | ||
public ReactiveRequestHandlerAdvice(BiFunction<Message<?>, Mono<?>, Publisher<?>> replyCustomizer) { | ||
Assert.notNull(replyCustomizer, "'replyCustomizer' must not be null"); | ||
this.replyCustomizer = replyCustomizer; | ||
} | ||
|
||
@Override | ||
public final Object invoke(MethodInvocation invocation) throws Throwable { | ||
Object result = invocation.proceed(); | ||
|
||
Method method = invocation.getMethod(); | ||
Object invocationThis = invocation.getThis(); | ||
Object[] arguments = invocation.getArguments(); | ||
boolean isReactiveMethod = | ||
method.getName().equals("handleRequestMessage") && | ||
(arguments.length == 1 && arguments[0] instanceof Message) && | ||
result instanceof Mono<?>; | ||
if (!isReactiveMethod) { | ||
if (LOGGER.isWarnEnabled()) { | ||
String clazzName = | ||
invocationThis == null | ||
? method.getDeclaringClass().getName() | ||
: invocationThis.getClass().getName(); | ||
LOGGER.warn("This advice " + getClass().getName() + | ||
" can only be used for MessageHandlers with reactive reply; an attempt to advise method '" | ||
+ method.getName() + "' in '" + clazzName + "' is ignored."); | ||
} | ||
return result; | ||
} | ||
|
||
Mono<?> replyMono = (Mono<?>) result; | ||
|
||
Message<?> requestMessage = (Message<?>) arguments[0]; | ||
|
||
return replyMono | ||
.transform(mono -> this.replyCustomizer.apply(requestMessage, mono)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters