我如何在骆驼路线中使用 java 布尔条件?

How do i use java boolean condition in camel route?

我正在使用 camel 将文件从一个端点传输到另一个端点。我正在启动多个路由,其中​​一些路由需要解密文件。我如何根据布尔条件在特定路由中使解组过程可选?

from(source)
    .choice()
    .when(isEncrypted())) //Java boolean value 
    .unmarshal(decrypt(pgpEncryptionDetails)) 
    .endChoice()
to(destination);

PGPDataFormat decrypt(PGPEncryptionDetails pgpEncryptionDetails) {
    PGPDataFormat pgpDataFormat = new PGPDataFormat();
    pgpDataFormat.setKeyFileName(pgpEncryptionDetails.getPrivateKeyPath());
    pgpDataFormat.setPassword(pgpEncryptionDetails.getPassphrase());
    pgpDataFormat.setKeyUserid(pgpEncryptionDetails.getUserId());
    return pgpDataFormat;
}

我知道如何用一个简单的表达式来处理,但这里我的条件不依赖于交换。

它对我有用。

from(source)
    .process(exchange -> decryptIfEncrypted(pgpEncryptionDetails))
to(destination);

ProcessDefinition decryptIfEncrypted(PGPEncryptionDetails pgpEncryptionDetails) {
    if (isPgpEncryped()) {
        PGPDataFormat pgpDataFormat = new PGPDataFormat();
        pgpDataFormat.setKeyFileName(pgpEncryptionDetails.getPrivateKeyPath());
        pgpDataFormat.setPassword(pgpEncryptionDetails.getPassphrase());
        pgpDataFormat.setKeyUserid(pgpEncryptionDetails.getUserId());
        return new ProcessDefinition().unmarshal(pgpDataFormat);
    }
    return new ProcessDefinition();
}

您可以使用方法调用表达式来调用 java bean 上实现谓词的方法

public class Foo {
  public boolean isSomething(Object body) {
    ... return true or false
  }
} 

然后使用Camel路由中的方法

when(method(Foo.class, "isSomething"))