【问题标题】:Possible to initialize a bindy (Apache Camel DataFormat - FixedLength and use it in the same route可以初始化绑定(Apache Camel DataFormat - FixedLength 并在同一路由中使用它
【发布时间】:2016-06-07 10:48:21
【问题描述】:

我的输入文件包含几种类型的 FixedLengthRecord,所以我有很多 FixedLengthDataFormat 来解组每个帖子。

  1. 我每行拆分正文
  2. 首先我应该知道我应该使用哪种 DataFormat,然后创建一个对象
  3. 然后解组

类似这样的:

from(myURI)
    .split().tokenize("\n")
        .process(initializeMyBindyDataFormat)
        .unmarshal(bindy)
    .end();

但我的问题是,当我通过进程初始化该绑定对象时,我得到了 NPE。 但是,如果我在路由定义之前(从之前)创建一个绑定对象,它将可以正常工作。我的 bindy 对象依赖于 body,我无法在路由定义之前对其进行初始化。 实际上 Apache Camel 在开始路由之前会初始化绑定对象

【问题讨论】:

    标签: apache-camel fixed-length-record bindy


    【解决方案1】:

    答案是使用 .inout 由于我想在另一条路线上解组,一个简单的例子应该如下:

    from(myURI)
        .split().tokenize("\n")
            .inout("direct:unmarshalSpecificRow")
        .end();
    
    from(direct:unmarshalSpecificRow")
        .choice()
            .when(firstPredicate)
               unmarshal(new BindyFixedLengthDataFormat(package1)
            .when(secondPredicate)
               unmarshal(new BindyFixedLengthDataFormat(package1)
            .when(thirdPredicate)
               unmarshal(new BindyFixedLengthDataFormat(package1)
            .otherwise()
               .throwException(new IllegalArgumentException("Unrecognised post")
        .end();
    

    感谢jakub-korab 的帮助。

    【讨论】:

      【解决方案2】:

      在这种情况下,我认为最好将您的处理分成两个步骤。

      1. 接收不同数据的主路由。在这里,您定义确定它是什么类型的主体的谓词规则。检查身体的开始,或者确定它是这种类型或那种类型的东西。添加一个choice() when() 并根据哪个谓词设置为true,将其设置为单独的路由。

      2. 在辅助路由中添加特定的绑定格式并执行您的编组/解组工作。

      文档中的一个示例:

      Predicate isWidget = header("type").isEqualTo("widget");
      from("jms:queue:order")
         .choice()
            .when(isWidget).to("bean:widgetOrder")
            .when(isWombat).to("bean:wombatOrder")
         .otherwise()
            .to("bean:miscOrder")
         .end();  
      

      http://camel.apache.org/predicate.html

      【讨论】:

      • 感谢您的回答,这几乎是解决方案。不过有一点,我会详细回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多