如何从groovy元素访问Camel的属性CamelLoopIndex?

u5rb5r59  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(144)

我有一个带有groovy元素的头文件设置,如下所示:

<setHeader headerName="time_to">
    <groovy>1625761488 - 3600*(properties.resolve(CamelLoopIndex)-1)</groovy>
  </setHeader>
  <setHeader headerName="time_from">
    <groovy>1625761488 - 3600*properties.resolve(CamelLoopIndex)</groovy>
  </setHeader>

但在执行路由时,我得到了如下运行时错误:

groovy.lang.MissingPropertyException: No such property: CamelLoopIndex for class: script16306769092271458244993
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:67)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309)
    at script16306769092271458244993.run(script16306769092271458244993.groovy:1)

因此,问题是如何正确访问CamelLoopIndex

vptzau2j

vptzau2j1#

这里正确的方法是使用exchange.getProperty('CamelLoopIndex')

<setHeader headerName="time_to">
        <groovy>1625761488 - 3600*(exchange.getProperty('CamelLoopIndex')-1)</groovy>
      </setHeader>
      <setHeader headerName="time_from">
        <groovy>1625761488 - 3600*exchange.getProperty('CamelLoopIndex')</groovy>
      </setHeader>

相关问题