Spring 集成 SFTP 按需删除远程文件
Spring Integration SFTP Delete Remote File On Demand
我正在尝试在我的应用程序中发生某些事情时从远程 SFTP 存储桶中删除单个文件(我们将文件保存到 blob 存储)。我没能做到这一点。我所能看到的只是在处理完成后始终通过出站通道适配器删除文件。我正在使用 Java 配置。
这是我的代码:
@Bean
@ServiceActivator(inputChannel = "sftpDeleteChannel")
public MessageHandler deleteHandler(SftpInboundProperties properties) {
SftpOutboundGateway SFTP = new SftpOutboundGateway(sftpSessionFactory(properties), "rm", "'" + properties.getRemoteDirectory() + "'");
return SFTP;
}
@Gateway(requestChannel = "sftpDeleteChannel")
Boolean delete(Message<File> file);
这是方便输入文件的服务激活器:
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler(FileProcessingService fileProcessingService) {
return message -> {
fileProcessingService.processFile((Message<File> message);
};
}
我收到此错误消息:
Reply message received but the receiving thread has exited due to an exception while sending the request message: ErrorMessage [payload=org.springframework.messaging.MessageHandlingException: error occurred in message handler [bean 'deleteHandler'; defined in: 'class path resource [com/.../SftpInboundConfiguration.class]'; from source: 'org.springframework.core.type.classreading.SimpleMethodMetadata@6ff37443']; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to remove file.; nested exception is 3: Permission denied, failedMessage=GenericMessage [payload=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, headers={file_remoteHostPort=127.0.0.1:2222, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@34d7c35d, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@34d7c35d, file_name=COMPLETE_FILE_NAME, file_remoteDirectory=//sftp_remote, file_originalFile=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, id=2252eee1-6086-c9d9-c421-403f8a0bfc28, file_relativePath=COMPLETE_FILE_NAME, file_remoteFile=COMPLETE_FILE_NAME, timestamp=1588177024991}], headers={id=0d2b19ca-0e7c-7ba8-58cf-f225c126048c, timestamp=1588177025001}] for original GenericMessage [payload=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, headers={file_remoteHostPort=127.0.0.1:2222, file_name=COMPLETE_FILE_NAME, file_remoteDirectory=//sftp_remote, file_originalFile=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, id=9f29dd04-362a-ae93-df97-b392572d8864, file_relativePath=COMPLETE_FILE_NAME, file_remoteFile=COMPLETE_FILE_NAME, timestamp=1588177023713}]
我不确定我是不是做错了,或者出站网关是否不适用于特定的按需删除,或两者兼而有之。会喜欢一些帮助。
谢谢!
编辑:
我可以使用SftpInboundFileSynchronizer.setDeleteRemoteFiles(true)删除服务器上的文件,所以就sftp bucket而言,我可以删除文件。我需要更改 InboundChannelAdapter 中的某些内容吗?下面是:
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "${com.esrx.dhf.listener.sftp.inbound.poll-interval:5000}"))
public MessageSource<File> sftpMessageSource(SftpInboundProperties properties, JdbcMetadataStore metadataStore) {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer(properties, metadataStore));
source.setLocalDirectory(new File(System.getProperty("user.dir") + "/" + "sftp_local"));
source.setAutoCreateLocalDirectory(true);
source.setMaxFetchSize(1);
source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(metadataStore, SFTP_LOCAL_PERSISTENT_PREFIX));
return source;
}
编辑:已解决
所以可敬的 Gary Russel 的真正回答是我应该在 SftpOutboundGateway 中指定文件的完整路径。所以现在有效的解决方案意味着新的 OutboundGateway 看起来像这样:
@Bean
@ServiceActivator(inputChannel = "sftpDeleteChannel")
public MessageHandler deleteHandler(SftpInboundProperties properties) {
return new SftpOutboundGateway(
sftpSessionFactory(properties),
"rm",
"'" + properties.getRemoteDirectory() + "/'" + " + " + "headers['file_remoteFile']");
}
你得到的是正确的。
查看原因:
nested exception is 3: Permission denied,
您没有删除该文件的权限。
我正在尝试在我的应用程序中发生某些事情时从远程 SFTP 存储桶中删除单个文件(我们将文件保存到 blob 存储)。我没能做到这一点。我所能看到的只是在处理完成后始终通过出站通道适配器删除文件。我正在使用 Java 配置。
这是我的代码:
@Bean
@ServiceActivator(inputChannel = "sftpDeleteChannel")
public MessageHandler deleteHandler(SftpInboundProperties properties) {
SftpOutboundGateway SFTP = new SftpOutboundGateway(sftpSessionFactory(properties), "rm", "'" + properties.getRemoteDirectory() + "'");
return SFTP;
}
@Gateway(requestChannel = "sftpDeleteChannel")
Boolean delete(Message<File> file);
这是方便输入文件的服务激活器:
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler(FileProcessingService fileProcessingService) {
return message -> {
fileProcessingService.processFile((Message<File> message);
};
}
我收到此错误消息:
Reply message received but the receiving thread has exited due to an exception while sending the request message: ErrorMessage [payload=org.springframework.messaging.MessageHandlingException: error occurred in message handler [bean 'deleteHandler'; defined in: 'class path resource [com/.../SftpInboundConfiguration.class]'; from source: 'org.springframework.core.type.classreading.SimpleMethodMetadata@6ff37443']; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to remove file.; nested exception is 3: Permission denied, failedMessage=GenericMessage [payload=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, headers={file_remoteHostPort=127.0.0.1:2222, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@34d7c35d, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@34d7c35d, file_name=COMPLETE_FILE_NAME, file_remoteDirectory=//sftp_remote, file_originalFile=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, id=2252eee1-6086-c9d9-c421-403f8a0bfc28, file_relativePath=COMPLETE_FILE_NAME, file_remoteFile=COMPLETE_FILE_NAME, timestamp=1588177024991}], headers={id=0d2b19ca-0e7c-7ba8-58cf-f225c126048c, timestamp=1588177025001}] for original GenericMessage [payload=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, headers={file_remoteHostPort=127.0.0.1:2222, file_name=COMPLETE_FILE_NAME, file_remoteDirectory=//sftp_remote, file_originalFile=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, id=9f29dd04-362a-ae93-df97-b392572d8864, file_relativePath=COMPLETE_FILE_NAME, file_remoteFile=COMPLETE_FILE_NAME, timestamp=1588177023713}]
我不确定我是不是做错了,或者出站网关是否不适用于特定的按需删除,或两者兼而有之。会喜欢一些帮助。
谢谢!
编辑:
我可以使用SftpInboundFileSynchronizer.setDeleteRemoteFiles(true)删除服务器上的文件,所以就sftp bucket而言,我可以删除文件。我需要更改 InboundChannelAdapter 中的某些内容吗?下面是:
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "${com.esrx.dhf.listener.sftp.inbound.poll-interval:5000}"))
public MessageSource<File> sftpMessageSource(SftpInboundProperties properties, JdbcMetadataStore metadataStore) {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer(properties, metadataStore));
source.setLocalDirectory(new File(System.getProperty("user.dir") + "/" + "sftp_local"));
source.setAutoCreateLocalDirectory(true);
source.setMaxFetchSize(1);
source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(metadataStore, SFTP_LOCAL_PERSISTENT_PREFIX));
return source;
}
编辑:已解决
所以可敬的 Gary Russel 的真正回答是我应该在 SftpOutboundGateway 中指定文件的完整路径。所以现在有效的解决方案意味着新的 OutboundGateway 看起来像这样:
@Bean
@ServiceActivator(inputChannel = "sftpDeleteChannel")
public MessageHandler deleteHandler(SftpInboundProperties properties) {
return new SftpOutboundGateway(
sftpSessionFactory(properties),
"rm",
"'" + properties.getRemoteDirectory() + "/'" + " + " + "headers['file_remoteFile']");
}
你得到的是正确的。
查看原因:
nested exception is 3: Permission denied,
您没有删除该文件的权限。