|
| 1 | +package com.baeldung.dsl; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Scanner; |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | + |
| 7 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.context.support.AbstractApplicationContext; |
| 11 | +import org.springframework.integration.annotation.IntegrationComponentScan; |
| 12 | +import org.springframework.integration.channel.PriorityChannel; |
| 13 | +import org.springframework.integration.config.EnableIntegration; |
| 14 | +import org.springframework.integration.core.GenericSelector; |
| 15 | +import org.springframework.integration.core.MessageSource; |
| 16 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 17 | +import org.springframework.integration.dsl.IntegrationFlows; |
| 18 | +import org.springframework.integration.dsl.Pollers; |
| 19 | +import org.springframework.integration.file.FileReadingMessageSource; |
| 20 | +import org.springframework.integration.file.FileWritingMessageHandler; |
| 21 | +import org.springframework.messaging.MessageHandler; |
| 22 | + |
| 23 | +/** |
| 24 | + * JavaDSLFileCopyConfig contains various Integration Flows created from various spring integration components. |
| 25 | + * Activate only one flow at a time by un-commenting @Bean annotation from IntegrationFlow beans. |
| 26 | + * <p> |
| 27 | + * Different flows are :<br> |
| 28 | + * - {@link #fileMover()} - default app - activated<br> |
| 29 | + * - {@link #fileMoverWithLambda()} - app with file writing expressions as lambda<br> |
| 30 | + * - {@link #fileMoverWithPriorityChannel()} - app with priority channel<br> |
| 31 | + * - {@link #fileReader()}, {@link #fileWriter()}, {@link #anotherFileWriter()} - app with bridge |
| 32 | + */ |
| 33 | +@Configuration |
| 34 | +@EnableIntegration |
| 35 | +@IntegrationComponentScan |
| 36 | +public class JavaDSLFileCopyConfig { |
| 37 | + |
| 38 | + public static final String INPUT_DIR = "source"; |
| 39 | + public static final String OUTPUT_DIR = "target"; |
| 40 | + public static final String OUTPUT_DIR2 = "target2"; |
| 41 | + |
| 42 | + @Bean |
| 43 | + public MessageSource<File> sourceDirectory() { |
| 44 | + FileReadingMessageSource messageSource = new FileReadingMessageSource(); |
| 45 | + messageSource.setDirectory(new File(INPUT_DIR)); |
| 46 | + return messageSource; |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public GenericSelector<File> onlyJpgs() { |
| 51 | + return new GenericSelector<File>() { |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean accept(File source) { |
| 55 | + return source.getName() |
| 56 | + .endsWith(".jpg"); |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + public MessageHandler targetDirectory() { |
| 63 | + FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR)); |
| 64 | + handler.setExpectReply(false); // end of pipeline, reply not needed |
| 65 | + return handler; |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public IntegrationFlow fileMover() { |
| 70 | + return IntegrationFlows.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000))) |
| 71 | + .filter(onlyJpgs()) |
| 72 | + .handle(targetDirectory()) |
| 73 | + .get(); |
| 74 | + } |
| 75 | + |
| 76 | + // @Bean |
| 77 | + public IntegrationFlow fileMoverWithLambda() { |
| 78 | + return IntegrationFlows.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000))) |
| 79 | + .filter(message -> ((File) message).getName() |
| 80 | + .endsWith(".jpg")) |
| 81 | + .handle(targetDirectory()) |
| 82 | + .get(); |
| 83 | + } |
| 84 | + |
| 85 | + @Bean |
| 86 | + public PriorityChannel alphabetically() { |
| 87 | + return new PriorityChannel(1000, (left, right) -> ((File) left.getPayload()).getName() |
| 88 | + .compareTo(((File) right.getPayload()).getName())); |
| 89 | + } |
| 90 | + |
| 91 | + // @Bean |
| 92 | + public IntegrationFlow fileMoverWithPriorityChannel() { |
| 93 | + return IntegrationFlows.from(sourceDirectory()) |
| 94 | + .filter(onlyJpgs()) |
| 95 | + .channel("alphabetically") |
| 96 | + .handle(targetDirectory()) |
| 97 | + .get(); |
| 98 | + } |
| 99 | + |
| 100 | + @Bean |
| 101 | + public MessageHandler anotherTargetDirectory() { |
| 102 | + FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR2)); |
| 103 | + handler.setExpectReply(false); // end of pipeline, reply not needed |
| 104 | + return handler; |
| 105 | + } |
| 106 | + |
| 107 | + // @Bean |
| 108 | + public IntegrationFlow fileReader() { |
| 109 | + return IntegrationFlows.from(sourceDirectory()) |
| 110 | + .filter(onlyJpgs()) |
| 111 | + .channel("holdingTank") |
| 112 | + .get(); |
| 113 | + } |
| 114 | + |
| 115 | + // @Bean |
| 116 | + public IntegrationFlow fileWriter() { |
| 117 | + return IntegrationFlows.from("holdingTank") |
| 118 | + .bridge(e -> e.poller(Pollers.fixedRate(1, TimeUnit.SECONDS, 20))) |
| 119 | + .handle(targetDirectory()) |
| 120 | + .get(); |
| 121 | + } |
| 122 | + |
| 123 | + // @Bean |
| 124 | + public IntegrationFlow anotherFileWriter() { |
| 125 | + return IntegrationFlows.from("holdingTank") |
| 126 | + .bridge(e -> e.poller(Pollers.fixedRate(2, TimeUnit.SECONDS, 10))) |
| 127 | + .handle(anotherTargetDirectory()) |
| 128 | + .get(); |
| 129 | + } |
| 130 | + |
| 131 | + public static void main(final String... args) { |
| 132 | + final AbstractApplicationContext context = new AnnotationConfigApplicationContext(JavaDSLFileCopyConfig.class); |
| 133 | + context.registerShutdownHook(); |
| 134 | + final Scanner scanner = new Scanner(System.in); |
| 135 | + System.out.print("Please enter a string and press <enter>: "); |
| 136 | + while (true) { |
| 137 | + final String input = scanner.nextLine(); |
| 138 | + if ("q".equals(input.trim())) { |
| 139 | + context.close(); |
| 140 | + scanner.close(); |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + System.exit(0); |
| 145 | + } |
| 146 | +} |
0 commit comments