import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileCopier {
public static void main(String args[]) throws Exception {
File inboxDirectory = new File("data/inbox");
File outboxDirectory = new File("data/outbox");
outboxDirectory.mkdir();
File[] files = inboxDirectory.listFiles();
for (File source : files) {
if (source.isFile()) {
File dest = new File(outboxDirectory.getPath() + File.separator + source.getName());
copyFile(source, dest);
}
}
}
private static void copyFile(File source, File dest) throws IOException {
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[(int) source.length()];
FileInputStream in = new FileInputStream(source);
in.read(buffer);
try {
out.write(buffer);
} finally {
out.close();
in.close();
}
}
}
You have to use low-level file APIs and ensure that resources get closed properly, a task that can easily go wrong. Also, if you wanted to poll the data/ inbox directory for new files, you’d need to set up a timer and also keep track of which files you’ve already copied. This simple example is getting more complex.
Every Camel application uses a CamelContext that’s subsequently started and then stopped. You also add a sleep method to allow your simple Camel application time to copy the files. What you should really focus on in listing 1.2 is the route B. Routes in Camel are defined in such a way that they flow when read. This route can be read like this: consume messages from file location data/inbox with the noop option set, and send to file location data/outbox. The noop option tells Camel to leave the source file as is. If you didn’t use this option, the file would be moved. Most people who have never seen Camel before will be able to understand what this route does. You may also want to note that, excluding the boilerplate code, you created a file-polling route in just one line of Java code.
메일을 보내기 위해선 먼저, pom.xml에 메일 보내는 dependency를 추가해야한다.
<!-- spring boot용 camel을 사용하기 위한 디펜던시-->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<!-- Mail 컴포넌트 -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-mail-starter</artifactId>
<version>${camel.version}</version>
<!-- use the same version as your Camel core version -->
</dependency>
그리고 네이버 메일 설정에서 SMTP 옵션을 허용해야한다.
그리고 설정 밑에 아래와 같은 정보를 참고해서
환경설정에 등록할 아이디, 네이버 비번, SMTP 서버명, SMTP 포트, 보안 연결 SSL 필요 정보