Skip to content

Commit 400d28d

Browse files
Merge pull request eugenp#5576 from myluckagain/BAEL-2276
BAEL-2276
2 parents 2c5f92e + d81cb4a commit 400d28d

5 files changed

Lines changed: 328 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.subflows.publishsubscribechannel;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.integration.annotation.Gateway;
10+
import org.springframework.integration.annotation.IntegrationComponentScan;
11+
import org.springframework.integration.annotation.MessagingGateway;
12+
import org.springframework.integration.channel.DirectChannel;
13+
import org.springframework.integration.config.EnableIntegration;
14+
import org.springframework.integration.dsl.IntegrationFlow;
15+
16+
@EnableIntegration
17+
@IntegrationComponentScan
18+
public class PublishSubscibeChannelExample {
19+
@MessagingGateway
20+
public interface NumbersClassifier {
21+
@Gateway(requestChannel = "flow.input")
22+
void flow(Collection<Integer> numbers);
23+
}
24+
25+
@Bean
26+
DirectChannel multipleof3Channel() {
27+
return new DirectChannel();
28+
}
29+
30+
@Bean
31+
DirectChannel remainderIs1Channel() {
32+
return new DirectChannel();
33+
}
34+
35+
@Bean
36+
DirectChannel remainderIs2Channel() {
37+
return new DirectChannel();
38+
}
39+
40+
@Bean
41+
public IntegrationFlow flow() {
42+
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();
62+
}
63+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.subflows.routeToRecipients;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.integration.annotation.Gateway;
10+
import org.springframework.integration.annotation.IntegrationComponentScan;
11+
import org.springframework.integration.annotation.MessagingGateway;
12+
import org.springframework.integration.channel.DirectChannel;
13+
import org.springframework.integration.config.EnableIntegration;
14+
import org.springframework.integration.dsl.IntegrationFlow;
15+
16+
@EnableIntegration
17+
@IntegrationComponentScan
18+
public class RouteToRecipientsExample {
19+
@MessagingGateway
20+
public interface NumbersClassifier {
21+
@Gateway(requestChannel = "flow.input")
22+
void flow(Collection<Integer> numbers);
23+
}
24+
25+
@Bean
26+
DirectChannel multipleof3Channel() {
27+
return new DirectChannel();
28+
}
29+
30+
@Bean
31+
DirectChannel remainderIs1Channel() {
32+
return new DirectChannel();
33+
}
34+
35+
@Bean
36+
DirectChannel remainderIs2Channel() {
37+
return new DirectChannel();
38+
}
39+
40+
@Bean
41+
public IntegrationFlow flow() {
42+
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();
60+
}
61+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.subflows.separateflows;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.integration.annotation.Gateway;
10+
import org.springframework.integration.annotation.IntegrationComponentScan;
11+
import org.springframework.integration.annotation.MessagingGateway;
12+
import org.springframework.integration.channel.DirectChannel;
13+
import org.springframework.integration.config.EnableIntegration;
14+
import org.springframework.integration.dsl.IntegrationFlow;
15+
16+
@EnableIntegration
17+
@IntegrationComponentScan
18+
public class SeparateFlowsExample {
19+
@MessagingGateway
20+
public interface NumbersClassifier {
21+
@Gateway(requestChannel = "multipleof3Flow.input")
22+
void multipleof3(Collection<Integer> is);
23+
24+
@Gateway(requestChannel = "remainderIs1Flow.input")
25+
void remainderIs1(Collection<Integer> is);
26+
27+
@Gateway(requestChannel = "remainderIs2Flow.input")
28+
void remainderIs2(Collection<Integer> numbers);
29+
}
30+
31+
@Bean
32+
DirectChannel multipleof3Channel() {
33+
return new DirectChannel();
34+
}
35+
36+
@Bean
37+
DirectChannel remainderIs1Channel() {
38+
return new DirectChannel();
39+
}
40+
41+
@Bean
42+
DirectChannel remainderIs2Channel() {
43+
return new DirectChannel();
44+
}
45+
46+
@Bean
47+
public IntegrationFlow multipleof3Flow() {
48+
return f -> f.split()
49+
.<Integer> filter(p -> p % 3 == 0)
50+
.channel("multipleof3Channel");
51+
}
52+
53+
@Bean
54+
public IntegrationFlow remainderIs1Flow() {
55+
return f -> f.split()
56+
.<Integer> filter(p -> p % 3 == 1)
57+
.channel("remainderIs1Channel");
58+
}
59+
60+
@Bean
61+
public IntegrationFlow remainderIs2Flow() {
62+
return f -> f.split()
63+
.<Integer> filter(p -> p % 3 == 2)
64+
.channel("remainderIs2Channel");
65+
}
66+
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();
82+
}
83+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.subflows.subflowchannel;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.integration.annotation.Gateway;
10+
import org.springframework.integration.annotation.IntegrationComponentScan;
11+
import org.springframework.integration.annotation.MessagingGateway;
12+
import org.springframework.integration.channel.DirectChannel;
13+
import org.springframework.integration.config.EnableIntegration;
14+
import org.springframework.integration.dsl.IntegrationFlow;
15+
16+
@EnableIntegration
17+
@IntegrationComponentScan
18+
public class FilterExample {
19+
@MessagingGateway
20+
public interface NumbersClassifier {
21+
@Gateway(requestChannel = "flow.input")
22+
void flow(Collection<Integer> numbers);
23+
}
24+
25+
@Bean
26+
DirectChannel multipleof3Channel() {
27+
return new DirectChannel();
28+
}
29+
30+
@Bean
31+
DirectChannel remainderIs1Channel() {
32+
return new DirectChannel();
33+
}
34+
35+
@Bean
36+
DirectChannel remainderIs2Channel() {
37+
return new DirectChannel();
38+
}
39+
40+
@Bean
41+
public IntegrationFlow flow() {
42+
return flow -> flow.split()
43+
.<Integer> filter(x -> x % 3 == 0, sf -> sf.discardFlow(subf -> subf.<Integer> filter(x -> x % 3 == 1, ssf -> ssf.discardChannel("remainderIs2Channel"))
44+
.channel("remainderIs1Channel")))
45+
.channel("multipleof3Channel");
46+
}
47+
48+
public static void main(String[] args) {
49+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(FilterExample.class);
50+
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
51+
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
52+
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
53+
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
54+
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
55+
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
56+
ctx.getBean(NumbersClassifier.class)
57+
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
58+
ctx.close();
59+
}
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.subflows.subflowmapping;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.integration.annotation.Gateway;
10+
import org.springframework.integration.annotation.IntegrationComponentScan;
11+
import org.springframework.integration.annotation.MessagingGateway;
12+
import org.springframework.integration.channel.DirectChannel;
13+
import org.springframework.integration.config.EnableIntegration;
14+
import org.springframework.integration.dsl.IntegrationFlow;
15+
16+
@EnableIntegration
17+
@IntegrationComponentScan
18+
public class RouterExample {
19+
@MessagingGateway
20+
public interface NumbersClassifier {
21+
@Gateway(requestChannel = "flow.input")
22+
void flow(Collection<Integer> numbers);
23+
}
24+
25+
@Bean
26+
DirectChannel multipleof3Channel() {
27+
return new DirectChannel();
28+
}
29+
30+
@Bean
31+
DirectChannel remainderIs1Channel() {
32+
return new DirectChannel();
33+
}
34+
35+
@Bean
36+
DirectChannel remainderIs2Channel() {
37+
return new DirectChannel();
38+
}
39+
40+
@Bean
41+
public IntegrationFlow flow() {
42+
return f -> f.split()
43+
.<Integer, Integer> route(p -> p % 3, m -> m.channelMapping(0, "multipleof3Channel")
44+
.subFlowMapping(1, sf -> sf.channel("remainderIs1Channel"))
45+
.subFlowMapping(2, sf -> sf.<Integer> handle((p, h) -> p)))
46+
.channel("remainderIs2Channel");
47+
}
48+
49+
public static void main(String[] args) {
50+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(RouterExample.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();
60+
}
61+
}

0 commit comments

Comments
 (0)