Apache Camel在使用setBody插入数据之前进行验证

问题描述 投票:1回答:1

我有如下的RouteBuilder。

from("seda:requestQueue").routeId("service_request").log(LoggingLevel.INFO, "Processing STARTED,  {body = ${body}")
            .setBody(body())
            .to("select * from SERVICE_REQUEST WHERE requestType=:#TYPE AND requestDate=:#date AND owner=:#OWNER)
            .split(body()).streaming().process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    Map<String, Object> row = exchange.getIn().getBody(Map.class);
                    if (row == null) {
                        LOGGER.info("Request is new. No records found.");
                        return;
                    }
                    //Duplicate request. Q(Not sure how to terminate the process with exception)
                }
            })
            .log(LoggingLevel.INFO, "Processing CONTINUE,  {body = ${body}")
            .setBody(body())
            .to("insert into SERVICE_REQUEST (ID,....) ").log(LoggingLevel.INFO, "Processing COMPLETED").end();

我想实现

  1. 无论何时提交请求(目前通过SEDA),首先检查数据库中是否有相同的请求。
  2. 如果它不可用则只插入数据库(新行)

问题:1。如何设置原始请求体到insertQuery?根据上面的代码,在seda:requestQueue收到的正文不可用于(“插入到SERVICE ..”)。

apache apache-camel
1个回答
0
投票

您的拆分器将发送带有新主体的消息(基于SQL)。所以,身体本身不能使用。

相反,在调用拆分器之前,将主体设置为Exchange的属性。然后,当您需要使用它时,请读取属性的值。

.setProperty("mySampleProperty", simple("${body}")

如果你需要它作为正文,那么 - 在那一点 - 将正文设置为您以前存储在Exchange属性中的值。

这是一个类似的问题:Apache Camel: how store variable for later use

© www.soinside.com 2019 - 2024. All rights reserved.