骆驼:Choice 子句中的 doTry

Camel: doTry in the Choice clause

同事们,我正在尝试使用 choicedoTry 配置 Camel 路由。 我的问题是第二个 when 子句中的 Cannot resolve method 'when'。 问题:是否可以在 choice 子句中使用 doTry 结构?

路线如下:

public void configure() {
        from("sftp://.....").choice()
            .when(header("CamelFileName").contains("XML P")).log("Validate ${file:name}").bean(new MessProcessor(), "logInputMessDetails")
             .doTry().
                    to("validator:file:C:\Users...\xsds\p.xsd").log("${file:name} is valid")
             .doCatch(ValidationException.class)
                 .log("XML P is not valid")
                 .to("file:sftp-inbound\data-invalid\" + header("CamelFileParent").toString())
                 .bean(messProcessor, "stopCamel")
/*The problem is here*/ .when(header("CamelFileName").contains("XML X")).log("Validate ${file:name}").bean(new MessProcessor(), "logInputMessDetails").
            doTry().to("validator:file:C:\Users\....\xsds\position.xsd").log("${file:name} is valid").
            doCatch(ValidationException.class).log("XML X is not valid").to("file:sftp-inbound\data-invalid\" + header("CamelFileParent").toString()).
            bean(messProcessor, "stopCamel")
            .end();

    }

更新

.bean(messProcessor, "stopCamel")删除后的路线:

public void configure() {
        from("sftp://.....").choice()
            .when(header("CamelFileName").contains("XML P")).log("Validate ${file:name}").bean(new MessProcessor(), "logInputMessDetails")
             .doTry().
                    to("validator:file:C:\Users...\xsds\p.xsd").log("${file:name} is valid")
             .doCatch(ValidationException.class)
                 .log("XML P is not valid")
                 .to("file:sftp-inbound\data-invalid\" + header("CamelFileParent").toString())
                 .end;
/*The problem is still here*/ .when(header("CamelFileName").contains("XML X")).log("Validate ${file:name}").bean(new MessProcessor(), "logInputMessDetails").
            doTry().to("validator:file:C:\Users\....\xsds\position.xsd").log("${file:name} is valid").
            doCatch(ValidationException.class).log("XML X is not valid").to("file:sftp-inbound\data-invalid\" + header("CamelFileParent").toString()).
            bean(messProcessor, "stopCamel")
            .end();

    }

更新 另一种变体

public void configure() {

        from("sftp:...}")

                .choice()
                .when(header("CamelFileName").contains("XML_POS")).log("Validate ${file:name}")
                .to("validator:file:C:\pos.xsd").log("${file:name} valid")
                .when(header("CamelFileName").contains("XML_SETS")).log("Validate ${file:name}")
                 .to("validator:file:C:\sets.xsd").log("${file:name} valid").end()
               .onException(Exception.class)
               .process(new FailureResponseProcessor())
               .handled(true);

            /*to("file:sftp-inbound");*/
    }

重点大概是

.bean(messProcessor, "stopCamel")

不会生成支持 when() 方法的对象。

记住:这还是普通的Java。您只能在相应 class 定义的对象上调用这些方法。编译器只是告诉你调用 bean() 确实会导致一些不知道 when.

的结果

所以答案是退后一步,查看 bean() 的确切结果类型,并可能在那里进行不同的调用。

将错误处理与路由分开,

 onException (exception)
   handled (true)
   process failure processor

然后就是路线

from ftp
 to validate