我正在创建一个从本地服务器读取文件的camel路由,添加一些标头,然后将内容写入另一个远程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在异常之前显示了其最新的交换。该交换的标题包含我期望的值。
有什么想法吗?
答案 0 :(得分:1)
好的,所以如果我理解你的话,由于注入了标题,你的“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")
除了使用标题和来自端点之外,这与您想要的类似。
文档说如果你使用的是低于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 您可以在动态收件人列表下找到示例。
注意,我还没有测试过该端点。
答案 1 :(得分:1)
使用动态而不是 plain to ,路线将开始工作:
from(fromWorkInDirectoryEndpoint())
.transacted()
.process(new SetCustomFileName())
.process(new SetSenderTargetUsername())
.toD(targetProvideFileEndpoint());
自Camel:2.16 https://camel.apache.org/message-endpoint.html起可用。
对于旧版本,按照 Souciance 的建议使用recipientList。