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

注:样例Demo并没有完全按照书中的源码,稍微做了一点修改。

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo

如果觉得还行,麻烦点个Star

 

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)的可能解决方案。

 

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");)。

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值