为什么我的属性没有在此骆驼 sftp 路由中解析?

Why are my properties not resolved in this camel sftp route?

我正在创建一个从本地服务器读取文件的骆驼路线,添加一些 headers 然后将内容写入另一个远程 sftp 服务器上的文件。

我动态设置的 headers 之一是登录最终服务器所需的用户名。但是,当连接发生时,我可以看到我设置的用户名未用于登录,而是使用文字值“${in.header.senderTargetUserName}”。 属性 未解决。

这是代码:

@Override
public void configure() throws Exception {

    from(fromWorkInDirectoryEndpoint())
                    .transacted()
                    .process(new SetCustomFileName())
                    .process(new SetSenderTargetUsername())
                    .to(targetProvideFileEndpoint());

    private String targetProvideFileEndpoint() {
        return "sftp://${in.header.senderTargetUserName}@{{target.sftp-core.server}}/{{target.environment.inDirectory}}?disconnect=true&privateKeyUri=/certificate/private.key&fileName=${in.header.customFileName}";
    }
}

public class SetCustomFileName implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {

            final String userId = exchange.getIn().getHeader(CAMEL_FILE_NAME, String.class).replaceAll("/" + sentFileName, "");

            exchange.getIn().setHeader(CUSTOM_FILE_NAME, "a_custom_file_name.txt");
            exchange.getIn().setHeader(USER_ID, userId);
    }
}

public class SetSenderTargetUsername implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        final String userId = exchange.getIn().getHeader(USER_ID, String.class);

        User user = userService.findOne(userId);
        exchange.getIn().setHeader("senderTargetUserName", user.getUserNameThatShouldBeUsedOnSftp());
    }
}

我得到这个异常:

Exchange
---------------------------------------------------------------------------------------------------------------------------------------
Exchange[
    Id                  ID-DESKTOP-HRMD8N6-54673-1455104133665-0-406
    ExchangePattern     InOnly
    Headers             {customFileName=a-custom-filename.txt, userId=6bb37a77-6f74-4410-8043-26f96ce06be0, senderTargetUserName=USERNAME}
    BodyType            org.apache.camel.component.file.remote.RemoteFile
    Body                [Body is file based: RemoteFile[6bb37a77-6f74-4410-8043-26f96ce06be0/FI.DIMN.108853.20160210.00013.T.1.1]]
]

2016-02-10 13:10:13.550 DEBUG 5356 --- [lan.be/work/in/] o.a.c.c.file.remote.RemoteFileProducer   : Could not connect to: Endpoint[sftp://$%7Bin.header.senderTargetUserName%7D@destinationFileServer:8520/INTEST?disconnect=true&fileName=%24%7Bin.header.customFileName%7D&privateKeyUri=%2Fcertificate%2Fprivate.key]. Will try to recover.

org.apache.camel.component.file.GenericFileOperationFailedException: Cannot connect to sftp://${in.header.senderTargetUserName}@sftp.socialsecurity.be:8022
    at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:146)
    at org.apache.camel.component.file.remote.RemoteFileProducer.connectIfNecessary(RemoteFileProducer.java:202)
    at org.apache.camel.component.file.remote.RemoteFileProducer.recoverableConnectIfNecessary(RemoteFileProducer.java:172)
    at org.apache.camel.component.file.remote.RemoteFileProducer.preWriteCheck(RemoteFileProducer.java:126)
    at org.apache.camel.component.file.GenericFileProducer.processExchange(GenericFileProducer.java:113)
    at org.apache.camel.component.file.remote.RemoteFileProducer.process(RemoteFileProducer.java:56)
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
    at org.apache.camel.processor.SendProcessor.doInAsyncProducer(SendProcessor.java:157)
    at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:304)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:152)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:329)
    at org.apache.camel.processor.DefaultErrorHandler.process(DefaultErrorHandler.java:56)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:165)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
    at org.apache.camel.spring.spi.TransactionErrorHandler.processByErrorHandler(TransactionErrorHandler.java:218)
    at org.apache.camel.spring.spi.TransactionErrorHandler.process(TransactionErrorHandler.java:99)
    at org.apache.camel.spring.spi.TransactionErrorHandler.process(TransactionErrorHandler.java:112)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
    at org.apache.camel.spring.spi.TransactionErrorHandler.processByErrorHandler(TransactionErrorHandler.java:218)
    at org.apache.camel.spring.spi.TransactionErrorHandler.doInTransactionWithoutResult(TransactionErrorHandler.java:181)
    at org.springframework.transaction.support.TransactionCallbackWithoutResult.doInTransaction(TransactionCallbackWithoutResult.java:34)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at org.apache.camel.spring.spi.TransactionErrorHandler.doInTransactionTemplate(TransactionErrorHandler.java:174)
    at org.apache.camel.spring.spi.TransactionErrorHandler.processInTransaction(TransactionErrorHandler.java:134)
    at org.apache.camel.spring.spi.TransactionErrorHandler.process(TransactionErrorHandler.java:103)
    at org.apache.camel.spring.spi.TransactionErrorHandler.process(TransactionErrorHandler.java:112)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:165)
    at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:439)
    at org.apache.camel.component.file.remote.RemoteFileConsumer.processExchange(RemoteFileConsumer.java:137)
    at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:211)
    at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:175)
    at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174)
    at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:512)
    at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:118)
    ... 48 common frames omitted

当我在最终代码抛出异常之前设置断点时,我可以看到登录用户名设置为 ${in.header.senderTargetUserName}。

大家也可以看到,camel 显示的是异常前的最新交易所。该交易所的 headers 包含我期望的值。

有什么想法吗?

好的,如果我理解正确的话,由于 header.

的注入,您的 "from" 端点实际上是一个动态端点

来自http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html

This is not valid code

This snippet is not valid code. Read on.
.to("freemarker://templateHome/${body.templateName}.ftl")

这与您想要的类似,只是您使用的是 header 和一个来自端点。

文档说,如果您使用低于 2.16 的 Camel,则应该使用收件人列表,例如

.recipientList(simple("sftp://${in.header.senderTargetUserName}@{{target.sftp-core.server}}/{{target.environment.inDirectory}}?disconnect=true&privateKeyUri=/certificate/private.key&fileName=${in.header.customFileName}"))

http://camel.apache.org/recipient-list.html 您可以在此处的动态收件人列表下找到示例。

请注意,我尚未测试该端点。

使用动态到而不是普通到,路线将开始工作:

    from(fromWorkInDirectoryEndpoint())
                .transacted()
                .process(new SetCustomFileName())
                .process(new SetSenderTargetUsername())
                .toD(targetProvideFileEndpoint());

从 Camel 开始可用:2.16 https://camel.apache.org/message-endpoint.html

对于旧版本,请按照 Souciance 的建议使用 recipientList。