天天看點

Apache Camel - 2 - Camel小栗子(File)

Your first Camel ride(第一次使用Camel)

The first example we’ll look at can be considered the “hello world” of integrations: routing files.

Suppose you need to read files from one directory (data/inbox), process them in some way, and write the result to another directory (data/outbox).

For simplicity, you’ll skip the processing, so your output will be merely a copy of the original file. Figure 1.2 illustrates this process.

It looks pretty simple, right? Here’s a possible solution using pure Java (with no Camel).

我們要看的第一個例子可以被看作是內建的“hello world”:路由檔案。

假設您需要從一個目錄(資料/收件箱)讀取檔案,以某種方式處理它們,并将結果寫入另一個目錄(資料/發件箱)。

為了簡單,您将跳過處理,是以您的輸出将隻是原始檔案的副本。 下圖說明了這個過程。

它看起來很簡單,對吧? 這是一個使用純Java(沒有Camel)的可能解決方案。

Apache Camel - 2 - Camel小栗子(File)
package com.test;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
public class TestCode {
 
        public static void main(String[] args) throws Exception {
 
        File inBoxDirectory = new File("./inbox");
        File outBoxDirectory = new File("./outbox");
 
        outBoxDirectory.mkdirs();
 
        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 Exception {
 
        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();
        }
 
        }
 
}

           

The FileCopier example in listing 1.1 is a pretty simple use case, but it still resultsin 34 lines of code.

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.

Integration tasks like these have been done thousands of times before—you shouldn’t ever need to code something like this by hand.

Let’s not reinvent the wheel here.

Let’s see what a polling solution looks like if you use an integration framework like Apache Camel.

上面的代碼,是一個非常簡單的用例,但仍然要寫這麼多代碼。

您必須使用低級别的檔案API,并確定正确關閉資源,這是一個很容易出錯的任務。

另外,如果你想輪詢資料/收件箱目錄中的新檔案,你需要設定一個計時器,并跟蹤你已經複制了哪些檔案。

這個簡單的例子變得越來越複雜。

像這樣的內建任務已經完成了數千次,您不需要手動編寫這樣的代碼。

我們不要在這裡重新發明輪子。

讓我們來看看如果使用像Apache Camel這樣的內建架構,輪詢解決方案是什麼樣的。

package com.test.camel.file;
 
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
 
public class FileCopierWithCamel {
 
        public static void main(String[] args) throws Exception {
 
        CamelContext context = new DefaultCamelContext();
 
        context.addRoutes(new RouteBuilder() {
 
        @Override
        public void configure() throws Exception {
                from("file:inbox?noop=true").to("file:outbox");
        }
        });
 
        context.start();
 
        // 通用沒有具體業務意義的代碼,隻是為了保證主線程不退出
        synchronized (FileCopierWithCamel.class) {
                FileCopierWithCamel.class.wait();
        }
 
        }
 
}

           

Most of this code is boilerplate stuff when using Camel.

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.

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.

大部分代碼是使用Camel時的樣闆檔案。

每個Camel應用程式使用一個随後啟動然後停止的CamelContext。

您還添加一個睡眠方法,讓您簡單的Camel應用程式時間來複制檔案。

Camel中的路線是這樣定義的,它們在閱讀時流動。 這條路線可以這樣讀取:使用設定的noop選項從檔案位置資料/收件箱中消息,并發送到檔案位置資料/發件箱。

noop選項告訴Camel按原樣離開源檔案。

如果你沒有使用這個選項,檔案将被移動。 大多數以前從未見過Camel的人将能夠了解這條路線的作用。

您可能還需要注意的是,除了樣闆代碼以外,您隻需在一行Java代碼中建立一個檔案輪詢路由

(from(“file:inbox?noop=true”).to(“file:outbox”)?。