|
| 1 | +package com.baeldung.samples; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.support.AbstractApplicationContext; |
| 10 | +import org.springframework.integration.annotation.InboundChannelAdapter; |
| 11 | +import org.springframework.integration.annotation.Poller; |
| 12 | +import org.springframework.integration.annotation.ServiceActivator; |
| 13 | +import org.springframework.integration.channel.DirectChannel; |
| 14 | +import org.springframework.integration.config.EnableIntegration; |
| 15 | +import org.springframework.integration.core.MessageSource; |
| 16 | +import org.springframework.integration.file.FileReadingMessageSource; |
| 17 | +import org.springframework.integration.file.FileWritingMessageHandler; |
| 18 | +import org.springframework.integration.file.filters.SimplePatternFileListFilter; |
| 19 | +import org.springframework.integration.file.support.FileExistsMode; |
| 20 | +import org.springframework.messaging.MessageChannel; |
| 21 | +import org.springframework.messaging.MessageHandler; |
| 22 | + |
| 23 | +@Configuration |
| 24 | +@EnableIntegration |
| 25 | +public class FileCopyConfig { |
| 26 | + |
| 27 | + public final String INPUT_DIR = "source"; |
| 28 | + public final String OUTPUT_DIR = "target"; |
| 29 | + public final String FILE_PATTERN = ".jpg"; |
| 30 | + |
| 31 | + @Bean |
| 32 | + public MessageChannel fileChannel() { |
| 33 | + return new DirectChannel(); |
| 34 | + } |
| 35 | + |
| 36 | + @Bean |
| 37 | + @InboundChannelAdapter(value = "fileChannel", poller = @Poller(fixedDelay = "10000")) |
| 38 | + public MessageSource<File> fileReadingMessageSource() { |
| 39 | + FileReadingMessageSource sourceReader = new FileReadingMessageSource(); |
| 40 | + sourceReader.setDirectory(new File(INPUT_DIR)); |
| 41 | + sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN)); |
| 42 | + return sourceReader; |
| 43 | + } |
| 44 | + |
| 45 | + @Bean |
| 46 | + @ServiceActivator(inputChannel = "fileChannel") |
| 47 | + public MessageHandler fileWritingMessageHandler() { |
| 48 | + FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR)); |
| 49 | + handler.setFileExistsMode(FileExistsMode.REPLACE); |
| 50 | + return handler; |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(final String... args) { |
| 54 | + final AbstractApplicationContext context = new AnnotationConfigApplicationContext(FileCopyConfig.class.getCanonicalName()); |
| 55 | + context.registerShutdownHook(); |
| 56 | + final Scanner scanner = new Scanner(System.in); |
| 57 | + System.out.print("Please enter a string and press <enter>: "); |
| 58 | + while (true) { |
| 59 | + final String input = scanner.nextLine(); |
| 60 | + if ("q".equals(input.trim())) { |
| 61 | + context.close(); |
| 62 | + scanner.close(); |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + System.exit(0); |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments