Skip to content

Commit 76e7901

Browse files
GH-3219: Add HandleMessageAdviceAdapter (#3234)
* GH-3219: Add `HandleMessageAdviceAdapter` Fixes #3219 * * Fix language in doc Co-Authored-By: Gary Russell <[email protected]> Co-authored-by: Gary Russell <[email protected]>
1 parent 7433e41 commit 76e7901

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/ConsumerEndpointSpec.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import java.util.function.BiFunction;
2323

2424
import org.aopalliance.aop.Advice;
25+
import org.aopalliance.intercept.MethodInterceptor;
2526
import org.reactivestreams.Publisher;
2627

2728
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
2829
import org.springframework.integration.handler.AbstractMessageHandler;
2930
import org.springframework.integration.handler.AbstractMessageProducingHandler;
3031
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
32+
import org.springframework.integration.handler.advice.HandleMessageAdviceAdapter;
3133
import org.springframework.integration.handler.advice.ReactiveRequestHandlerAdvice;
3234
import org.springframework.integration.router.AbstractMessageRouter;
3335
import org.springframework.integration.scheduling.PollerMetadata;
@@ -101,6 +103,21 @@ public S taskScheduler(TaskScheduler taskScheduler) {
101103
return _this();
102104
}
103105

106+
/**
107+
* Configure a list of {@link MethodInterceptor} objects to be applied, in nested order, to the
108+
* endpoint's handler. The advice objects are applied to the {@code handleMessage()} method
109+
* and therefore to the whole sub-flow afterwards.
110+
* @param interceptors the advice chain.
111+
* @return the endpoint spec.
112+
* @since 5.3
113+
*/
114+
public S handleMessageAdvice(MethodInterceptor... interceptors) {
115+
for (MethodInterceptor interceptor: interceptors) {
116+
advice(new HandleMessageAdviceAdapter(interceptor));
117+
}
118+
return _this();
119+
}
120+
104121
/**
105122
* Configure a list of {@link Advice} objects to be applied, in nested order, to the
106123
* endpoint's handler. The advice objects are applied only to the handler.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.handler.advice;
18+
19+
import org.aopalliance.intercept.MethodInterceptor;
20+
import org.aopalliance.intercept.MethodInvocation;
21+
22+
import org.springframework.util.Assert;
23+
24+
/**
25+
* A {@link HandleMessageAdvice} implementation with a plain delegation
26+
* to the provided {@link MethodInterceptor}.
27+
* <p> This advice should be used for consumer endpoints to proxy exactly
28+
* a {@link org.springframework.messaging.MessageHandler#handleMessage} and the whole-subflow therefore;
29+
* unlike regular proxying which is applied only for the
30+
* {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}.
31+
*
32+
* @author Artem Bilan
33+
*
34+
* @since 5.3
35+
*/
36+
public class HandleMessageAdviceAdapter implements HandleMessageAdvice {
37+
38+
private final MethodInterceptor delegate;
39+
40+
public HandleMessageAdviceAdapter(MethodInterceptor delegate) {
41+
Assert.notNull(delegate, "The 'delegate' must not be null");
42+
this.delegate = delegate;
43+
}
44+
45+
46+
@Override
47+
public Object invoke(MethodInvocation invocation) throws Throwable {
48+
return this.delegate.invoke(invocation);
49+
}
50+
51+
}

spring-integration-jpa/src/test/java/org/springframework/integration/jpa/dsl/JpaTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -41,6 +41,7 @@
4141
import org.springframework.integration.jpa.test.entity.StudentDomain;
4242
import org.springframework.integration.support.MessageBuilder;
4343
import org.springframework.integration.test.util.OnlyOnceTrigger;
44+
import org.springframework.integration.transaction.TransactionInterceptorBuilder;
4445
import org.springframework.jdbc.core.JdbcTemplate;
4546
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4647
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
@@ -57,6 +58,7 @@
5758
import org.springframework.test.annotation.DirtiesContext;
5859
import org.springframework.test.context.junit4.SpringRunner;
5960
import org.springframework.transaction.PlatformTransactionManager;
61+
import org.springframework.transaction.interceptor.TransactionInterceptor;
6062

6163
/**
6264
* @author Artem Bilan
@@ -230,7 +232,12 @@ public IntegrationFlow outboundAdapterFlow(EntityManagerFactory entityManagerFac
230232
.handle(Jpa.outboundAdapter(entityManagerFactory)
231233
.entityClass(StudentDomain.class)
232234
.persistMode(PersistMode.PERSIST),
233-
e -> e.transactional(true));
235+
e -> e.handleMessageAdvice(transactionInterceptor()));
236+
}
237+
238+
@Bean
239+
public TransactionInterceptor transactionInterceptor() {
240+
return new TransactionInterceptorBuilder().build();
234241
}
235242

236243
@Bean

src/reference/asciidoc/handler-advice.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ Note that, in that case, the entire downstream flow is within the transaction sc
683683

684684
In the case of a `MessageHandler` that does not return a response, the advice chain order is retained.
685685

686+
Starting with version 5.3, the `HandleMessageAdviceAdapter` is present to let apply any existing `MethodInterceptor` for the `MessageHandler.handleMessage()` and, therefore, whole sub-flow.
687+
For example a `RetryOperationsInterceptor` could be applied for the whole sub-flow starting from some endpoint, which is not possible by default because consumer endpoint applies advices only for the `AbstractReplyProducingMessageHandler.RequestHandler.handleRequestMessage()`.
688+
Starting with version 5.3, the `HandleMessageAdviceAdapter` is provided to apply any `MethodInterceptor` for the `MessageHandler.handleMessage()` method and, therefore, the whole sub-flow.
689+
For example, a `RetryOperationsInterceptor` could be applied to the whole sub-flow starting from some endpoint; this is not possible, by default, because the consumer endpoint applies advices only to the `AbstractReplyProducingMessageHandler.RequestHandler.handleRequestMessage()`.
690+
686691
[[tx-handle-message-advice]]
687692
==== Transaction Support
688693

src/reference/asciidoc/whats-new.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ See <<./kotlin-dsl.adoc#kotlin-dsl,Kotlin DSL Chapter>> for more information.
4646
A `ReactiveRequestHandlerAdvice` is provided to customize `Mono` replies from message handlers.
4747
See <<./handler-advice.adoc#reactive-advice,Reactive Advice>> for more information.
4848

49+
[[x5.3-reactive-request-handler-advice]]
50+
==== HandleMessageAdviceAdapter
51+
52+
A `HandleMessageAdviceAdapter` is provided to wrap any `MethodInterceptor` for applying on the `MessageHandler.handleMessage()` instead of a default `AbstractReplyProducingMessageHandler.RequestHandler.handleRequestMessage()` behavior.
53+
See <<./handler-advice.adoc#handle-message-advice,Handling Message Advice>> for more information.
54+
4955
[[x5.3-mongodb-reactive-channel-adapters]]
5056
==== MongoDB Reactive Channel Adapters
5157

0 commit comments

Comments
 (0)