【问题标题】:Calling a camel route using Producer Template使用生产者模板调用骆驼路线
【发布时间】:2022-02-04 01:21:40
【问题描述】:

我的用例基于我需要从不同的源系统获取或移动文件到目标系统的其余控制器输入。

路线:-

@Component
public class MoveFile extends RouteBuilder {
@override
public void configure() throws Exception {

from("file:tmp/${header.inPath}")
    .to("file:/tmp${header.outPath}?fileName=${header.fileName}")
    .setBody().constant("File - ${header.inPath}/${header.fileName} Moved Succesfully")

}
}

我的休息控制器将沿 getMapping 传递 jobName 以调用此特定路由 inPath 、 outPath 和文件名

@Resource(name=RouteProperties)
private Prosperties props;

@GetMapping("/runJob/{jobToInvoke}")
public String runJob (@PathVariable final String jobToInvoke){
String inPath=props.getProperty("inPath"+jobToInvoke)
String outPath=props.getProperty("outPath"+jobToInvoke)
String fileName=props.getProperty("fileName"+jobToInvoke)

String jobStatus = ProducerTemplate.withHeader("inPath",inPath)
                   .   
                   .
                   .to(??)
                   .request(String.class)
}

我需要帮助才能使用 Producer Template 来传递使用 to 的属性? 我在谷歌上尝试了一些搜索,但在 youtube (link) 中有一个示例,但在那个视频中它正在调用 uri , (Direct:sendMessage) 并且从路线中也有。 在这种情况下如何处理? 提前致谢

【问题讨论】:

    标签: apache-camel spring-batch spring-camel


    【解决方案1】:

    可以从 Java 代码以编程方式调用以 direct: 端点开头的路由。在路由中,pollEnrich 组件调用消费者端点来读取文件并将交换消息正文替换为文件内容。

    from("direct:start")
        .pollEnrich().simple("file:/tmp?fileName=${header.inPath}")
        .toD("file:/tmp?fileName=${header.outPath}")
        .setBody().simple("File - ${header.inPath} Moved Successfully");
    

    从 Java 代码调用路由:

    String jobStatus = producerTemplate.withHeader("inPath", inPath)
        .withHeader("outPath", outPath)
        .to("direct:start")
        .request(String.class);
    

    【讨论】:

    • 感谢 sn-p。将尝试此解决方案。
    【解决方案2】:

    我不知道from 中的这些动态文件URI 是否有效,但至少Camel File documentation 状态

    另外,起始目录不能包含动态表达式 ${ } 占位符。再次使用 fileName 选项指定 文件名的动态部分。

    所以文档建议更改

    from("file:tmp/${header.inPath}")
    

    进入

    from("file:tmp?fileName=${header.inPath}")
    

    fileName 选项可以是相对路径(不仅仅是文件名)。

    如果该更改对您有效,那么您的问题就会过时,因为路由 URI 不再是动态的。

    .withHeader("inPath",inPath)
    .to("file:tmp")
    

    【讨论】:

    • 会尝试相同的。感谢您提供信息。
    猜你喜欢
    • 2018-01-27
    • 2018-09-05
    • 2012-10-27
    • 2023-03-03
    • 1970-01-01
    • 2014-08-22
    • 2020-06-09
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多