使用 Camel 文件组件传输文件

x33g5p2x  于2022-09-28 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(583)

Apache****Camel File 组件 是将文件从源文件夹传输到目标的最简单方法,无需任何编码。本教程将教您如何使用 File 组件创建 Camel 应用程序,以及如何配置在 Route 中移动(或不移动)文件。

设置Camel项目

要启动 Camel 项目,您可以查看本教程:面向初学者的 Apache Camel 教程

接下来,让我们调整 RouteBuilder 以包含一个包含 Apache****File 组件的 Route:

public class MainApp {
  public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(
        new RouteBuilder() {
          public void configure() {
            from("file:src/data?noop=true").to("file:target/messages/others");
          }
        });
    context.start();
    Thread.sleep(5000);
    context.stop();
  }
}

这是带有文件组件的路由:

public class MyRouteBuilder extends RouteBuilder {
  public void configure() {
    from("file:src/data?noop=true").to("file:target/messages/others");
  }
}

这是运行上述路线时的项目树:

src
├── data
│   └── file.txt
└── main
    ├── java
    │   └── com
    │       └── masterspringboot
    │           └── camel
    │               ├── MainApp2.java
    │               ├── MainApp.java
    │               └── MyProcessor.java
    └── resources
        └── log4j2.properties

如您所见,“file.txt”已被复制到 target/messages/other。

在这条路线中,源文件夹和目标文件夹位于您的 Maven 项目中。您可以改用绝对路径:

public void configure() {
  from("file:/path/data?noop=true").to("file:/path/target/messages/others");
}

处理后移动文件

在从源文件夹传输期间,我们设置了 noop=true,这表明即使在传输(复制粘贴)之后,传输的文件也应保留在源文件夹中。如果未指定,则在传输后文件将从源文件夹中删除(剪切和粘贴)。

此外,如果 noop 设置为“true”,Camel 也会设置 idempotent=true,以避免重复使用相同的文件。

为文件组件使用处理器

让我们为我们的示例添加一些复杂性。在大多数情况下,您希望对在 Route 中移动的文件执行操作。

最简单的方法是在路由中链接处理器 Bean:

public void configure() {
  from("file:src/data?noop=true").process(new MyProcessor()).to("file:target/messages/others");
}

在 MyProcessor 类中,我们将从消息正文中收集文件并使用它执行所需的操作。最后,您需要使用新的文本内容设置 Exchange Body:

public class MyProcessor implements Processor {
  public void process(Exchange exchange) throws Exception {
    String myString = exchange.getIn().getBody(String.class);
    myString += System.getProperty("line.separator");
    myString += "Processed with MyProcessor";
    System.out.println("Processed!");
    exchange.getIn().setBody(myString);
  }

  public MyProcessor() {}
}

过滤需要复制的文件

有几种策略可以过滤哪些文件必须从源文件夹复制到目标文件夹。最简单的选择是在“include”参数中包含一个正则表达式(例如文件后缀)。例如,仅包含“*.txt”文件:

public void configure() {
  from("file:src/data?noop=true&include=.*.txt").process(new MyProcessor()).to("file:target/messages/others");
}

使用ProducerTemplate 生成文件内容

如果想将 Camel 源文件与 Route 解耦,可以使用抽象的直接 Camel 组件,使用 ProducerTemplate 类发送文件内容。这是一个基本示例:

public class MainApp {
  public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(
        new RouteBuilder() {
          public void configure() {
            from("direct:transferFile")
                .process(new MyProcessor())
                .to("file:target/messages/others");
          }
        });
    ProducerTemplate producerTemplate = context.createProducerTemplate();
    context.start();
    String fileText =
        new String(Files.readAllBytes(Paths.get("src/data/file.txt")), StandardCharsets.UTF_8);
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Exchange.FILE_NAME, "file.txt");
    producerTemplate.sendBodyAndHeaders("direct:transferFile", fileText, headerMap);
    Thread.sleep(5000);
    context.stop();
  }
}

如您所见,生产者设置为“direct:transferFile”,文件内容由普通 Java 7 函数收集:

String fileText = new String(Files.readAllBytes(Paths.get("src/data/file.txt")), StandardCharsets.UTF_8);

另外,请注意我们正在设置 ProducerTemplate 的 Header 以包含将注入 Route 的实际文件名。

本教程的源代码:https://github.com/fmarchioni/masterspringboot/tree/master/camel/camel-file

相关文章