繁体   English   中英

Apache 骆驼配置路由以创建文件并可选择通过 tcp 发送

[英]Apache Camel configure route to create a file and optionally send it via tcp

我正在 Apache Camel 中配置一个路由,其中有一个 from() 但多个 to() 。 我的任务是创建一个文件(已经有效)并将其放在某个文件夹中(有效)或通过 TCP 发送。

如何定义创建文件的路由,然后将其保存到磁盘或通过 TCP 发送?

我确实尝试过使用创建文件的处理器,但我只想创建一次文件,然后使用 camel 来存储或发送。 我希望有人能理解我的话。 目前我正在检查配置属性并在启用两个端点(文件和 tcp)时遇到麻烦,我收到骆驼错误。

我的目标是使用骆驼架构。

@Component
public class HL7DirectImportRouteBuilder extends RouteBuilder {

@Override
public void configure() throws Exception {
    String gdtexportFolder = propertyService.getExportFolderGDT();

    // inspect gdt file and create a hl7 message and send it
    if (propertyService.isExportMessageHl7Enabled()) {
        log.info("export hl7 message (mllp) is enabled....");
        throw new L7Exception("not yet implemented");
        
    
    } else {
        log.info("export hl7 message (mllp) is disabled");
    }

    // the customer wants to have the hl7 messages as file to be exchanged via a
    // shared network folder
    if (propertyService.isExportMessageFileEnabled()) {
        log.info("export hl7 message file is enabled....");
        from("file:" + padsyGdtExportFolder).log(
                "File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelFileLastModified}")
                .process(new GDTFileProcessor());

    } else {
        log.info("export hl7 message file is disabled");
    }

    
    if (propertyService.isExportPdfFileEnabled()) {
        log.info("export pdf file is enabled....");
        from("file:" + padsyGdtExportFolder).log(
                "File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelFileLastModified}")
                .process(new CopyPDFFileProcessor(propertyService));
    } else {
        log.info("export pdf file is disabled");
    }
}

编辑:添加编码并删除机密部分。

我不知道如何根据应用程序的配置动态更改路由

我想在伪代码中拥有什么: from(file:input).createMessage() // 将文件放在磁盘上或发送它 // 或者只是复制文件

您可以使用骆驼文件生产者端点创建文件,例如。 to(file:path?filenName=example.txt) 该文件将以消息正文作为其内容。 当您创建文件时,可以使用消费者端点进行控制,消费者端点可能是很多东西,例如计时器、计划、消息队列事件等。

使用定时器的例子:

from("timer:createFileTimer?repeatCount=1")
    .routeId("createFileTimer")
    .setBody(constant("Hello world"))
    .to("file:{{input.path}}?fileName=Hello.txt")
;        

这将创建一个文件并使用 spring 引导使用的 application.properties 文件的 input.path 属性中定义的路径将其保存到磁盘

要将文件从一个位置移动到另一个位置,您可以简单地使用:

from("file:{{input.path}}?delete=true")
    .routeId("moveFileToPath")
    .to("file:{{output.path}}")
;

请注意,从技术上讲,这将从 input.path 读取文件并将其写入 output.path。 使用delete=true这将在完成后从 input.path 中删除原始文件。

要使用 TCP 发送文件,您可以使用FTP 组件,最好使用 SFTP,使用强 RSA 私钥进行身份验证。

from("file:{{input.path}}?delete=true")
    .routeId("createFileTimer")
    .to("sftp:{{sftp.host}}:{{sftp.port}}" 
        + "?username={{sftp.username}}&privateKeyFile={{sftp.keyfile.path}}")    
;

Spring 引导的 Camel-ftp maven 依赖项:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-ftp-starter</artifactId>
  <version>x.x.x</version>
</dependency>

或者只是复制文件

如果您使用的是文件消费者端点( from("file:path") ,需要了解的一件关键事情是它会在路由处理完文件后使用该文件。这意味着该文件将默认移动到 hidden .camel input.path 目录中的.camel文件夹,以防止它再次被使用。如果您的目标是复制文件,这可能会导致意外行为。

使用noop=true设置将防止 camel 移动或删除原始文件,并使用内存中的 IdempotentRepository 来跟踪已消耗的文件以防止它们再次被消耗。

要将已处理的文件移动到自定义位置,您可以将move=/some/path/for/processed/files/${file:name}设置与文件消费者端点一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM