BAEL-80 - file copy java config
This commit is contained in:
parent
3914a5d417
commit
3c254fad0b
|
@ -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<File> 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 <enter>: ");
|
||||
while (true) {
|
||||
final String input = scanner.nextLine();
|
||||
if ("q".equals(input.trim())) {
|
||||
context.close();
|
||||
scanner.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Loggers -->
|
||||
<logger name="org.springframework.integration">
|
||||
<level value="warn"/>
|
||||
</logger>
|
||||
|
||||
<logger name="com.baeldung.samples">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="warn"/>
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue