|
| 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 | +} |
0 commit comments