【发布时间】:2017-11-06 21:22:25
【问题描述】:
我正在使用 Apache Camel 从使用 sftp 的服务器中提取文件。我的代码是从
sftp://example.com:22/DEV139/Uploads/Alamo?username=admin&password=admin&binary=true&disconnect=true&delete=true
它可以成功地从这个目录中抓取文件,因为我可以在我的日志中看到文件内容,但是它会抛出以下异常。
2017-11-06 16:18:42,498 |错误 | || FromAlamoSFTP => [ msgtyp:AlamoCSV msgdst:CDX ] || java.lang.IllegalStateException:无效的 BundleContext。 在 org.apache.felix.framework.BundleContextImpl.checkValidity(BundleContextImpl.java:514) 在 org.apache.felix.framework.BundleContextImpl.getBundle(BundleContextImpl.java:112) 在 org.apache.camel.core.osgi.OsgiClassResolver.resolveClass(OsgiClassResolver.java:58) 在 org.apache.camel.component.bean.BeanHelper.isAssignableToExpectedType(BeanHelper.java:121) 在 org.apache.camel.component.bean.BeanInfo.matchMethod(BeanInfo.java:1074) 在 org.apache.camel.component.bean.BeanInfo.removeNonMatchingMethods(BeanInfo.java:999) 在 org.apache.camel.component.bean.BeanInfo.chooseMethod(BeanInfo.java:566) 在 org.apache.camel.component.bean.BeanInfo.createInvocation(BeanInfo.java:254) 在 org.apache.camel.component.bean.BeanInfo.createInvocation(BeanInfo.java:185) 在 org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:159) 在 org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) 在 org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:121) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:83) 在 org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:63) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:171) 在 org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:454) 在 org.apache.camel.component.file.remote.RemoteFileConsumer.processExchange(RemoteFileConsumer.java:137) 在 org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:226) 在 org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:190) 在 org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175) 在 org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 在 java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 在 java.lang.Thread.run(Thread.java:748)
我的代码如下。
public class FromAlamoRouteBuilder extends RouteBuilder implements
InitializingBean, DisposableBean {
@EnforceInitialization
private String DEAD_LETTER_MQ = "";
@EnforceInitialization
private String ALAMO_SFTP_SOURCE;
@EnforceInitialization
private String DESTINATION_QUEUE;
@EnforceInitialization(preventZero = false)
private int exceptionMaximumRedeliveries;
@EnforceInitialization(preventZero = false)
private int exceptionMaximumRedeliveriesDelay;
public void configure() throws Exception {
onException(Exception.class)
.handled(true)
.retriesExhaustedLogLevel(LoggingLevel.ERROR)
.retryAttemptedLogLevel(LoggingLevel.WARN)
.maximumRedeliveries((int) exceptionMaximumRedeliveries)
.redeliveryDelay((int) exceptionMaximumRedeliveriesDelay)
.bean("logging", "error(*, '${exception.stacktrace}')")
.to(DEAD_LETTER_MQ)
.stop();
/*
* The exchange body is expected to be an Alamo CSV file
*/
from(ALAMO_SFTP_SOURCE)
.routeId("FromAlamoSFTP")
.setExchangePattern(ExchangePattern.InOnly)
.bean("logging", "info(*, 'Received message " + ALAMO_SFTP_SOURCE + "[${body}]')")
.setHeader(MsgHdr.MESSAGE_TYPE, constant("AlamoCSV"))
.setHeader(MsgHdr.MESSAGE_DESTINATION, constant("CDX"))
.bean("logging", "info(*, 'Attempting to place Alamo msg on DESTINATION QUEUE " + DESTINATION_QUEUE + "')")
.to(DESTINATION_QUEUE)
.bean("logging", "info(*, 'Successfully placed Alamo msg on " + DESTINATION_QUEUE + "')");
}
public void setDeadLetterQueue(String dEAD_LETTER_MQ) {
DEAD_LETTER_MQ = dEAD_LETTER_MQ;
}
public void setExceptionMaximumRedeliveries(int exceptionMaximumRedeliveries) {
this.exceptionMaximumRedeliveries = exceptionMaximumRedeliveries;
}
public void setExceptionMaximumRedeliveriesDelay(
int exceptionMaximumRedeliveriesDelay) {
this.exceptionMaximumRedeliveriesDelay = exceptionMaximumRedeliveriesDelay;
}
public void setDestinationQueue(String queue){
this.DESTINATION_QUEUE = queue;
}
public void setAlamoSftpSource(String bcRelease){
this.ALAMO_SFTP_SOURCE = bcRelease;
}
public void afterPropertiesSet() throws Exception {
System.out.println("FromAlamoRouteBuilder is starting up");
FieldInitialization.checkAllFields(this);
}
public void destroy() throws Exception {
System.out.println("FromAlmoRouteBuilder is shutting down");
}
}
【问题讨论】:
-
您确定需要使用 SFTP 吗? SFTP 是 SSH 的一部分,它在端口 22 而不是 21 上运行。21 是 FTP 的标准端口,因此它实际上可能是您应该使用的 FTPS(显式)。
-
我将端口更改为 21 以在堆栈溢出时隐藏服务器信息,因为我不想泄露服务器凭据。但我确实在我的代码中使用了 22。
-
奇怪的是我能够成功抓取文件然后我得到这个错误。
-
为了未来。使用
www.example.com作为服务器名称。此域保留用于文档和其他用例(包括您的)。所以你可以保持相关的东西(比如你正在使用的端口)完好无损。 -
这是完整的堆栈跟踪还是您省略了“Caused by”-blocks?
标签: java apache-camel osgi apache-karaf