java - 从处理器内部设置 Camel 交换属性
问题描述
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...
}
}
我想在 之后<from>
和之前创建一个交换属性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
但是重新分配它的值内联实际上并没有修改路由其余部分的实际值。关于如何做到这一点的任何想法?
解决方案
不要将属性传递给处理器,而是接受交换 - 然后您可以在交换上设置属性。
推荐阅读
- javascript - Paypal Javascript SDK订阅按钮和购买按钮在同一页面上
- javascript - 如何在 js 浏览器模块中动态获取导入的对象
- android - PopupWindow Y 位置错误
- django - 没有这样的表:django_session 在 azure web 应用程序上
- c++ - C++:检查存储在 std::any 中的值是否是整数
- c++ - 在简单代码演示中使用 std::thread 时有关重载函数的 C++ 编译错误
- c# - 等待不同线程上的调用时会发生什么
- excel - 在 VBA 中使用 SUMPRODUCT 公式的变体
- html - 修复/改进响应式 CSS 网格
- npm - 纱线:当文件在那里时 MODULE_NOT_FOUND