【问题标题】:Apache Camel Seda Route still modifies my main routes bodyApache Camel Seda Route 仍然修改了我的主要路线主体
【发布时间】:2018-03-15 09:46:18
【问题描述】:

我有我正在研究的骆驼路线。该路由将 CSV 文件从 sftp 中的一个目录带到 sftp 中的另一个目录,同时执行到 XML 的转换。

from(mySftp.getUri("/camel"))
   .choice()
       .when(body().isNull())
           .log("No Files Found")
       .otherwise()
           .process(new Processor() {
                StringBuilder sb = new StringBuilder();
                public void process(Exchange exchange) throws Exception {
                String body =  exchange.getIn().getBody(String.class).toString();
                String [] lines = body.split("\n");
                for(String line : lines) {
                      String [] fields = line.split(",");
                      //trasformation here
                }
                exchange.setProperty("generatedXml", sb.toString());
                }
}).to(mySftp.getUri("/camel/archive"))

这工作得很好,直到我调用seda route 我已经定义了它的目的是通过设置正文和所需的标头来发送 SNS。

代码如下。

from("seda:sendSNS")
.setBody().simple("message")
.setHeader("CamelAwsSnsSubject", simple("subject"))
.to(myInfoSns.getUri());

这就是我使用“to”来称呼我的seda route的方式

 from(mySftp.getUri("/camel"))
   .to("seda:sendSNS")
   .choice()
       .when(body().isNull())
           .log("No Files Found")
       .otherwise()
           .process(new Processor() {
                StringBuilder sb = new StringBuilder();
                public void process(Exchange exchange) throws Exception {
                String body =  exchange.getIn().getBody(String.class).toString();
                String [] lines = body.split("\n");
                for(String line : lines) {
                      String [] fields = line.split(",");
                      //trasformation here
                }
                exchange.setProperty("generatedXml", sb.toString());
                }
}).to(mySftp.getUri("/camel/archive"))

我期待虽然我调用了 seda 路线并将其主体设置在其中,但这不应该影响我的主要路线的主体。似乎我的 XML 已成功生成,但随后我的主要路线未能将文件移动到所需的目的地。

我得到的错误是

Exhausted after delivery attempt: 1 caught: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: camel/archive/file.csv

No body available of type: java.io.InputStream but has value: RemoteFile[file.csv] of type: org.apache.camel.component.file.remote.RemoteFile on: file.csv. Caused by: Error during type conversion from type: java.lang.String to the required type: java.io.InputStream with value [Body is file based: \tmp\file.csv] due \tmp\file.csv (The system cannot find the file specified). Exchange[ID-IT1289-1521106847220-0-1]. Caused by: [org.apache.camel.TypeConversionException - Error during type conversion from type: java.lang.String to the required type: java.io.InputStream with value [Body is file based: \tmp\file.csv] due \tmp\file.csv (The system cannot find the file specified)]

任何想法可能是什么原因?为什么我拨打seda route "sendSNS"后找不到我的文件。

【问题讨论】:

  • 你想要 seda 还是窃听?
  • 我尝试过窃听,虽然当我在一个choice() 中使用窃听时,我被阻止了The method when(Predicate) is undefined for the type WireTapDefinition<ChoiceDefinition>
  • 您是否有条件地路由到"seda:sendSNS"?如果是,条件是什么?否则,你为什么在when中使用它?
  • @ErnestKiwele 在将文件发送到存档之前,我希望发送另一个 XML 生成成功的 sns.....wireTap("seda:sendSNS") .choice() .when(body().isNull()) .log("No Files Found") .otherwise() .process(new Processor() { sb = new StringBuilder(); //transofmration exchange.setProperty("generatedXml", sb.toString()); } }) .wireTap("seda:sendSNS") .to(mySftp.getUri("/camel/archive")) .endChoice()

标签: java apache-camel


【解决方案1】:

您的意图是将消息的副本发送到seda 端点,因此您需要的集成是wireTap

from(mySftp.getUri("/camel"))
  .wireTap("seda:sendSNS")
  .choice()
  //the rest...

相关文档为here

Wire Tap(来自 EIP 模式)允许您将消息路由到一个单独的位置,同时将它们转发到最终目的地。

【讨论】:

  • 我试过这个,虽然当我在一个choice()中使用窃听时我被阻止了The method when(Predicate) is undefined for the type WireTapDefinition<ChoiceDefinition>
  • 使用 endChoice 返回选择(例如读作结束此块并返回选择)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
相关资源
最近更新 更多