Apache Camel在Exchange标头中设置路由异常



我正在使用Camel sql组件,并希望在事务失败时使用onConsumeFailed来更新具有异常stacktrace的记录。

表格结构:如果不存在则创建表库存(itemnbr integer NOT NULL DEFAULT nextval('eventory_itemnbr_seq'::regclass(,位置整数,loctype字符变化(2(,颜色特征变化(5(,品牌特征变化(5(,soh双精度,read整数DEFAULT 0,异常字符变化(500(,CONSTRAINT inventory_pkey主键(itemnbr()

在我的骆驼路线中,我使用onConsumeFailed选项,如下

sql://<select statement>?dataSource=#dataSource&onConsumeFailed=update inventory set camel_is_read = 0, exception=:#exception where itemNbr= :#itemNbr

我在路由上创建了onException,如下所示,并将根本原因设置为标头属性";异常";像

onException(Exception.class).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
Throwable.class);
exchange.getIn().setHeader("exception", ex.getCause());
}
});

当事务发生异常时,在更新行时抛出以下错误

org.apache.camel.RuntimeExchangeException:Cannot find key [exception] in message body or headers to use when setting named parameter in query

在路由执行期间,我们如何从路由访问头属性?

您可以使用简单的表达式语言来获取异常

exception=:#${exception}

https://camel.apache.org/components/next/languages/simple-language.html