-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
IntegrationFlowDefinition.java
2837 lines (2621 loc) · 117 KB
/
IntegrationFlowDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2018 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
*
* http://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.dsl;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.aggregator.AggregatingMessageHandler;
import org.springframework.integration.aggregator.BarrierMessageHandler;
import org.springframework.integration.channel.ChannelInterceptorAware;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.FixedSubscriberChannel;
import org.springframework.integration.channel.FluxMessageChannel;
import org.springframework.integration.channel.MessageChannelReactiveUtils;
import org.springframework.integration.channel.interceptor.WireTap;
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
import org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.core.GenericSelector;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.dsl.channel.MessageChannelSpec;
import org.springframework.integration.dsl.channel.WireTapSpec;
import org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype;
import org.springframework.integration.dsl.support.MessageChannelReference;
import org.springframework.integration.expression.ControlBusMethodFilter;
import org.springframework.integration.expression.FunctionExpression;
import org.springframework.integration.filter.ExpressionEvaluatingSelector;
import org.springframework.integration.filter.MessageFilter;
import org.springframework.integration.filter.MethodInvokingSelector;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.handler.BeanNameMessageProcessor;
import org.springframework.integration.handler.BridgeHandler;
import org.springframework.integration.handler.DelayHandler;
import org.springframework.integration.handler.ExpressionCommandMessageProcessor;
import org.springframework.integration.handler.GenericHandler;
import org.springframework.integration.handler.LambdaMessageProcessor;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.handler.MessageTriggerAction;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.router.AbstractMessageRouter;
import org.springframework.integration.router.ErrorMessageExceptionTypeRouter;
import org.springframework.integration.router.ExpressionEvaluatingRouter;
import org.springframework.integration.router.MethodInvokingRouter;
import org.springframework.integration.router.RecipientListRouter;
import org.springframework.integration.scattergather.ScatterGatherHandler;
import org.springframework.integration.splitter.AbstractMessageSplitter;
import org.springframework.integration.splitter.DefaultMessageSplitter;
import org.springframework.integration.splitter.ExpressionEvaluatingSplitter;
import org.springframework.integration.splitter.MethodInvokingSplitter;
import org.springframework.integration.store.MessageStore;
import org.springframework.integration.support.MapBuilder;
import org.springframework.integration.transformer.ClaimCheckInTransformer;
import org.springframework.integration.transformer.ClaimCheckOutTransformer;
import org.springframework.integration.transformer.ContentEnricher;
import org.springframework.integration.transformer.ExpressionEvaluatingTransformer;
import org.springframework.integration.transformer.GenericTransformer;
import org.springframework.integration.transformer.HeaderFilter;
import org.springframework.integration.transformer.MessageTransformingHandler;
import org.springframework.integration.transformer.MethodInvokingTransformer;
import org.springframework.integration.transformer.Transformer;
import org.springframework.integration.util.ClassUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import reactor.util.function.Tuple2;
/**
* The {@code Builder} pattern implementation for the EIP-method chain.
* Provides a variety of methods to populate Spring Integration components
* to an {@link IntegrationFlow} for the future registration in the
* application context.
*
* @param <B> the {@link IntegrationFlowDefinition} implementation type.
*
* @author Artem Bilan
* @author Gary Russell
* @author Gabriele Del Prete
*
* @since 5.0
*
* @see org.springframework.integration.config.dsl.IntegrationFlowBeanPostProcessor
*/
public abstract class IntegrationFlowDefinition<B extends IntegrationFlowDefinition<B>> {
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
private static final Set<MessageProducer> REFERENCED_REPLY_PRODUCERS = new HashSet<>();
protected final Map<Object, String> integrationComponents = new LinkedHashMap<>();
private MessageChannel currentMessageChannel;
private Object currentComponent;
private StandardIntegrationFlow integrationFlow;
private boolean implicitChannel;
IntegrationFlowDefinition() {
}
B addComponent(Object component) {
this.integrationComponents.put(component, null);
return _this();
}
B addComponents(Map<Object, String> components) {
if (components != null) {
this.integrationComponents.putAll(components);
}
return _this();
}
B currentComponent(Object component) {
this.currentComponent = component;
return _this();
}
/**
* Populate an {@link org.springframework.integration.channel.FixedSubscriberChannel} instance
* at the current {@link IntegrationFlow} chain position.
* The 'bean name' will be generated during the bean registration phase.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B fixedSubscriberChannel() {
return fixedSubscriberChannel(null);
}
/**
* Populate an {@link org.springframework.integration.channel.FixedSubscriberChannel} instance
* at the current {@link IntegrationFlow} chain position.
* The provided {@code messageChannelName} is used for the bean registration.
* @param messageChannelName the bean name to use.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B fixedSubscriberChannel(String messageChannelName) {
return channel(new FixedSubscriberChannelPrototype(messageChannelName));
}
/**
* Populate a {@link MessageChannelReference} instance
* at the current {@link IntegrationFlow} chain position.
* The provided {@code messageChannelName} is used for the bean registration
* ({@link org.springframework.integration.channel.DirectChannel}), if there is no such a bean
* in the application context. Otherwise the existing {@link MessageChannel} bean is used
* to wire integration endpoints.
* @param messageChannelName the bean name to use.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B channel(String messageChannelName) {
return channel(new MessageChannelReference(messageChannelName));
}
/**
* Populate a {@link MessageChannel} instance
* at the current {@link IntegrationFlow} chain position using the {@link MessageChannelSpec}
* fluent API.
* @param messageChannelSpec the {@link MessageChannelSpec} to use.
* @return the current {@link IntegrationFlowDefinition}.
* @see org.springframework.integration.dsl.channel.MessageChannels
*/
public B channel(MessageChannelSpec<?, ?> messageChannelSpec) {
Assert.notNull(messageChannelSpec, "'messageChannelSpec' must not be null");
return channel(messageChannelSpec.get());
}
/**
* Populate the provided {@link MessageChannel} instance
* at the current {@link IntegrationFlow} chain position.
* The {@code messageChannel} can be an existing bean, or fresh instance, in which case
* the {@link org.springframework.integration.config.dsl.IntegrationFlowBeanPostProcessor}
* will populate it as a bean with a generated name.
* @param messageChannel the {@link MessageChannel} to populate.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B channel(MessageChannel messageChannel) {
Assert.notNull(messageChannel, "'messageChannel' must not be null");
if (this.currentMessageChannel != null) {
bridge();
}
this.currentMessageChannel = messageChannel;
return registerOutputChannelIfCan(this.currentMessageChannel);
}
/**
* Populate a {@link MessageChannel} instance
* at the current {@link IntegrationFlow} chain position using the {@link Channels}
* factory fluent API.
* @param channels the {@link Function} to use.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B channel(Function<Channels, MessageChannelSpec<?, ?>> channels) {
Assert.notNull(channels, "'channels' must not be null");
return channel(channels.apply(new Channels()));
}
/**
* The {@link org.springframework.integration.channel.PublishSubscribeChannel} {@link #channel}
* method specific implementation to allow the use of the 'subflow' subscriber capability.
* @param publishSubscribeChannelConfigurer the {@link Consumer} to specify
* {@link PublishSubscribeSpec} options including 'subflow' definition.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B publishSubscribeChannel(Consumer<PublishSubscribeSpec> publishSubscribeChannelConfigurer) {
return publishSubscribeChannel(null, publishSubscribeChannelConfigurer);
}
/**
* The {@link org.springframework.integration.channel.PublishSubscribeChannel} {@link #channel}
* method specific implementation to allow the use of the 'subflow' subscriber capability.
* Use the provided {@link Executor} for the target subscribers.
* @param executor the {@link Executor} to use.
* @param publishSubscribeChannelConfigurer the {@link Consumer} to specify
* {@link PublishSubscribeSpec} options including 'subflow' definition.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B publishSubscribeChannel(Executor executor,
Consumer<PublishSubscribeSpec> publishSubscribeChannelConfigurer) {
Assert.notNull(publishSubscribeChannelConfigurer, "'publishSubscribeChannelConfigurer' must not be null");
PublishSubscribeSpec spec = new PublishSubscribeSpec(executor);
publishSubscribeChannelConfigurer.accept(spec);
return addComponents(spec.getComponentsToRegister()).channel(spec);
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* .filter("World"::equals)
* .wireTap(sf -> sf.<String, String>transform(String::toUpperCase))
* .handle(p -> process(p))
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param flow the {@link IntegrationFlow} for wire-tap subflow as an alternative to the {@code wireTapChannel}.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(IntegrationFlow flow) {
return wireTap(flow, null);
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* f -> f.wireTap("tapChannel")
* .handle(p -> process(p))
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param wireTapChannel the {@link MessageChannel} bean name to wire-tap.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(String wireTapChannel) {
return wireTap(wireTapChannel, null);
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* .transform("payload")
* .wireTap(tapChannel())
* .channel("foo")
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param wireTapChannel the {@link MessageChannel} to wire-tap.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(MessageChannel wireTapChannel) {
return wireTap(wireTapChannel, null);
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* .transform("payload")
* .wireTap(sf -> sf.<String, String>transform(String::toUpperCase), wt -> wt.selector("payload == 'foo'"))
* .channel("foo")
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param flow the {@link IntegrationFlow} for wire-tap subflow as an alternative to the {@code wireTapChannel}.
* @param wireTapConfigurer the {@link Consumer} to accept options for the {@link WireTap}.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(IntegrationFlow flow, Consumer<WireTapSpec> wireTapConfigurer) {
MessageChannel wireTapChannel = obtainInputChannelFromFlow(flow);
return wireTap(wireTapChannel, wireTapConfigurer);
}
private MessageChannel obtainInputChannelFromFlow(IntegrationFlow flow) {
Assert.notNull(flow, "'flow' must not be null");
MessageChannel messageChannel = flow.getInputChannel();
if (messageChannel == null) {
messageChannel = new DirectChannel();
IntegrationFlowDefinition<?> flowBuilder = IntegrationFlows.from(messageChannel);
flow.configure(flowBuilder);
addComponent(flowBuilder.get());
}
else {
addComponent(flow);
}
return messageChannel;
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* .transform("payload")
* .wireTap("tapChannel", wt -> wt.selector(m -> m.getPayload().equals("foo")))
* .channel("foo")
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param wireTapChannel the {@link MessageChannel} bean name to wire-tap.
* @param wireTapConfigurer the {@link Consumer} to accept options for the {@link WireTap}.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(String wireTapChannel, Consumer<WireTapSpec> wireTapConfigurer) {
DirectChannel internalWireTapChannel = new DirectChannel();
addComponent(IntegrationFlows.from(internalWireTapChannel).channel(wireTapChannel).get());
return wireTap(internalWireTapChannel, wireTapConfigurer);
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* .transform("payload")
* .wireTap(tapChannel(), wt -> wt.selector(m -> m.getPayload().equals("foo")))
* .channel("foo")
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param wireTapChannel the {@link MessageChannel} to wire-tap.
* @param wireTapConfigurer the {@link Consumer} to accept options for the {@link WireTap}.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(MessageChannel wireTapChannel, Consumer<WireTapSpec> wireTapConfigurer) {
WireTapSpec wireTapSpec = new WireTapSpec(wireTapChannel);
if (wireTapConfigurer != null) {
wireTapConfigurer.accept(wireTapSpec);
}
addComponent(wireTapChannel);
return wireTap(wireTapSpec);
}
/**
* Populate the {@code Wire Tap} EI Pattern specific
* {@link org.springframework.messaging.support.ChannelInterceptor} implementation
* to the current {@link #currentMessageChannel}.
* <p> It is useful when an implicit {@link MessageChannel} is used between endpoints:
* <pre class="code">
* {@code
* .transform("payload")
* .wireTap(new WireTap(tapChannel().selector(m -> m.getPayload().equals("foo")))
* .channel("foo")
* }
* </pre>
* This method can be used after any {@link #channel} for explicit {@link MessageChannel},
* but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
* @param wireTapSpec the {@link WireTapSpec} to use.
* <p> When this EIP-method is used in the end of flow, it appends {@code nullChannel} to terminate flow properly,
* Otherwise {@code Dispatcher has no subscribers} exception is thrown for implicit {@link DirectChannel}.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B wireTap(WireTapSpec wireTapSpec) {
WireTap interceptor = wireTapSpec.get();
if (this.currentMessageChannel == null || !(this.currentMessageChannel instanceof ChannelInterceptorAware)) {
this.implicitChannel = true;
channel(new DirectChannel());
}
addComponent(wireTapSpec);
((ChannelInterceptorAware) this.currentMessageChannel).addInterceptor(interceptor);
return _this();
}
/**
* Populate the {@code Control Bus} EI Pattern specific {@link MessageHandler} implementation
* at the current {@link IntegrationFlow} chain position.
* @return the current {@link IntegrationFlowDefinition}.
* @see ExpressionCommandMessageProcessor
*/
public B controlBus() {
return controlBus(null);
}
/**
* Populate the {@code Control Bus} EI Pattern specific {@link MessageHandler} implementation
* at the current {@link IntegrationFlow} chain position.
* @param endpointConfigurer the {@link Consumer} to accept integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
* @see ExpressionCommandMessageProcessor
* @see GenericEndpointSpec
*/
public B controlBus(Consumer<GenericEndpointSpec<ServiceActivatingHandler>> endpointConfigurer) {
return handle(new ServiceActivatingHandler(new ExpressionCommandMessageProcessor(
new ControlBusMethodFilter())), endpointConfigurer);
}
/**
* Populate the {@code Transformer} EI Pattern specific {@link MessageHandler} implementation
* for the SpEL {@link Expression}.
* @param expression the {@code Transformer} {@link Expression}.
* @return the current {@link IntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
*/
public B transform(String expression) {
return transform(expression, (Consumer<GenericEndpointSpec<MessageTransformingHandler>>) null);
}
/**
* Populate the {@code Transformer} EI Pattern specific {@link MessageHandler} implementation
* for the SpEL {@link Expression}.
* @param expression the {@code Transformer} {@link Expression}.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
*/
public B transform(String expression,
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
Assert.hasText(expression, "'expression' must not be empty");
return transform(new ExpressionEvaluatingTransformer(PARSER.parseExpression(expression)),
endpointConfigurer);
}
/**
* Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
* to invoke the discovered service method at runtime.
* @param service the service to use.
* @return the current {@link IntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
*/
public B transform(Object service) {
return transform(service, null);
}
/**
* Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
* to invoke the service method at runtime.
* @param service the service to use.
* @param methodName the method to invoke.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
*/
public B transform(Object service, String methodName) {
return transform(service, methodName, null);
}
/**
* Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
* to invoke the service method at runtime.
* @param service the service to use.
* @param methodName the method to invoke.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
*/
public B transform(Object service, String methodName,
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
MethodInvokingTransformer transformer;
if (StringUtils.hasText(methodName)) {
transformer = new MethodInvokingTransformer(service, methodName);
}
else {
transformer = new MethodInvokingTransformer(service);
}
return transform(transformer, endpointConfigurer);
}
/**
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer}. Use {@link #transform(Class, GenericTransformer)} if
* you need to access the entire message.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param <S> the source type - 'transform from'.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
* @see LambdaMessageProcessor
*/
public <S, T> B transform(GenericTransformer<S, T> genericTransformer) {
return this.transform(null, genericTransformer);
}
/**
* Populate the {@link MessageTransformingHandler} instance for the
* {@link org.springframework.integration.handler.MessageProcessor} from provided {@link MessageProcessorSpec}.
* <pre class="code">
* {@code
* .transform(Scripts.script("classpath:myScript.py").variable("foo", bar()))
* }
* </pre>
* @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
*/
public B transform(MessageProcessorSpec<?> messageProcessorSpec) {
return transform(messageProcessorSpec, (Consumer<GenericEndpointSpec<MessageTransformingHandler>>) null);
}
/**
* Populate the {@link MessageTransformingHandler} instance for the
* {@link org.springframework.integration.handler.MessageProcessor} from provided {@link MessageProcessorSpec}.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* <pre class="code">
* {@code
* .transform(Scripts.script("classpath:myScript.py").variable("foo", bar()),
* e -> e.autoStartup(false))
* }
* </pre>
* @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
*/
public B transform(MessageProcessorSpec<?> messageProcessorSpec,
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
Assert.notNull(messageProcessorSpec, "'messageProcessorSpec' must not be null");
MessageProcessor<?> processor = messageProcessorSpec.get();
return addComponent(processor)
.transform(new MethodInvokingTransformer(processor), endpointConfigurer);
}
/**
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer} for the specific {@code payloadType} to convert at
* runtime.
* Use {@link #transform(Class, GenericTransformer)} if you need access to the
* entire message.
* @param payloadType the {@link Class} for expected payload type. It can also be
* {@code Message.class} if you wish to access the entire message in the transformer.
* Conversion to this type will be attempted, if necessary.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param <P> the payload type - 'transform from' or {@code Message.class}.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
* @see LambdaMessageProcessor
*/
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer) {
return this.transform(payloadType, genericTransformer, null);
}
/**
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer}. In addition accept options for the integration endpoint
* using {@link GenericEndpointSpec}. Use
* {@link #transform(Class, GenericTransformer, Consumer)} if you need to access the
* entire message.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint
* options.
* @param <S> the source type - 'transform from'.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
* @see LambdaMessageProcessor
* @see GenericEndpointSpec
*/
public <S, T> B transform(GenericTransformer<S, T> genericTransformer,
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
return transform(null, genericTransformer, endpointConfigurer);
}
/**
* Populate the {@link MessageTransformingHandler} instance for the provided {@link GenericTransformer}
* for the specific {@code payloadType} to convert at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* @param payloadType the {@link Class} for expected payload type. It can also be
* {@code Message.class} if you wish to access the entire message in the transformer.
* Conversion to this type will be attempted, if necessary.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @param <P> the payload type - 'transform from', or {@code Message.class}.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingTransformer
* @see LambdaMessageProcessor
* @see GenericEndpointSpec
*/
public <P, T> B transform(Class<P> payloadType, GenericTransformer<P, T> genericTransformer,
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
Assert.notNull(genericTransformer, "'genericTransformer' must not be null");
Transformer transformer = genericTransformer instanceof Transformer ? (Transformer) genericTransformer :
(isLambda(genericTransformer)
? new MethodInvokingTransformer(new LambdaMessageProcessor(genericTransformer, payloadType))
: new MethodInvokingTransformer(genericTransformer, ClassUtils.TRANSFORMER_TRANSFORM_METHOD));
return addComponent(transformer)
.handle(new MessageTransformingHandler(transformer), endpointConfigurer);
}
/**
* Populate a {@link MessageFilter} with {@link MessageSelector} for the provided SpEL expression.
* @param expression the SpEL expression.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B filter(String expression) {
return filter(expression, (Consumer<FilterEndpointSpec>) null);
}
/**
* Populate a {@link MessageFilter} with {@link MessageSelector} for the provided SpEL expression.
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}:
* <pre class="code">
* {@code
* .filter("payload.hot"), e -> e.autoStartup(false))
* }
* </pre>
* @param expression the SpEL expression.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
* @see FilterEndpointSpec
*/
public B filter(String expression, Consumer<FilterEndpointSpec> endpointConfigurer) {
Assert.hasText(expression, "'expression' must not be empty");
return filter(new ExpressionEvaluatingSelector(expression), endpointConfigurer);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector} for the
* discovered method of the provided service.
* @param service the service to use.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingSelector
*/
public B filter(Object service) {
return filter(service, null);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector} for the
* method of the provided service.
* @param service the service to use.
* @param methodName the method to invoke
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingSelector
*/
public B filter(Object service, String methodName) {
return filter(service, methodName, null);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector} for the
* method of the provided service.
* @param service the service to use.
* @param methodName the method to invoke
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
* @see MethodInvokingSelector
*/
public B filter(Object service, String methodName, Consumer<FilterEndpointSpec> endpointConfigurer) {
MethodInvokingSelector selector;
if (StringUtils.hasText(methodName)) {
selector = new MethodInvokingSelector(service, methodName);
}
else {
selector = new MethodInvokingSelector(service);
}
return filter(selector, endpointConfigurer);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the provided {@link GenericSelector}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .filter("World"::equals)
* }
* </pre>
* Use {@link #filter(Class, GenericSelector)} if you need to access the entire
* message.
* @param genericSelector the {@link GenericSelector} to use.
* @param <P> the source payload type.
* @return the current {@link IntegrationFlowDefinition}.
*/
public <P> B filter(GenericSelector<P> genericSelector) {
return filter(null, genericSelector);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the {@link org.springframework.integration.handler.MessageProcessor} from
* the provided {@link MessageProcessorSpec}.
* <pre class="code">
* {@code
* .filter(Scripts.script(scriptResource).lang("ruby"))
* }
* </pre>
* @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B filter(MessageProcessorSpec<?> messageProcessorSpec) {
return filter(messageProcessorSpec, (Consumer<FilterEndpointSpec>) null);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the {@link org.springframework.integration.handler.MessageProcessor} from
* the provided {@link MessageProcessorSpec}.
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
* <pre class="code">
* {@code
* .filter(Scripts.script(scriptResource).lang("ruby"),
* e -> e.autoStartup(false))
* }
* </pre>
* @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B filter(MessageProcessorSpec<?> messageProcessorSpec, Consumer<FilterEndpointSpec> endpointConfigurer) {
Assert.notNull(messageProcessorSpec, "'messageProcessorSpec' must not be null");
MessageProcessor<?> processor = messageProcessorSpec.get();
return addComponent(processor)
.filter(new MethodInvokingSelector(processor), endpointConfigurer);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the provided {@link GenericSelector}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .filter(Date.class, p -> p.after(new Date()))
* }
* </pre>
* @param payloadType the {@link Class} for expected payload type. It can also be
* {@code Message.class} if you wish to access the entire message in the selector.
* Conversion to this type will be attempted, if necessary.
* @param genericSelector the {@link GenericSelector} to use.
* @param <P> the source payload type or {@code Message.class}.
* @return the current {@link IntegrationFlowDefinition}.
* @see LambdaMessageProcessor
*/
public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector) {
return filter(payloadType, genericSelector, null);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the provided {@link GenericSelector}.
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .filter("World"::equals, e -> e.autoStartup(false))
* }
* </pre>
* Use {@link #filter(Class, GenericSelector, Consumer)} if you need to access the entire
* message.
* @param genericSelector the {@link GenericSelector} to use.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @param <P> the source payload type.
* @return the current {@link IntegrationFlowDefinition}.
* @see FilterEndpointSpec
*/
public <P> B filter(GenericSelector<P> genericSelector, Consumer<FilterEndpointSpec> endpointConfigurer) {
return filter(null, genericSelector, endpointConfigurer);
}
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the provided {@link GenericSelector}.
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .filter(Date.class, p -> p.after(new Date()), e -> e.autoStartup(false))
* }
* </pre>
* @param payloadType the {@link Class} for expected payload type. It can also be
* {@code Message.class} if you wish to access the entire message in the selector.
* Conversion to this type will be attempted, if necessary.
* @param genericSelector the {@link GenericSelector} to use.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @param <P> the source payload type or {@code Message.class}.
* @return the current {@link IntegrationFlowDefinition}.
* @see LambdaMessageProcessor
* @see FilterEndpointSpec
*/
public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector,
Consumer<FilterEndpointSpec> endpointConfigurer) {
Assert.notNull(genericSelector, "'genericSelector' must not be null");
MessageSelector selector = genericSelector instanceof MessageSelector ? (MessageSelector) genericSelector :
(isLambda(genericSelector)
? new MethodInvokingSelector(new LambdaMessageProcessor(genericSelector, payloadType))
: new MethodInvokingSelector(genericSelector, ClassUtils.SELECTOR_ACCEPT_METHOD));
return this.register(new FilterEndpointSpec(new MessageFilter(selector)), endpointConfigurer);
}
/**
* Populate a {@link ServiceActivatingHandler} for the selected protocol specific
* {@link MessageHandler} implementation from {@code Namespace Factory}:
* <pre class="code">
* {@code
* .handle(Amqp.outboundAdapter(this.amqpTemplate).routingKeyExpression("headers.routingKey"))
* }
* </pre>
* @param messageHandlerSpec the {@link MessageHandlerSpec} to configure protocol specific
* {@link MessageHandler}.
* @param <H> the target {@link MessageHandler} type.
* @return the current {@link IntegrationFlowDefinition}.
*/
public <H extends MessageHandler> B handle(MessageHandlerSpec<?, H> messageHandlerSpec) {
return handle(messageHandlerSpec, (Consumer<GenericEndpointSpec<H>>) null);
}
/**
* Populate a {@link ServiceActivatingHandler} for the provided
* {@link MessageHandler} implementation.
* Can be used as Java 8 Lambda expression:
* <pre class="code">
* {@code
* .handle(m -> logger.info(m.getPayload())
* }
* </pre>
* @param messageHandler the {@link MessageHandler} to use.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B handle(MessageHandler messageHandler) {
return handle(messageHandler, (Consumer<GenericEndpointSpec<MessageHandler>>) null);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
* to invoke the {@code method} for provided {@code bean} at runtime.
* @param beanName the bean name to use.
* @param methodName the method to invoke.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B handle(String beanName, String methodName) {
return handle(beanName, methodName, null);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
* to invoke the {@code method} for provided {@code bean} at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* @param beanName the bean name to use.
* @param methodName the method to invoke.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B handle(String beanName, String methodName,
Consumer<GenericEndpointSpec<ServiceActivatingHandler>> endpointConfigurer) {
return handle(new ServiceActivatingHandler(new BeanNameMessageProcessor<Object>(beanName, methodName)),
endpointConfigurer);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link MethodInvokingMessageProcessor}
* to invoke the discovered {@code method} for provided {@code service} at runtime.
* @param service the service object to use.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B handle(Object service) {
return handle(service, null);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link MethodInvokingMessageProcessor}
* to invoke the {@code method} for provided {@code bean} at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* @param service the service object to use.
* @param methodName the method to invoke.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B handle(Object service, String methodName) {
return handle(service, methodName, null);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link MethodInvokingMessageProcessor}
* to invoke the {@code method} for provided {@code bean} at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* @param service the service object to use.
* @param methodName the method to invoke.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link IntegrationFlowDefinition}.
*/
public B handle(Object service, String methodName,
Consumer<GenericEndpointSpec<ServiceActivatingHandler>> endpointConfigurer) {
ServiceActivatingHandler handler;
if (StringUtils.hasText(methodName)) {
handler = new ServiceActivatingHandler(service, methodName);
}
else {
handler = new ServiceActivatingHandler(service);
}
return handle(handler, endpointConfigurer);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
* to invoke the provided {@link GenericHandler} at runtime.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .<Integer>handle((p, h) -> p / 2)
* }
* </pre>
* Use {@link #handle(Class, GenericHandler)} if you need to access the entire
* message.
* @param handler the handler to invoke.
* @param <P> the payload type to expect.
* @return the current {@link IntegrationFlowDefinition}.
* @see LambdaMessageProcessor
*/
public <P> B handle(GenericHandler<P> handler) {
return handle(null, handler);
}
/**
* Populate a {@link ServiceActivatingHandler} for the
* {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
* to invoke the provided {@link GenericHandler} at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .<Integer>handle((p, h) -> p / 2, e -> e.autoStartup(false))
* }