【问题标题】:Setting Camel exchange property from inside a processor从处理器内部设置 Camel 交换属性
【发布时间】:2019-03-06 01:14:49
【问题描述】:

Java 8 和 Camel 2.19.x 在这里。我有以下骆驼路线:

<route id="widgetProcessing">
  <from uri="activemq:inputQueue"/>
  <to uri="{{widgetFetcher}}"/>
</route>

还有widgetFetcher 处理器:

@Component("widgetFetcher")
public class WidgetFetcher {
  private WidgetDao widgetDao;

  public WidgetFetcher(WidgetDao widgetDao) {
    this.widgetDao = widgetDao;
  }

  public Widget getWidgetToProcess() {
    // get the next widget id from the database
    final Integer firstWidgetId = widgetDao.getFirstSubmittedWidgetId();

    // Do lots of stuff with 'firstWidgetId' down here...
  }
}

我想在&lt;from&gt;之后和WidgetFetcher之前创建一个交换属性,并将该属性的初始值设置为null;然后有条件地将其值设置为WidgetFetcher 内部的其他值。此外,我希望这个重新分配的值在剩余的路线/处理中“坚持”。所以像:

<route id="widgetProcessing">
  <from uri="activemq:inputQueue"/>

  <setProperty propertyName="fizzId">
    <constant>null</constant>
  </setProperty>

  <to uri="{{widgetFetcher}}"/>

  <log message="fizzId = ${property[fizzId]}" loggingLevel="ERROR"/>
</route>

然后:

public Widget getWidgetToProcess(@ExchangeProperty("fizzId") final String fizzId) {
  // get the next widget id from the database
  final Integer firstWidgetId = widgetDao.getFirstSubmittedWidgetId();

  if (someMethodReturnsTrue()) {
    // Does this actually get saved outside the 
    log.info("About to update fizzId...")
    fizzId = UUID.randomUUID().toString();
  }

  // Do lots of stuff with 'firstWidgetId' down here...
}

但是在运行时,本地分配 fizzId = ... 似乎并没有像日志输出显示的那样:

About to update fizzId...
fizzId = null

所以我认为我的处理器正在接收fizzId 交换属性的副本,但重新分配其值内联实际上并没有修改路由其余部分的实际值。 知道如何做到这一点吗?

【问题讨论】:

    标签: java apache-camel


    【解决方案1】:

    接受交换而不是将属性传递给处理器,然后您可以在交换上设置属性。

    【讨论】:

      【解决方案2】:

      您可能需要参考更高级别的内容来设置值。尝试对完整的属性映射 @Properties 使用注释,或者让您的 WidgetFetcher 实现处理器以获取对完整交换的引用。

      参考:Camel annotations

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-18
        • 1970-01-01
        相关资源
        最近更新 更多