Spring 集成 DSL | Jch 异常 |正在与 <<Server>> 端口 22 断开连接
Spring Integration DSL | Jch Exception | Disconnecting from <<Server>> port 22
我正在尝试将文件上传到远程 SFTP 服务器。我从同一个
创建了出站流量
@Configuration
public class SftpIntegrationFlow {
@Value("${report-uploader.reportingServer.remoteDirectory}")
private String remoteDirectory;
@Value("${report-uploader.reportingServer.fileEncoding}")
private String fileEncoding;
@Autowired
@Qualifier("cachedSftpSessionFactory")
private CachingSessionFactory<LsEntry> cachedSftpSessionFactory;
@Bean
public IntegrationFlow sftpOutboundFlow() {
return IntegrationFlows
.from("toSftpChannel")
.handle(Sftp.outboundAdapter(this.cachedSftpSessionFactory, FileExistsMode.REPLACE)
.charset(Charset.forName(fileEncoding)).remoteFileSeparator("/")
.remoteDirectory(remoteDirectory).fileNameExpression("headers.get('FILE_NAME')")
.autoCreateDirectory(true).useTemporaryFileName(true).temporaryFileSuffix(".tranferring"))
.get();
}
@Bean(name = "toSftpChannel")
public MessageChannel getMessageChannel() {
return new DirectChannel();
}
}
我的缓存会话工厂看起来像::
@Configuration
public class UploaderSftpConnectionFactoryBuilder {
@Value("${report-uploader.sftp-factory.host}")
private String host = null;
@Value("${report-uploader.sftp-factory.port}")
private Integer port = null;
@Value("classpath:${report-uploader.sftp-factory.privateKey}")
private Resource privateKey = null;
@Value("${report-uploader.sftp-factory.privateKeyPassphrase}")
private String privateKeyPassphrase = null;
@Value("${report-uploader.sftp-factory.user}")
private String user = null;
@Value("${report-uploader.sftp-factory.poolSize}")
private Integer poolSize = null;
@Value("${report-uploader.sftp-factory.sessionWaitTimeout}")
private Long sessionWaitTimeout = null;
private DefaultSftpSessionFactory getSftpSessionFactory() {
DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory();
Optional.ofNullable(this.getHost()).ifPresent(value -> defaultSftpSessionFactory.setHost(value));
Optional.ofNullable(this.getPort()).ifPresent(value -> defaultSftpSessionFactory.setPort(port));
Optional.ofNullable(this.getPrivateKey()).ifPresent(
value -> defaultSftpSessionFactory.setPrivateKey(privateKey));
Optional.ofNullable(this.getPrivateKeyPassphrase()).ifPresent(
value -> defaultSftpSessionFactory.setPrivateKeyPassphrase(value));
Optional.ofNullable(this.getUser()).ifPresent(value -> defaultSftpSessionFactory.setUser(value));
return defaultSftpSessionFactory;
}
@Bean(name = "cachedSftpSessionFactory")
public CachingSessionFactory<LsEntry> getCachedSftpSessionFactory() {
CachingSessionFactory<LsEntry> cachedFtpSessionFactory = new CachingSessionFactory<LsEntry>(
getSftpSessionFactory());
cachedFtpSessionFactory.setPoolSize(poolSize);
cachedFtpSessionFactory.setSessionWaitTimeout(sessionWaitTimeout);
return cachedFtpSessionFactory;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Resource getPrivateKey() {
return privateKey;
}
public void setPrivateKey(Resource privateKey) {
this.privateKey = privateKey;
}
public String getPrivateKeyPassphrase() {
return privateKeyPassphrase;
}
public void setPrivateKeyPassphrase(String privateKeyPassphrase) {
this.privateKeyPassphrase = privateKeyPassphrase;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
服务发起上传::
@Service
public class SftpFileUploaderServiceImpl implements SftpFileUploaderService {
@Value("${report-uploader.reportingServer.remoteDirectory}")
private String remoteDirectory;
@Value("${report-uploader.reportingServer.attemptsInCaseOfFaliure}")
private Integer noOfAttempts = 5;
@Autowired
@Qualifier("toSftpChannel")
private MessageChannel toSftpChannel;
@Autowired
@Qualifier("CachedSftpSessionFactory")
private CachingSessionFactory<LsEntry> CachedSftpSessionFactory;
public static Logger LOG = LoggerFactory.getLogger(SftpFileUploaderServiceImpl.class);
@Override
public boolean uploadFileToSftpServer(String filename, String fileContent) {
LOG.debug("Uploading file '{}' to the sftp server. File content: {}", filename, fileContent);
int i = 0;
// Try to upload file to the SFTP server retry for the configurable
// number of times in case of failure
while (i < noOfAttempts) {
if (i > 0) {
LOG.info("Retrying to uploade file '{}' to the sftp server....Attempt {}.", filename, i + 1);
}
try {
i++;
return this.toSftpChannel
.send(MessageBuilder.withPayload(fileContent).setHeader("FILE_NAME", filename).build());
} catch (Exception e) {
if (i >= noOfAttempts) {
LOG.error("Exception occured while sending file:'{}' to the SFTP server.", filename);
throw e;
}
}
}
LOG.info("Could not upload file Uploading file '{}' to the sftp server in {} attempts. Aborting!!", filename,
noOfAttempts);
return false;
}
private RemoteFileTemplate<LsEntry> getRemoteTemplate() {
return new RemoteFileTemplate<>(this.cachedSftpSessionFactory);
}
}
调用服务时,调试日志显示以下跟踪::
2016-04-27 11:01:12.034 INFO 9460 --- [nio-9056-exec-1] c.s.l.s.rest.FileUploaderResource : Received request to manually initiate report uploading between 2016-04-26T00:00:00Z & 2016-04-27T00:00:00Z.
2016-04-27 11:01:13.337 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : Processing the request for report between 2016-04-26T00:00:00Z & 2016-04-27T00:00:00Z
2016-04-27 11:01:13.339 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : The reportting file 'Test_2016-04-27_2016-04-26_Test.tsv' will be created with the below content::
*************Start*****************
This is only test content.
************End********************
2016-04-27 11:01:13.339 INFO 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : Initializing the file uploading process......
2016-04-27 11:01:13.340 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : Uploading tempFile:'Test_2016-04-27_2016-04-26_Test.tsv.tmp' to server.
2016-04-27 11:01:33.125 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.i.SftpFileUploaderServiceImpl : Uploading file 'Test_2016-04-27_2016-04-26_Test.tsv.tmp' to the sftp server. File content: 1
2016-04-27 11:01:33.893 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Connecting to <<Server_IP>> port 22
2016-04-27 11:01:33.895 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Connection established
2016-04-27 11:01:33.910 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Remote version string: SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.4
2016-04-27 11:01:33.910 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Local version string: SSH-2.0-JSCH-0.1.52
2016-04-27 11:01:33.910 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
2016-04-27 11:01:33.911 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes256-ctr is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes192-ctr is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes256-cbc is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes192-cbc is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
2016-04-27 11:01:33.934 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
2016-04-27 11:01:33.934 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_KEXINIT sent
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_KEXINIT received
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: none,zlib@openssh.com
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: none,zlib@openssh.com
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server:
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server:
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: none
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: none
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client:
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client:
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server->client aes128-ctr hmac-md5 none
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client->server aes128-ctr hmac-md5 none
2016-04-27 11:01:33.941 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_KEXDH_INIT sent
2016-04-27 11:01:33.941 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : expecting SSH_MSG_KEXDH_REPLY
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : ssh_rsa_verify: signature true
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Host '<<Server_IP>>' is known and matches the RSA host key
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_NEWKEYS sent
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_NEWKEYS received
2016-04-27 11:01:33.955 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_SERVICE_REQUEST sent
2016-04-27 11:01:33.960 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_SERVICE_ACCEPT received
2016-04-27 11:01:33.965 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Authentications that can continue: publickey,keyboard-interactive,password
2016-04-27 11:01:33.966 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Next authentication method: publickey
2016-04-27 11:01:33.970 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Disconnecting from <<Server_IP>> port 22
2016-04-27 11:01:33.970 ERROR 9460 --- [nio-9056-exec-1] c.s.l.s.s.i.SftpFileUploaderServiceImpl : Exception occured while sending file:'Test_2016-04-27_2016-04-26_Test.tsv.tmp' to the SFTP server.
更高级别的错误如下所示::
<30>2016-04-27T02:55:05+02:00 docker-1.mscgn.de docker/smile-report-uploader[1098]:
2016-04-27 00:55:05.528 ERROR 1 --- [sk-scheduler-10] o.s.integration.handler.LoggingHandler : org.springframework.messaging.MessagingException: ; nested exception is
org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.dispatcher.AbstractDispatcher.wrapExceptionIfNecessary(AbstractDispatcher.java:133)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:120)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:286)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:245)
at com.sapient.lufthansa.smilereportuploader.service.impl.SftpFileUploaderServiceImpl.uploadFileToSftpServer(SftpFileUploaderServiceImpl.java:56)
at com.sapient.lufthansa.smilereportuploader.service.impl.FileUploaderHandlerImpl.startUploadingProcess(FileUploaderHandlerImpl.java:94)
at com.sapient.lufthansa.smilereportuploader.service.impl.FileUploaderHandlerImpl.handleUploadingRequest(FileUploaderHandlerImpl.java:76)
at com.sapient.lufthansa.smilereportuploader.scheduler.UploadingScheduler.startUploading(UploadingScheduler.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
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: org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is
java.lang.IllegalStateException: failed to create SFTP Session at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:178)
at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:118)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:334)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:211)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:201)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:193)
at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:110)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
... 23 more
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:355)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:49)
at org.springframework.integration.file.remote.session.CachingSessionFactory.createForPool(CachingSessionFactory.java:76)
at org.springframework.integration.file.remote.session.CachingSessionFactory.createForPool(CachingSessionFactory.java:73)
at org.springframework.integration.util.SimplePool.doGetItem(SimplePool.java:188)
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:169)
... 31 more
Caused by: java.lang.IllegalStateException: failed to connect at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:272)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:350)
... 36 more
Caused by: com.jcraft.jsch.JSchException: session is down
at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:762)
at com.jcraft.jsch.Channel.connect(Channel.java:151)
at com.jcraft.jsch.Channel.connect(Channel.java:145)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:267)
... 37 more
我做错了什么?它让我坚持了几天。非常感谢任何帮助。
2016-04-27 11:01:33.966 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Next authentication method: publickey
2016-04-27 11:01:33.970 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Disconnecting from <> port 22
很可能是密钥或密码问题 - 查看服务器日志。
我正在尝试将文件上传到远程 SFTP 服务器。我从同一个
创建了出站流量@Configuration
public class SftpIntegrationFlow {
@Value("${report-uploader.reportingServer.remoteDirectory}")
private String remoteDirectory;
@Value("${report-uploader.reportingServer.fileEncoding}")
private String fileEncoding;
@Autowired
@Qualifier("cachedSftpSessionFactory")
private CachingSessionFactory<LsEntry> cachedSftpSessionFactory;
@Bean
public IntegrationFlow sftpOutboundFlow() {
return IntegrationFlows
.from("toSftpChannel")
.handle(Sftp.outboundAdapter(this.cachedSftpSessionFactory, FileExistsMode.REPLACE)
.charset(Charset.forName(fileEncoding)).remoteFileSeparator("/")
.remoteDirectory(remoteDirectory).fileNameExpression("headers.get('FILE_NAME')")
.autoCreateDirectory(true).useTemporaryFileName(true).temporaryFileSuffix(".tranferring"))
.get();
}
@Bean(name = "toSftpChannel")
public MessageChannel getMessageChannel() {
return new DirectChannel();
}
}
我的缓存会话工厂看起来像::
@Configuration
public class UploaderSftpConnectionFactoryBuilder {
@Value("${report-uploader.sftp-factory.host}")
private String host = null;
@Value("${report-uploader.sftp-factory.port}")
private Integer port = null;
@Value("classpath:${report-uploader.sftp-factory.privateKey}")
private Resource privateKey = null;
@Value("${report-uploader.sftp-factory.privateKeyPassphrase}")
private String privateKeyPassphrase = null;
@Value("${report-uploader.sftp-factory.user}")
private String user = null;
@Value("${report-uploader.sftp-factory.poolSize}")
private Integer poolSize = null;
@Value("${report-uploader.sftp-factory.sessionWaitTimeout}")
private Long sessionWaitTimeout = null;
private DefaultSftpSessionFactory getSftpSessionFactory() {
DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory();
Optional.ofNullable(this.getHost()).ifPresent(value -> defaultSftpSessionFactory.setHost(value));
Optional.ofNullable(this.getPort()).ifPresent(value -> defaultSftpSessionFactory.setPort(port));
Optional.ofNullable(this.getPrivateKey()).ifPresent(
value -> defaultSftpSessionFactory.setPrivateKey(privateKey));
Optional.ofNullable(this.getPrivateKeyPassphrase()).ifPresent(
value -> defaultSftpSessionFactory.setPrivateKeyPassphrase(value));
Optional.ofNullable(this.getUser()).ifPresent(value -> defaultSftpSessionFactory.setUser(value));
return defaultSftpSessionFactory;
}
@Bean(name = "cachedSftpSessionFactory")
public CachingSessionFactory<LsEntry> getCachedSftpSessionFactory() {
CachingSessionFactory<LsEntry> cachedFtpSessionFactory = new CachingSessionFactory<LsEntry>(
getSftpSessionFactory());
cachedFtpSessionFactory.setPoolSize(poolSize);
cachedFtpSessionFactory.setSessionWaitTimeout(sessionWaitTimeout);
return cachedFtpSessionFactory;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Resource getPrivateKey() {
return privateKey;
}
public void setPrivateKey(Resource privateKey) {
this.privateKey = privateKey;
}
public String getPrivateKeyPassphrase() {
return privateKeyPassphrase;
}
public void setPrivateKeyPassphrase(String privateKeyPassphrase) {
this.privateKeyPassphrase = privateKeyPassphrase;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
服务发起上传::
@Service
public class SftpFileUploaderServiceImpl implements SftpFileUploaderService {
@Value("${report-uploader.reportingServer.remoteDirectory}")
private String remoteDirectory;
@Value("${report-uploader.reportingServer.attemptsInCaseOfFaliure}")
private Integer noOfAttempts = 5;
@Autowired
@Qualifier("toSftpChannel")
private MessageChannel toSftpChannel;
@Autowired
@Qualifier("CachedSftpSessionFactory")
private CachingSessionFactory<LsEntry> CachedSftpSessionFactory;
public static Logger LOG = LoggerFactory.getLogger(SftpFileUploaderServiceImpl.class);
@Override
public boolean uploadFileToSftpServer(String filename, String fileContent) {
LOG.debug("Uploading file '{}' to the sftp server. File content: {}", filename, fileContent);
int i = 0;
// Try to upload file to the SFTP server retry for the configurable
// number of times in case of failure
while (i < noOfAttempts) {
if (i > 0) {
LOG.info("Retrying to uploade file '{}' to the sftp server....Attempt {}.", filename, i + 1);
}
try {
i++;
return this.toSftpChannel
.send(MessageBuilder.withPayload(fileContent).setHeader("FILE_NAME", filename).build());
} catch (Exception e) {
if (i >= noOfAttempts) {
LOG.error("Exception occured while sending file:'{}' to the SFTP server.", filename);
throw e;
}
}
}
LOG.info("Could not upload file Uploading file '{}' to the sftp server in {} attempts. Aborting!!", filename,
noOfAttempts);
return false;
}
private RemoteFileTemplate<LsEntry> getRemoteTemplate() {
return new RemoteFileTemplate<>(this.cachedSftpSessionFactory);
}
}
调用服务时,调试日志显示以下跟踪::
2016-04-27 11:01:12.034 INFO 9460 --- [nio-9056-exec-1] c.s.l.s.rest.FileUploaderResource : Received request to manually initiate report uploading between 2016-04-26T00:00:00Z & 2016-04-27T00:00:00Z.
2016-04-27 11:01:13.337 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : Processing the request for report between 2016-04-26T00:00:00Z & 2016-04-27T00:00:00Z
2016-04-27 11:01:13.339 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : The reportting file 'Test_2016-04-27_2016-04-26_Test.tsv' will be created with the below content::
*************Start*****************
This is only test content.
************End********************
2016-04-27 11:01:13.339 INFO 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : Initializing the file uploading process......
2016-04-27 11:01:13.340 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.impl.FileUploaderHandlerImpl : Uploading tempFile:'Test_2016-04-27_2016-04-26_Test.tsv.tmp' to server.
2016-04-27 11:01:33.125 DEBUG 9460 --- [nio-9056-exec-1] c.s.l.s.s.i.SftpFileUploaderServiceImpl : Uploading file 'Test_2016-04-27_2016-04-26_Test.tsv.tmp' to the sftp server. File content: 1
2016-04-27 11:01:33.893 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Connecting to <<Server_IP>> port 22
2016-04-27 11:01:33.895 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Connection established
2016-04-27 11:01:33.910 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Remote version string: SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.4
2016-04-27 11:01:33.910 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Local version string: SSH-2.0-JSCH-0.1.52
2016-04-27 11:01:33.910 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
2016-04-27 11:01:33.911 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes256-ctr is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes192-ctr is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes256-cbc is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : aes192-cbc is not available.
2016-04-27 11:01:33.912 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
2016-04-27 11:01:33.934 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
2016-04-27 11:01:33.934 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_KEXINIT sent
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_KEXINIT received
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: none,zlib@openssh.com
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server: none,zlib@openssh.com
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server:
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server:
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.938 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: none
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client: none
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client:
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client:
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: server->client aes128-ctr hmac-md5 none
2016-04-27 11:01:33.939 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : kex: client->server aes128-ctr hmac-md5 none
2016-04-27 11:01:33.941 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_KEXDH_INIT sent
2016-04-27 11:01:33.941 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : expecting SSH_MSG_KEXDH_REPLY
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : ssh_rsa_verify: signature true
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Host '<<Server_IP>>' is known and matches the RSA host key
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_NEWKEYS sent
2016-04-27 11:01:33.954 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_NEWKEYS received
2016-04-27 11:01:33.955 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_SERVICE_REQUEST sent
2016-04-27 11:01:33.960 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : SSH_MSG_SERVICE_ACCEPT received
2016-04-27 11:01:33.965 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Authentications that can continue: publickey,keyboard-interactive,password
2016-04-27 11:01:33.966 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Next authentication method: publickey
2016-04-27 11:01:33.970 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Disconnecting from <<Server_IP>> port 22
2016-04-27 11:01:33.970 ERROR 9460 --- [nio-9056-exec-1] c.s.l.s.s.i.SftpFileUploaderServiceImpl : Exception occured while sending file:'Test_2016-04-27_2016-04-26_Test.tsv.tmp' to the SFTP server.
更高级别的错误如下所示::
<30>2016-04-27T02:55:05+02:00 docker-1.mscgn.de docker/smile-report-uploader[1098]:
2016-04-27 00:55:05.528 ERROR 1 --- [sk-scheduler-10] o.s.integration.handler.LoggingHandler : org.springframework.messaging.MessagingException: ; nested exception is
org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.dispatcher.AbstractDispatcher.wrapExceptionIfNecessary(AbstractDispatcher.java:133)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:120)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:286)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:245)
at com.sapient.lufthansa.smilereportuploader.service.impl.SftpFileUploaderServiceImpl.uploadFileToSftpServer(SftpFileUploaderServiceImpl.java:56)
at com.sapient.lufthansa.smilereportuploader.service.impl.FileUploaderHandlerImpl.startUploadingProcess(FileUploaderHandlerImpl.java:94)
at com.sapient.lufthansa.smilereportuploader.service.impl.FileUploaderHandlerImpl.handleUploadingRequest(FileUploaderHandlerImpl.java:76)
at com.sapient.lufthansa.smilereportuploader.scheduler.UploadingScheduler.startUploading(UploadingScheduler.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
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: org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is
java.lang.IllegalStateException: failed to create SFTP Session at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:178)
at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:118)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:334)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:211)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:201)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:193)
at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:110)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
... 23 more
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:355)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:49)
at org.springframework.integration.file.remote.session.CachingSessionFactory.createForPool(CachingSessionFactory.java:76)
at org.springframework.integration.file.remote.session.CachingSessionFactory.createForPool(CachingSessionFactory.java:73)
at org.springframework.integration.util.SimplePool.doGetItem(SimplePool.java:188)
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:169)
... 31 more
Caused by: java.lang.IllegalStateException: failed to connect at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:272)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:350)
... 36 more
Caused by: com.jcraft.jsch.JSchException: session is down
at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:762)
at com.jcraft.jsch.Channel.connect(Channel.java:151)
at com.jcraft.jsch.Channel.connect(Channel.java:145)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:267)
... 37 more
我做错了什么?它让我坚持了几天。非常感谢任何帮助。
2016-04-27 11:01:33.966 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Next authentication method: publickey
2016-04-27 11:01:33.970 INFO 9460 --- [nio-9056-exec-1] com.jcraft.jsch : Disconnecting from <> port 22
很可能是密钥或密码问题 - 查看服务器日志。