11package com .baeldung .subflows .separateflows ;
22
3- import java .util .Arrays ;
43import java .util .Collection ;
54
6- import org .springframework .context .ConfigurableApplicationContext ;
7- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
85import org .springframework .context .annotation .Bean ;
96import org .springframework .integration .annotation .Gateway ;
107import org .springframework .integration .annotation .IntegrationComponentScan ;
118import org .springframework .integration .annotation .MessagingGateway ;
12- import org .springframework .integration .channel .DirectChannel ;
9+
10+ import org .springframework .integration .channel .QueueChannel ;
1311import org .springframework .integration .config .EnableIntegration ;
1412import org .springframework .integration .dsl .IntegrationFlow ;
1513
1816public class SeparateFlowsExample {
1917 @ MessagingGateway
2018 public interface NumbersClassifier {
21- @ Gateway (requestChannel = "multipleof3Flow .input" )
22- void multipleof3 (Collection <Integer > is );
19+ @ Gateway (requestChannel = "multipleOfThreeFlow .input" )
20+ void multipleofThree (Collection <Integer > numbers );
2321
24- @ Gateway (requestChannel = "remainderIs1Flow .input" )
25- void remainderIs1 (Collection <Integer > is );
22+ @ Gateway (requestChannel = "remainderIsOneFlow .input" )
23+ void remainderIsOne (Collection <Integer > numbers );
2624
27- @ Gateway (requestChannel = "remainderIs2Flow .input" )
28- void remainderIs2 (Collection <Integer > numbers );
25+ @ Gateway (requestChannel = "remainderIsTwoFlow .input" )
26+ void remainderIsTwo (Collection <Integer > numbers );
2927 }
30-
28+
3129 @ Bean
32- DirectChannel multipleof3Channel () {
33- return new DirectChannel ();
30+ QueueChannel multipleOfThreeChannel () {
31+ return new QueueChannel ();
3432 }
35-
33+
3634 @ Bean
37- DirectChannel remainderIs1Channel () {
38- return new DirectChannel ();
35+ QueueChannel remainderIsOneChannel () {
36+ return new QueueChannel ();
3937 }
40-
38+
4139 @ Bean
42- DirectChannel remainderIs2Channel () {
43- return new DirectChannel ();
40+ QueueChannel remainderIsTwoChannel () {
41+ return new QueueChannel ();
4442 }
4543
46- @ Bean
47- public IntegrationFlow multipleof3Flow () {
48- return f -> f .split ()
49- .<Integer > filter (p -> p % 3 == 0 )
50- .channel ("multipleof3Channel" );
44+ boolean isMultipleOfThree (Integer number ) {
45+ return number % 3 == 0 ;
46+ }
47+
48+ boolean isRemainderOne (Integer number ) {
49+ return number % 3 == 1 ;
50+ }
51+
52+ boolean isRemainderTwo (Integer number ) {
53+ return number % 3 == 2 ;
5154 }
5255
5356 @ Bean
54- public IntegrationFlow remainderIs1Flow () {
55- return f -> f .split ()
56- .<Integer > filter (p -> p % 3 == 1 )
57- .channel ("remainderIs1Channel " );
57+ public IntegrationFlow multipleOfThreeFlow () {
58+ return flow -> flow .split ()
59+ .<Integer > filter (this :: isMultipleOfThree )
60+ .channel ("multipleOfThreeChannel " );
5861 }
5962
6063 @ Bean
61- public IntegrationFlow remainderIs2Flow () {
62- return f -> f .split ()
63- .<Integer > filter (p -> p % 3 == 2 )
64- .channel ("remainderIs2Channel " );
64+ public IntegrationFlow remainderIsOneFlow () {
65+ return flow -> flow .split ()
66+ .<Integer > filter (this :: isRemainderOne )
67+ .channel ("remainderIsOneChannel " );
6568 }
6669
67- public static void main (String [] args ) {
68- final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (SeparateFlowsExample .class );
69- DirectChannel multipleof3Channel = ctx .getBean ("multipleof3Channel" , DirectChannel .class );
70- multipleof3Channel .subscribe (x -> System .out .println ("multipleof3Channel: " + x ));
71- DirectChannel remainderIs1Channel = ctx .getBean ("remainderIs1Channel" , DirectChannel .class );
72- remainderIs1Channel .subscribe (x -> System .out .println ("remainderIs1Channel: " + x ));
73- DirectChannel remainderIs2Channel = ctx .getBean ("remainderIs2Channel" , DirectChannel .class );
74- remainderIs2Channel .subscribe (x -> System .out .println ("remainderIs2Channel: " + x ));
75- ctx .getBean (NumbersClassifier .class )
76- .multipleof3 (Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 ));
77- ctx .getBean (NumbersClassifier .class )
78- .remainderIs1 (Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 ));
79- ctx .getBean (NumbersClassifier .class )
80- .remainderIs2 (Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 ));
81- ctx .close ();
70+ @ Bean
71+ public IntegrationFlow remainderIsTwoFlow () {
72+ return flow -> flow .split ()
73+ .<Integer > filter (this ::isRemainderTwo )
74+ .channel ("remainderIsTwoChannel" );
8275 }
76+
8377}
0 commit comments