Skip to content

Commit fcca315

Browse files
myluckagainjzheaux
authored andcommitted
Spring Integration Subflows
Issue: BAEL-2276
1 parent 5145b90 commit fcca315

11 files changed

Lines changed: 514 additions & 202 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.subflows.discardflow;
2+
3+
import java.util.Collection;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.integration.annotation.Gateway;
6+
import org.springframework.integration.annotation.IntegrationComponentScan;
7+
import org.springframework.integration.annotation.MessagingGateway;
8+
import org.springframework.integration.channel.QueueChannel;
9+
import org.springframework.integration.config.EnableIntegration;
10+
import org.springframework.integration.dsl.IntegrationFlow;
11+
12+
@EnableIntegration
13+
@IntegrationComponentScan
14+
public class FilterExample {
15+
@MessagingGateway
16+
public interface NumbersClassifier {
17+
@Gateway(requestChannel = "classify.input")
18+
void classify(Collection<Integer> numbers);
19+
}
20+
21+
@Bean
22+
QueueChannel multipleofThreeChannel() {
23+
return new QueueChannel();
24+
}
25+
26+
@Bean
27+
QueueChannel remainderIsOneChannel() {
28+
return new QueueChannel();
29+
}
30+
31+
@Bean
32+
QueueChannel remainderIsTwoChannel() {
33+
return new QueueChannel();
34+
}
35+
boolean isMultipleOfThree(Integer number) {
36+
return number % 3 == 0;
37+
}
38+
39+
boolean isRemainderOne(Integer number) {
40+
return number % 3 == 1;
41+
}
42+
43+
boolean isRemainderTwo(Integer number) {
44+
return number % 3 == 2;
45+
}
46+
@Bean
47+
public IntegrationFlow classify() {
48+
return flow -> flow.split()
49+
.<Integer> filter(this::isMultipleOfThree, notMultiple -> notMultiple
50+
.discardFlow(oneflow -> oneflow
51+
.<Integer> filter(this::isRemainderOne,
52+
twoflow -> twoflow .discardChannel("remainderIsTwoChannel"))
53+
.channel("remainderIsOneChannel")))
54+
.channel("multipleofThreeChannel");
55+
}
56+
57+
}
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.baeldung.subflows.publishsubscribechannel;
22

3-
import java.util.Arrays;
43
import java.util.Collection;
54

6-
import org.springframework.context.ConfigurableApplicationContext;
7-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
85
import org.springframework.context.annotation.Bean;
96
import org.springframework.integration.annotation.Gateway;
107
import org.springframework.integration.annotation.IntegrationComponentScan;
118
import org.springframework.integration.annotation.MessagingGateway;
12-
import org.springframework.integration.channel.DirectChannel;
9+
10+
import org.springframework.integration.channel.QueueChannel;
1311
import org.springframework.integration.config.EnableIntegration;
1412
import org.springframework.integration.dsl.IntegrationFlow;
1513

@@ -18,46 +16,45 @@
1816
public class PublishSubscibeChannelExample {
1917
@MessagingGateway
2018
public interface NumbersClassifier {
21-
@Gateway(requestChannel = "flow.input")
22-
void flow(Collection<Integer> numbers);
19+
@Gateway(requestChannel = "classify.input")
20+
void classify(Collection<Integer> numbers);
2321
}
2422

2523
@Bean
26-
DirectChannel multipleof3Channel() {
27-
return new DirectChannel();
24+
QueueChannel multipleofThreeChannel() {
25+
return new QueueChannel();
2826
}
2927

3028
@Bean
31-
DirectChannel remainderIs1Channel() {
32-
return new DirectChannel();
29+
QueueChannel remainderIsOneChannel() {
30+
return new QueueChannel();
3331
}
3432

3533
@Bean
36-
DirectChannel remainderIs2Channel() {
37-
return new DirectChannel();
34+
QueueChannel remainderIsTwoChannel() {
35+
return new QueueChannel();
36+
}
37+
boolean isMultipleOfThree(Integer number) {
38+
return number % 3 == 0;
39+
}
40+
41+
boolean isRemainderOne(Integer number) {
42+
return number % 3 == 1;
3843
}
3944

45+
boolean isRemainderTwo(Integer number) {
46+
return number % 3 == 2;
47+
}
4048
@Bean
41-
public IntegrationFlow flow() {
49+
public IntegrationFlow classify() {
4250
return flow -> flow.split()
43-
.publishSubscribeChannel(s -> s.subscribe(f -> f.<Integer> filter(p -> p % 3 == 0)
44-
.channel("multipleof3Channel"))
45-
.subscribe(f -> f.<Integer> filter(p -> p % 3 == 1)
46-
.channel("remainderIs1Channel"))
47-
.subscribe(f -> f.<Integer> filter(p -> p % 3 == 2)
48-
.channel("remainderIs2Channel")));
49-
}
50-
51-
public static void main(String[] args) {
52-
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(PublishSubscibeChannelExample.class);
53-
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
54-
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
55-
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
56-
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
57-
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
58-
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
59-
ctx.getBean(NumbersClassifier.class)
60-
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
61-
ctx.close();
51+
.publishSubscribeChannel(subscription -> subscription.subscribe(subflow -> subflow.<Integer> filter(this::isMultipleOfThree)
52+
.channel("multipleofThreeChannel"))
53+
.subscribe(subflow -> subflow.<Integer> filter(this::isRemainderOne)
54+
.channel("remainderIsOneChannel"))
55+
.subscribe(subflow -> subflow.<Integer> filter(this::isRemainderTwo)
56+
.channel("remainderIsTwoChannel")));
6257
}
58+
59+
6360
}
Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
package com.baeldung.subflows.routeToRecipients;
1+
package com.baeldung.subflows.routetorecipients;
22

3-
import java.util.Arrays;
43
import java.util.Collection;
54

6-
import org.springframework.context.ConfigurableApplicationContext;
7-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
85
import org.springframework.context.annotation.Bean;
96
import org.springframework.integration.annotation.Gateway;
107
import org.springframework.integration.annotation.IntegrationComponentScan;
118
import org.springframework.integration.annotation.MessagingGateway;
12-
import org.springframework.integration.channel.DirectChannel;
9+
import org.springframework.integration.channel.QueueChannel;
1310
import org.springframework.integration.config.EnableIntegration;
1411
import org.springframework.integration.dsl.IntegrationFlow;
1512

@@ -18,44 +15,46 @@
1815
public class RouteToRecipientsExample {
1916
@MessagingGateway
2017
public interface NumbersClassifier {
21-
@Gateway(requestChannel = "flow.input")
22-
void flow(Collection<Integer> numbers);
18+
@Gateway(requestChannel = "classify.input")
19+
void classify(Collection<Integer> numbers);
2320
}
2421

2522
@Bean
26-
DirectChannel multipleof3Channel() {
27-
return new DirectChannel();
23+
QueueChannel multipleofThreeChannel() {
24+
return new QueueChannel();
2825
}
2926

3027
@Bean
31-
DirectChannel remainderIs1Channel() {
32-
return new DirectChannel();
28+
QueueChannel remainderIsOneChannel() {
29+
return new QueueChannel();
3330
}
3431

3532
@Bean
36-
DirectChannel remainderIs2Channel() {
37-
return new DirectChannel();
33+
QueueChannel remainderIsTwoChannel() {
34+
return new QueueChannel();
35+
}
36+
boolean isMultipleOfThree(Integer number) {
37+
return number % 3 == 0;
38+
}
39+
40+
boolean isRemainderOne(Integer number) {
41+
return number % 3 == 1;
42+
}
43+
44+
boolean isRemainderTwo(Integer number) {
45+
return number % 3 == 2;
3846
}
3947

4048
@Bean
41-
public IntegrationFlow flow() {
49+
public IntegrationFlow classify() {
4250
return flow -> flow.split()
43-
.routeToRecipients(r -> r.<Integer> recipient("multipleof3Channel", p -> p % 3 == 0)// filter
44-
.<Integer> recipient("remainderIs1Channel", p -> p % 3 == 1)
45-
.recipientFlow(sf -> sf.<Integer> filter(p -> p % 3 == 2)
46-
.channel("remainderIs2Channel")));
47-
}
48-
49-
public static void main(String[] args) {
50-
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(RouteToRecipientsExample.class);
51-
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
52-
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
53-
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
54-
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
55-
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
56-
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
57-
ctx.getBean(NumbersClassifier.class)
58-
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
59-
ctx.close();
51+
.routeToRecipients(route -> route
52+
.recipientFlow(subflow -> subflow
53+
.<Integer> filter(this::isMultipleOfThree)
54+
.channel("multipleofThreeChannel"))
55+
.<Integer> recipient("remainderIsOneChannel",this::isRemainderOne)
56+
.<Integer> recipient("remainderIsTwoChannel",this::isRemainderTwo));
6057
}
58+
59+
6160
}
Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.baeldung.subflows.separateflows;
22

3-
import java.util.Arrays;
43
import java.util.Collection;
54

6-
import org.springframework.context.ConfigurableApplicationContext;
7-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
85
import org.springframework.context.annotation.Bean;
96
import org.springframework.integration.annotation.Gateway;
107
import org.springframework.integration.annotation.IntegrationComponentScan;
118
import org.springframework.integration.annotation.MessagingGateway;
12-
import org.springframework.integration.channel.DirectChannel;
9+
10+
import org.springframework.integration.channel.QueueChannel;
1311
import org.springframework.integration.config.EnableIntegration;
1412
import org.springframework.integration.dsl.IntegrationFlow;
1513

@@ -18,66 +16,62 @@
1816
public 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

Comments
 (0)