1

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

我动态设置的标题之一是登录最终服务器所需的用户名。但是,当连接发生时,我可以看到我设置的用户名不用于登录,而是使用文字值“${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$2.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$1.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.access$301(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 显示了它在异常之前的最新交换。该交换的标头包含我期望的值。

有任何想法吗?

4

2 回答 2

1

好的,所以如果我理解正确,由于标头的注入,您的“来自”端点实际上是一个动态端点。

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")

这与您想要的类似,只是您使用的是标头和来自端点。

文档说如果您使用低于 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 您可以在动态收件人列表下找到示例。

注意,我没有测试过那个端点。

于 2016-02-10T13:03:43.053 回答
1

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

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

这在 Camel 中可用:2.16 https://camel.apache.org/message-endpoint.html

对于旧版本,请按照 Souciance的建议使用收件人列表。

于 2016-02-11T08:13:41.097 回答