Skip to content

Commit 3c254fa

Browse files
BAEL-80 - file copy java config
1 parent 3914a5d commit 3c254fa

3 files changed

Lines changed: 104 additions & 10 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3+
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
4+
5+
<!-- Appenders -->
6+
<appender name="console" class="org.apache.log4j.ConsoleAppender">
7+
<param name="Target" value="System.out"/>
8+
<layout class="org.apache.log4j.PatternLayout">
9+
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n"/>
10+
</layout>
11+
</appender>
12+
13+
<!-- Loggers -->
14+
<logger name="org.springframework.integration">
15+
<level value="warn"/>
16+
</logger>
17+
18+
<logger name="com.baeldung.samples">
19+
<level value="info"/>
20+
</logger>
21+
22+
<!-- Root Logger -->
23+
<root>
24+
<priority value="warn"/>
25+
<appender-ref ref="console"/>
26+
</root>
27+
28+
</log4j:configuration>

‎spring-integration/src/test/java/com/baeldung/samples/FileCopyTest.java‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717

1818
import org.apache.log4j.Logger;
1919
import org.junit.Test;
20+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2021
import org.springframework.context.support.AbstractApplicationContext;
2122
import org.springframework.context.support.ClassPathXmlApplicationContext;
2223

23-
import java.util.Scanner;
24-
2524

2625
/**
2726
* Starts the Spring Context and will initialize the Spring Integration routes.
@@ -35,15 +34,10 @@ public final class FileCopyTest {
3534
private static final Logger LOGGER = Logger.getLogger(FileCopyTest.class);
3635

3736
@Test
38-
public void test() throws InterruptedException {
39-
40-
41-
final AbstractApplicationContext context =
42-
new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/spring-integration-file-copy-context.xml");
43-
37+
public void whenFileCopyConfiguration_thanFileCopiedSuccessfully() throws InterruptedException {
38+
final AbstractApplicationContext context = new AnnotationConfigApplicationContext(FileCopyConfig.class.getCanonicalName());
39+
context.registerShutdownHook();
4440
Thread.sleep(5000);
45-
46-
4741
}
4842

4943
@Test

0 commit comments

Comments
 (0)