From 3c254fad0bef21108675dbd2fa4f45c59a3049e8 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Tue, 15 Nov 2016 07:04:56 +0100 Subject: [PATCH] BAEL-80 - file copy java config --- .../com/baeldung/samples/FileCopyConfig.java | 72 +++++++++++++++++++ .../src/main/resources/log4j.xml | 28 ++++++++ .../com/baeldung/samples/FileCopyTest.java | 14 ++-- 3 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 spring-integration/src/main/java/com/baeldung/samples/FileCopyConfig.java create mode 100644 spring-integration/src/main/resources/log4j.xml diff --git a/spring-integration/src/main/java/com/baeldung/samples/FileCopyConfig.java b/spring-integration/src/main/java/com/baeldung/samples/FileCopyConfig.java new file mode 100644 index 0000000000..e7cf43e902 --- /dev/null +++ b/spring-integration/src/main/java/com/baeldung/samples/FileCopyConfig.java @@ -0,0 +1,72 @@ +package com.baeldung.samples; + +import java.io.File; +import java.util.Scanner; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.integration.annotation.InboundChannelAdapter; +import org.springframework.integration.annotation.Poller; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.core.MessageSource; +import org.springframework.integration.file.FileReadingMessageSource; +import org.springframework.integration.file.FileWritingMessageHandler; +import org.springframework.integration.file.filters.SimplePatternFileListFilter; +import org.springframework.integration.file.support.FileExistsMode; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.MessageHandler; + +@Configuration +@EnableIntegration +public class FileCopyConfig { + + public final String INPUT_DIR = "source"; + public final String OUTPUT_DIR = "target"; + public final String FILE_PATTERN = ".jpg"; + + @Bean + public MessageChannel fileChannel() { + return new DirectChannel(); + } + + @Bean + @InboundChannelAdapter(value = "fileChannel", poller = @Poller(fixedDelay = "10000")) + public MessageSource fileReadingMessageSource() { + FileReadingMessageSource sourceReader = new FileReadingMessageSource(); + sourceReader.setDirectory(new File(INPUT_DIR)); + sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN)); + return sourceReader; + } + + @Bean + @ServiceActivator(inputChannel = "fileChannel") + public MessageHandler fileWritingMessageHandler() { + FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(OUTPUT_DIR)); + handler.setFileExistsMode(FileExistsMode.REPLACE); + return handler; + } + + public static void main(final String... args) { + final AbstractApplicationContext context = new AnnotationConfigApplicationContext(FileCopyConfig.class.getCanonicalName()); + context.registerShutdownHook(); + final Scanner scanner = new Scanner(System.in); + System.out.print("Please enter a string and press : "); + while (true) { + final String input = scanner.nextLine(); + if ("q".equals(input.trim())) { + context.close(); + scanner.close(); + break; + } + } + System.exit(0); + } + +} + + + diff --git a/spring-integration/src/main/resources/log4j.xml b/spring-integration/src/main/resources/log4j.xml new file mode 100644 index 0000000000..cfa93a8037 --- /dev/null +++ b/spring-integration/src/main/resources/log4j.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration/src/test/java/com/baeldung/samples/FileCopyTest.java b/spring-integration/src/test/java/com/baeldung/samples/FileCopyTest.java index 96e5a98f41..567d181972 100644 --- a/spring-integration/src/test/java/com/baeldung/samples/FileCopyTest.java +++ b/spring-integration/src/test/java/com/baeldung/samples/FileCopyTest.java @@ -17,11 +17,10 @@ package com.baeldung.samples; import org.apache.log4j.Logger; import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import java.util.Scanner; - /** * Starts the Spring Context and will initialize the Spring Integration routes. @@ -35,15 +34,10 @@ public final class FileCopyTest { private static final Logger LOGGER = Logger.getLogger(FileCopyTest.class); @Test - public void test() throws InterruptedException { - - - final AbstractApplicationContext context = - new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/spring-integration-file-copy-context.xml"); - + public void whenFileCopyConfiguration_thanFileCopiedSuccessfully() throws InterruptedException { + final AbstractApplicationContext context = new AnnotationConfigApplicationContext(FileCopyConfig.class.getCanonicalName()); + context.registerShutdownHook(); Thread.sleep(5000); - - } @Test