Skip to content

Commit d61df01

Browse files
committed
Fix DSL sample in the doc
* Improve JavaDocs for `BaseIntegrationFlowDefinition` with renaming `payloadType` param to `expectedType` since it can also be as a `Message.class`, not only type for the payload. * Add JavaDoc for `LambdaMessageProcessor` ctor
1 parent 7e5fef9 commit d61df01

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

Diff for: spring-integration-core/src/main/java/org/springframework/integration/dsl/BaseIntegrationFlowDefinition.java

+46-46
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ public <P> B convert(Class<P> payloadType) {
668668

669669
/**
670670
* Populate the {@link MessageTransformingHandler} instance for the provided
671-
* {@link GenericTransformer} for the specific {@code payloadType} to convert at
671+
* {@link GenericTransformer} for the specific {@code expectedType} to convert at
672672
* runtime.
673-
* @param payloadType the {@link Class} for expected payload type. It can also be
673+
* @param expectedType the {@link Class} for expected payload type. It can also be
674674
* {@code Message.class} if you wish to access the entire message in the transformer.
675675
* Conversion to this type will be attempted, if necessary.
676676
* @param genericTransformer the {@link GenericTransformer} to populate.
@@ -680,14 +680,14 @@ public <P> B convert(Class<P> payloadType) {
680680
* @see MethodInvokingTransformer
681681
* @see LambdaMessageProcessor
682682
*/
683-
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer) {
684-
return transform(payloadType, genericTransformer, null);
683+
public <P, T> B transform(Class<P> expectedType, GenericTransformer<P, T> genericTransformer) {
684+
return transform(expectedType, genericTransformer, null);
685685
}
686686

687687
/**
688688
* Populate the {@link MessageTransformingHandler} instance
689689
* for the provided {@code payloadType} to convert at runtime.
690-
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
690+
* In addition, accept options for the integration endpoint using {@link GenericEndpointSpec}.
691691
* @param payloadType the {@link Class} for expected payload type.
692692
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
693693
* @param <P> the payload type - 'transform to'.
@@ -706,9 +706,9 @@ public <P> B convert(Class<P> payloadType,
706706

707707
/**
708708
* Populate the {@link MessageTransformingHandler} instance for the provided {@link GenericTransformer}
709-
* for the specific {@code payloadType} to convert at runtime.
709+
* for the specific {@code expectedType} to convert at runtime.
710710
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
711-
* @param payloadType the {@link Class} for expected payload type. It can also be
711+
* @param expectedType the {@link Class} for expected payload type. It can also be
712712
* {@code Message.class} if you wish to access the entire message in the transformer.
713713
* Conversion to this type will be attempted, if necessary.
714714
* @param genericTransformer the {@link GenericTransformer} to populate.
@@ -720,13 +720,13 @@ public <P> B convert(Class<P> payloadType,
720720
* @see LambdaMessageProcessor
721721
* @see GenericEndpointSpec
722722
*/
723-
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer,
723+
public <P, T> B transform(Class<P> expectedType, GenericTransformer<P, T> genericTransformer,
724724
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
725725

726726
Assert.notNull(genericTransformer, "'genericTransformer' must not be null");
727727
Transformer transformer = genericTransformer instanceof Transformer ? (Transformer) genericTransformer :
728728
(ClassUtils.isLambda(genericTransformer.getClass())
729-
? new MethodInvokingTransformer(new LambdaMessageProcessor(genericTransformer, payloadType))
729+
? new MethodInvokingTransformer(new LambdaMessageProcessor(genericTransformer, expectedType))
730730
: new MethodInvokingTransformer(genericTransformer, ClassUtils.TRANSFORMER_TRANSFORM_METHOD));
731731
return addComponent(transformer)
732732
.handle(new MessageTransformingHandler(transformer), endpointConfigurer);
@@ -840,35 +840,35 @@ public B filter(MessageProcessorSpec<?> messageProcessorSpec, Consumer<FilterEnd
840840
/**
841841
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
842842
* for the provided {@link GenericSelector}.
843-
* Typically used with a Java 8 Lambda expression:
843+
* Typically, used with a Java 8 Lambda expression:
844844
* <pre class="code">
845845
* {@code
846846
* .filter(Date.class, p -> p.after(new Date()))
847847
* }
848848
* </pre>
849-
* @param payloadType the {@link Class} for expected payload type. It can also be
849+
* @param expectedType the {@link Class} for expected payload type. It can also be
850850
* {@code Message.class} if you wish to access the entire message in the selector.
851851
* Conversion to this type will be attempted, if necessary.
852852
* @param genericSelector the {@link GenericSelector} to use.
853853
* @param <P> the source payload type or {@code Message.class}.
854854
* @return the current {@link BaseIntegrationFlowDefinition}.
855855
* @see LambdaMessageProcessor
856856
*/
857-
public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector) {
858-
return filter(payloadType, genericSelector, null);
857+
public <P> B filter(Class<P> expectedType, GenericSelector<P> genericSelector) {
858+
return filter(expectedType, genericSelector, null);
859859
}
860860

861861
/**
862862
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
863863
* for the provided {@link GenericSelector}.
864-
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
865-
* Typically used with a Java 8 Lambda expression:
864+
* In addition, accept options for the integration endpoint using {@link FilterEndpointSpec}.
865+
* Typically, used with a Java 8 Lambda expression:
866866
* <pre class="code">
867867
* {@code
868868
* .filter(Date.class, p -> p.after(new Date()), e -> e.autoStartup(false))
869869
* }
870870
* </pre>
871-
* @param payloadType the {@link Class} for expected payload type. It can also be
871+
* @param expectedType the {@link Class} for expected payload type. It can also be
872872
* {@code Message.class} if you wish to access the entire message in the selector.
873873
* Conversion to this type will be attempted, if necessary.
874874
* @param genericSelector the {@link GenericSelector} to use.
@@ -878,13 +878,13 @@ public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector) {
878878
* @see LambdaMessageProcessor
879879
* @see FilterEndpointSpec
880880
*/
881-
public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector,
881+
public <P> B filter(Class<P> expectedType, GenericSelector<P> genericSelector,
882882
Consumer<FilterEndpointSpec> endpointConfigurer) {
883883

884884
Assert.notNull(genericSelector, "'genericSelector' must not be null");
885885
MessageSelector selector = genericSelector instanceof MessageSelector ? (MessageSelector) genericSelector :
886886
(ClassUtils.isLambda(genericSelector.getClass())
887-
? new MethodInvokingSelector(new LambdaMessageProcessor(genericSelector, payloadType))
887+
? new MethodInvokingSelector(new LambdaMessageProcessor(genericSelector, expectedType))
888888
: new MethodInvokingSelector(genericSelector, ClassUtils.SELECTOR_ACCEPT_METHOD));
889889
return this.register(new FilterEndpointSpec(new MessageFilter(selector)), endpointConfigurer);
890890
}
@@ -1001,22 +1001,22 @@ public B handle(Object service, String methodName,
10011001
* Populate a {@link ServiceActivatingHandler} for the
10021002
* {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
10031003
* to invoke the provided {@link GenericHandler} at runtime.
1004-
* Typically used with a Java 8 Lambda expression:
1004+
* Typically, used with a Java 8 Lambda expression:
10051005
* <pre class="code">
10061006
* {@code
10071007
* .handle(Integer.class, (p, h) -> p / 2)
10081008
* }
10091009
* </pre>
1010-
* @param payloadType the {@link Class} for expected payload type. It can also be
1010+
* @param expectedType the {@link Class} for expected payload type. It can also be
10111011
* {@code Message.class} if you wish to access the entire message in the handler.
10121012
* Conversion to this type will be attempted, if necessary.
10131013
* @param handler the handler to invoke.
10141014
* @param <P> the payload type to expect, or {@code Message.class}.
10151015
* @return the current {@link BaseIntegrationFlowDefinition}.
10161016
* @see LambdaMessageProcessor
10171017
*/
1018-
public <P> B handle(Class<P> payloadType, GenericHandler<P> handler) {
1019-
return handle(payloadType, handler, null);
1018+
public <P> B handle(Class<P> expectedType, GenericHandler<P> handler) {
1019+
return handle(expectedType, handler, null);
10201020
}
10211021

10221022
/**
@@ -1030,7 +1030,7 @@ public <P> B handle(Class<P> payloadType, GenericHandler<P> handler) {
10301030
* .handle(Integer.class, (p, h) -> p / 2, e -> e.autoStartup(false))
10311031
* }
10321032
* </pre>
1033-
* @param payloadType the {@link Class} for expected payload type. It can also be
1033+
* @param expectedType the {@link Class} for expected payload type. It can also be
10341034
* {@code Message.class} if you wish to access the entire message in the handler.
10351035
* Conversion to this type will be attempted, if necessary.
10361036
* @param handler the handler to invoke.
@@ -1039,15 +1039,15 @@ public <P> B handle(Class<P> payloadType, GenericHandler<P> handler) {
10391039
* @return the current {@link BaseIntegrationFlowDefinition}.
10401040
* @see LambdaMessageProcessor
10411041
*/
1042-
public <P> B handle(Class<P> payloadType, GenericHandler<P> handler,
1042+
public <P> B handle(Class<P> expectedType, GenericHandler<P> handler,
10431043
Consumer<GenericEndpointSpec<ServiceActivatingHandler>> endpointConfigurer) {
10441044

10451045
ServiceActivatingHandler serviceActivatingHandler;
10461046
if (ClassUtils.isLambda(handler.getClass())) {
1047-
serviceActivatingHandler = new ServiceActivatingHandler(new LambdaMessageProcessor(handler, payloadType));
1047+
serviceActivatingHandler = new ServiceActivatingHandler(new LambdaMessageProcessor(handler, expectedType));
10481048
}
1049-
else if (payloadType != null) {
1050-
return handle(payloadType, handler::handle, endpointConfigurer);
1049+
else if (expectedType != null) {
1050+
return handle(expectedType, handler::handle, endpointConfigurer);
10511051
}
10521052
else {
10531053
serviceActivatingHandler = new ServiceActivatingHandler(handler, ClassUtils.HANDLER_HANDLE_METHOD);
@@ -1488,7 +1488,7 @@ public B split(MessageProcessorSpec<?> messageProcessorSpec,
14881488
/**
14891489
* Populate the {@link MethodInvokingSplitter} to evaluate the provided
14901490
* {@link Function} at runtime.
1491-
* Typically used with a Java 8 Lambda expression:
1491+
* Typically, used with a Java 8 Lambda expression:
14921492
* <pre class="code">
14931493
* {@code
14941494
* .split(String.class, p ->
@@ -1499,23 +1499,23 @@ public B split(MessageProcessorSpec<?> messageProcessorSpec,
14991499
* new Foo(rs.getInt(1), rs.getString(2)))))
15001500
* }
15011501
* </pre>
1502-
* @param payloadType the {@link Class} for expected payload type. It can also be
1502+
* @param expectedType the {@link Class} for expected payload type. It can also be
15031503
* {@code Message.class} if you wish to access the entire message in the splitter.
15041504
* Conversion to this type will be attempted, if necessary.
15051505
* @param splitter the splitter {@link Function}.
15061506
* @param <P> the payload type or {@code Message.class}.
15071507
* @return the current {@link BaseIntegrationFlowDefinition}.
15081508
* @see LambdaMessageProcessor
15091509
*/
1510-
public <P> B split(Class<P> payloadType, Function<P, ?> splitter) {
1511-
return split(payloadType, splitter, null);
1510+
public <P> B split(Class<P> expectedType, Function<P, ?> splitter) {
1511+
return split(expectedType, splitter, null);
15121512
}
15131513

15141514
/**
15151515
* Populate the {@link MethodInvokingSplitter} to evaluate the provided
15161516
* {@link Function} at runtime.
1517-
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
1518-
* Typically used with a Java 8 Lambda expression:
1517+
* In addition, accept options for the integration endpoint using {@link GenericEndpointSpec}.
1518+
* Typically, used with a Java 8 Lambda expression:
15191519
* <pre class="code">
15201520
* {@code
15211521
* .split(String.class, p ->
@@ -1527,7 +1527,7 @@ public <P> B split(Class<P> payloadType, Function<P, ?> splitter) {
15271527
* , e -> e.applySequence(false))
15281528
* }
15291529
* </pre>
1530-
* @param payloadType the {@link Class} for expected payload type. It can also be
1530+
* @param expectedType the {@link Class} for expected payload type. It can also be
15311531
* {@code Message.class} if you wish to access the entire message in the splitter.
15321532
* Conversion to this type will be attempted, if necessary.
15331533
* @param splitter the splitter {@link Function}.
@@ -1537,12 +1537,12 @@ public <P> B split(Class<P> payloadType, Function<P, ?> splitter) {
15371537
* @see LambdaMessageProcessor
15381538
* @see SplitterEndpointSpec
15391539
*/
1540-
public <P> B split(Class<P> payloadType, Function<P, ?> splitter,
1540+
public <P> B split(Class<P> expectedType, Function<P, ?> splitter,
15411541
Consumer<SplitterEndpointSpec<MethodInvokingSplitter>> endpointConfigurer) {
15421542

15431543
MethodInvokingSplitter split =
15441544
ClassUtils.isLambda(splitter.getClass())
1545-
? new MethodInvokingSplitter(new LambdaMessageProcessor(splitter, payloadType))
1545+
? new MethodInvokingSplitter(new LambdaMessageProcessor(splitter, expectedType))
15461546
: new MethodInvokingSplitter(splitter, ClassUtils.FUNCTION_APPLY_METHOD);
15471547
return split(split, endpointConfigurer);
15481548
}
@@ -1880,24 +1880,24 @@ public <T> B route(String expression, Consumer<RouterSpec<T, ExpressionEvaluatin
18801880
* .route(Integer.class, p -> p % 2 == 0)
18811881
* }
18821882
* </pre>
1883-
* @param payloadType the {@link Class} for expected payload type. It can also be
1884-
* {@code Message.class} if you wish to access the entire message in the splitter.
1883+
* @param expectedType the {@link Class} for expected payload type. It can also be
1884+
* {@code Message.class} if you wish to access the entire message in the router.
18851885
* Conversion to this type will be attempted, if necessary.
18861886
* @param router the {@link Function} to use.
18871887
* @param <S> the source payload type or {@code Message.class}.
18881888
* @param <T> the target result type.
18891889
* @return the current {@link BaseIntegrationFlowDefinition}.
18901890
* @see LambdaMessageProcessor
18911891
*/
1892-
public <S, T> B route(Class<S> payloadType, Function<S, T> router) {
1893-
return route(payloadType, router, null);
1892+
public <S, T> B route(Class<S> expectedType, Function<S, T> router) {
1893+
return route(expectedType, router, null);
18941894
}
18951895

18961896
/**
18971897
* Populate the {@link MethodInvokingRouter} for provided {@link Function}
18981898
* and payload type and options from {@link RouterSpec}.
1899-
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
1900-
* Typically used with a Java 8 Lambda expression:
1899+
* In addition, accept options for the integration endpoint using {@link GenericEndpointSpec}.
1900+
* Typically, used with a Java 8 Lambda expression:
19011901
* <pre class="code">
19021902
* {@code
19031903
* .route(Integer.class, p -> p % 2 == 0,
@@ -1907,8 +1907,8 @@ public <S, T> B route(Class<S> payloadType, Function<S, T> router) {
19071907
* .applySequence(false))
19081908
* }
19091909
* </pre>
1910-
* @param payloadType the {@link Class} for expected payload type. It can also be
1911-
* {@code Message.class} if you wish to access the entire message in the splitter.
1910+
* @param expectedType the {@link Class} for expected payload type. It can also be
1911+
* {@code Message.class} if you wish to access the entire message in the router.
19121912
* Conversion to this type will be attempted, if necessary.
19131913
* @param router the {@link Function} to use.
19141914
* @param routerConfigurer the {@link Consumer} to provide {@link MethodInvokingRouter} options.
@@ -1917,12 +1917,12 @@ public <S, T> B route(Class<S> payloadType, Function<S, T> router) {
19171917
* @return the current {@link BaseIntegrationFlowDefinition}.
19181918
* @see LambdaMessageProcessor
19191919
*/
1920-
public <P, T> B route(Class<P> payloadType, Function<P, T> router,
1920+
public <P, T> B route(Class<P> expectedType, Function<P, T> router,
19211921
Consumer<RouterSpec<T, MethodInvokingRouter>> routerConfigurer) {
19221922

19231923
MethodInvokingRouter methodInvokingRouter =
19241924
ClassUtils.isLambda(router.getClass())
1925-
? new MethodInvokingRouter(new LambdaMessageProcessor(router, payloadType))
1925+
? new MethodInvokingRouter(new LambdaMessageProcessor(router, expectedType))
19261926
: new MethodInvokingRouter(router, ClassUtils.FUNCTION_APPLY_METHOD);
19271927
return route(new RouterSpec<>(methodInvokingRouter), routerConfigurer);
19281928
}

Diff for: spring-integration-core/src/main/java/org/springframework/integration/handler/LambdaMessageProcessor.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.BeanFactoryAware;
3131
import org.springframework.core.MethodIntrospector;
3232
import org.springframework.integration.context.IntegrationContextUtils;
33+
import org.springframework.lang.Nullable;
3334
import org.springframework.messaging.Message;
3435
import org.springframework.messaging.converter.MessageConverter;
3536
import org.springframework.util.Assert;
@@ -53,13 +54,19 @@ public class LambdaMessageProcessor implements MessageProcessor<Object>, BeanFac
5354

5455
private final Method method;
5556

56-
private final Class<?> payloadType;
57+
@Nullable
58+
private final Class<?> expectedType;
5759

5860
private final Class<?>[] parameterTypes;
5961

6062
private MessageConverter messageConverter;
6163

62-
public LambdaMessageProcessor(Object target, Class<?> payloadType) {
64+
/**
65+
* Create a processor to evaluate a provided lambda at runtime against a request message.
66+
* @param target the lambda object.
67+
* @param expectedType an optional expected type for method argument conversion.
68+
*/
69+
public LambdaMessageProcessor(Object target, @Nullable Class<?> expectedType) {
6370
Assert.notNull(target, "'target' must not be null");
6471
this.target = target;
6572

@@ -79,7 +86,7 @@ public LambdaMessageProcessor(Object target, Class<?> payloadType) {
7986
this.method = methods.iterator().next();
8087
this.method.setAccessible(true);
8188
this.parameterTypes = this.method.getParameterTypes();
82-
this.payloadType = payloadType;
89+
this.expectedType = expectedType;
8390
}
8491

8592
@Override
@@ -138,13 +145,13 @@ else if (Map.class.isAssignableFrom(parameterType)) {
138145
}
139146
}
140147
else {
141-
if (this.payloadType != null &&
142-
!ClassUtils.isAssignable(this.payloadType, message.getPayload().getClass())) {
143-
if (Message.class.isAssignableFrom(this.payloadType)) {
148+
if (this.expectedType != null &&
149+
!ClassUtils.isAssignable(this.expectedType, message.getPayload().getClass())) {
150+
if (Message.class.isAssignableFrom(this.expectedType)) {
144151
args[i] = message;
145152
}
146153
else {
147-
args[i] = this.messageConverter.fromMessage(message, this.payloadType);
154+
args[i] = this.messageConverter.fromMessage(message, this.expectedType);
148155
}
149156
}
150157
else {

Diff for: src/reference/asciidoc/router.adoc

+4-3
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,10 @@ Second, you can define the routing function within the DSL flow itself, as the f
580580
@Bean
581581
public IntegrationFlow routerFlow2() {
582582
return IntegrationFlows.from("routingChannel")
583-
.<Message<?>, String>route(m -> m.getHeaders().get("testHeader", String.class), m -> m
584-
.channelMapping("someHeaderValue", "channelA")
585-
.channelMapping("someOtherHeaderValue", "channelB"),
583+
.route(Message.class, m -> m.getHeaders().get("testHeader", String.class),
584+
m -> m
585+
.channelMapping("someHeaderValue", "channelA")
586+
.channelMapping("someOtherHeaderValue", "channelB"),
586587
e -> e.id("headerValueRouter"))
587588
.get();
588589
}

0 commit comments

Comments
 (0)