http://manning.com/ibsen http://code.google.com/p/camelinaction.
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”的集成:路由文件。假设你
需要从一个目录中读取文件
(数据/收件箱),以某种方式处理它们
方法,并将结果写入另一个
目录(数据/发件箱)。为了简单起见,您将跳过中间处理,因此
输出将只是原始文件的一个副本。图1.2说明了这个过程。
看起来很简单,对吧?这里有一个可能的解决方案,使用纯Java(没有Camel)。
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();
}
}
}
The FileCopier example in listing 1.1 is a pretty simple use case, but it still results
in 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.
清单1.1中的FileCopier示例是一个非常简单的用例,但它仍然会产生
34行代码。您必须使用低级文件api并确保资源
如果关闭得当,很容易出错的任务。另外,如果你想调查数据/
收件箱目录中的新文件,你需要设置一个计时器,并保持跟踪
你已经复制了哪些文件。这个简单的例子越来越复杂。
像这样的集成任务在您之前已经完成了数千次
不需要手工编写这样的代码。我们不要重新发明轮子
在这里。让我们看看如果使用集成框架,轮询解决方案会是什么样子
就像Apache Camel。
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 @1
当使用Camel时,这些代码的大部分都是样板文件。每个Camel应用程序都使用
随后启动然后停止的上下文。你还加了一个睡眠时间
方法允许简单的Camel应用程序有时间复制文件。你应该真正关注清单1.2中的route(路由) @1
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 @1
Camel中的路由的定义方式使它们在读取时流动。这条路线可以
可以这样理解:使用noop(no operation)从文件位置数据/收件箱中消费消息
并发送到文件位置数据/发件箱。noop选项告诉Camel
保持源文件原样。如果不使用此选项,文件将被移动。以前从未见过Camel的人将能够理解这条路线
您可能还需要注意,除了样板代码之外,您只创建了一行Java代码中文件轮询route
- maven相关忽略