Skip to content

Commit 2616948

Browse files
committed
BAEL-2276
1 parent 758648e commit 2616948

5 files changed

Lines changed: 387 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
20+
@MessagingGateway
21+
public interface I {
22+
23+
@Gateway(requestChannel = "flow.input")
24+
void flow(Collection<Integer> is);
25+
26+
}
27+
28+
@Bean
29+
DirectChannel multipleof3Channel() {
30+
return new DirectChannel();
31+
}
32+
33+
@Bean
34+
DirectChannel remainderIs1Channel() {
35+
return new DirectChannel();
36+
}
37+
38+
@Bean
39+
DirectChannel remainderIs2Channel() {
40+
return new DirectChannel();
41+
}
42+
43+
@Bean
44+
public IntegrationFlow flow() {
45+
return flow -> flow.split()
46+
.publishSubscribeChannel(s ->
47+
s.subscribe(f -> f.<Integer> filter(p -> p % 3 == 0).channel("multipleof3Channel"))
48+
.subscribe(f -> f.<Integer> filter(p -> p % 3 == 1).channel("remainderIs1Channel"))
49+
.subscribe(f -> f.<Integer> filter(p -> p % 3 == 2).channel("remainderIs2Channel"))
50+
);
51+
}
52+
53+
public static void main(String[] args) {
54+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(PublishSubscibeChannelExample.class);
55+
56+
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
57+
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
58+
59+
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
60+
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
61+
62+
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
63+
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
64+
65+
ctx.getBean(I.class)
66+
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
67+
68+
ctx.close();
69+
70+
}
71+
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
20+
@MessagingGateway
21+
public interface I {
22+
23+
@Gateway(requestChannel = "flow.input")
24+
void flow(Collection<Integer> is);
25+
26+
}
27+
28+
@Bean
29+
DirectChannel multipleof3Channel() {
30+
return new DirectChannel();
31+
}
32+
33+
@Bean
34+
DirectChannel remainderIs1Channel() {
35+
return new DirectChannel();
36+
}
37+
38+
@Bean
39+
DirectChannel remainderIs2Channel() {
40+
return new DirectChannel();
41+
}
42+
43+
@Bean
44+
public IntegrationFlow flow() {
45+
return flow -> flow.split()
46+
47+
.routeToRecipients(r -> r.<Integer> recipient("multipleof3Channel", p -> p % 3 == 0)// filter
48+
.<Integer> recipient("remainderIs1Channel", p -> p % 3 == 1)
49+
.recipientFlow(sf -> sf.<Integer> filter(p -> p % 3 == 2)
50+
.channel("remainderIs2Channel")));
51+
}
52+
53+
public static void main(String[] args) {
54+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(RouteToRecipientsExample.class);
55+
56+
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
57+
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
58+
59+
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
60+
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
61+
62+
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
63+
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
64+
65+
ctx.getBean(I.class)
66+
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
67+
68+
ctx.close();
69+
70+
}
71+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
20+
@MessagingGateway
21+
public interface I {
22+
23+
@Gateway(requestChannel = "multipleof3Flow.input")
24+
void multipleof3(Collection<Integer> is);
25+
26+
@Gateway(requestChannel = "remainderIs1Flow.input")
27+
void remainderIs1(Collection<Integer> is);
28+
29+
@Gateway(requestChannel = "remainderIs2Flow.input")
30+
void remainderIs2(Collection<Integer> is);
31+
32+
}
33+
34+
@Bean
35+
DirectChannel multipleof3Channel() {
36+
return new DirectChannel();
37+
}
38+
39+
@Bean
40+
DirectChannel remainderIs1Channel() {
41+
return new DirectChannel();
42+
}
43+
44+
@Bean
45+
DirectChannel remainderIs2Channel() {
46+
return new DirectChannel();
47+
}
48+
49+
@Bean
50+
public IntegrationFlow multipleof3Flow() {
51+
return f -> f.split()
52+
.<Integer> filter(p -> p % 3 == 0)
53+
.channel("multipleof3Channel");
54+
55+
}
56+
57+
@Bean
58+
public IntegrationFlow remainderIs1Flow() {
59+
return f -> f.split()
60+
.<Integer> filter(p -> p % 3 == 1)
61+
.channel("remainderIs1Channel");
62+
63+
}
64+
65+
@Bean
66+
public IntegrationFlow remainderIs2Flow() {
67+
return f -> f.split()
68+
.<Integer> filter(p -> p % 3 == 2)
69+
.channel("remainderIs2Channel");
70+
71+
}
72+
73+
public static void main(String[] args) {
74+
75+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(SeparateFlowsExample.class);
76+
77+
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
78+
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
79+
80+
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
81+
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
82+
83+
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
84+
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
85+
86+
ctx.getBean(I.class)
87+
.multipleof3(Arrays.asList(1, 2, 3, 4, 5, 6));
88+
89+
ctx.getBean(I.class)
90+
.remainderIs1(Arrays.asList(1, 2, 3, 4, 5, 6));
91+
92+
ctx.getBean(I.class)
93+
.remainderIs2(Arrays.asList(1, 2, 3, 4, 5, 6));
94+
95+
ctx.close();
96+
97+
}
98+
99+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
20+
@MessagingGateway
21+
public interface I {
22+
23+
@Gateway(requestChannel = "flow.input")
24+
void flow(Collection<Integer> is);
25+
26+
}
27+
28+
@Bean
29+
DirectChannel multipleof3Channel() {
30+
return new DirectChannel();
31+
}
32+
33+
@Bean
34+
DirectChannel remainderIs1Channel() {
35+
return new DirectChannel();
36+
}
37+
38+
@Bean
39+
DirectChannel remainderIs2Channel() {
40+
return new DirectChannel();
41+
}
42+
43+
@Bean
44+
public IntegrationFlow flow() {
45+
return flow -> flow.split()
46+
47+
.<Integer> filter(x -> x % 3 == 0, sf -> sf.discardFlow(subf -> subf
48+
49+
.<Integer> filter(x -> x % 3 == 1, ssf -> ssf.discardChannel("remainderIs2Channel"))
50+
.channel("remainderIs1Channel")
51+
52+
))
53+
54+
.channel("multipleof3Channel");
55+
}
56+
57+
public static void main(String[] args) {
58+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(FilterExample.class);
59+
60+
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
61+
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
62+
63+
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
64+
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
65+
66+
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
67+
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
68+
69+
ctx.getBean(I.class)
70+
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
71+
72+
ctx.close();
73+
74+
}
75+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 I {
21+
22+
@Gateway(requestChannel = "flow.input")
23+
void flow(Collection<Integer> is);
24+
25+
}
26+
27+
@Bean
28+
DirectChannel multipleof3Channel() {
29+
return new DirectChannel();
30+
}
31+
32+
@Bean
33+
DirectChannel remainderIs1Channel() {
34+
return new DirectChannel();
35+
}
36+
37+
@Bean
38+
DirectChannel remainderIs2Channel() {
39+
return new DirectChannel();
40+
}
41+
42+
@Bean
43+
public IntegrationFlow flow() {
44+
return f -> f.split()
45+
.<Integer, Integer> route(p -> p % 3, m -> m.channelMapping(0, "multipleof3Channel")
46+
.subFlowMapping(1, sf -> sf .channel("remainderIs1Channel"))
47+
.subFlowMapping(2, sf -> sf.<Integer> handle((p,h)->p)))
48+
.channel("remainderIs2Channel");
49+
}
50+
51+
public static void main(String[] args) {
52+
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(RouterExample.class);
53+
54+
DirectChannel multipleof3Channel = ctx.getBean("multipleof3Channel", DirectChannel.class);
55+
multipleof3Channel.subscribe(x -> System.out.println("multipleof3Channel: " + x));
56+
57+
DirectChannel remainderIs1Channel = ctx.getBean("remainderIs1Channel", DirectChannel.class);
58+
remainderIs1Channel.subscribe(x -> System.out.println("remainderIs1Channel: " + x));
59+
60+
DirectChannel remainderIs2Channel = ctx.getBean("remainderIs2Channel", DirectChannel.class);
61+
remainderIs2Channel.subscribe(x -> System.out.println("remainderIs2Channel: " + x));
62+
63+
ctx.getBean(I.class)
64+
.flow(Arrays.asList(1, 2, 3, 4, 5, 6));
65+
66+
ctx.close();
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)