[英]Problems with spring sftp inbound adapter
I have a requirement for collecting files from an SFTP location and putting it into a local input directory at a regular interval. 我需要从SFTP位置收集文件,并定期将其放入本地输入目录。 After the file is transferred, the file gets deleted from the SFTP location.
传输文件后,文件将从SFTP位置删除。 For the trigger I am using a customized Cron Trigger in which I am dynamically changing the interval.
对于触发器,我使用自定义的Cron触发器,在其中动态更改间隔。 Below are the entries in context.xml and the code for the CustomCronTrigger class.
以下是context.xml中的条目以及CustomCronTrigger类的代码。
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="127.0.0.1"/>
<property name="port" value="22"/>
<property name="user" value="user"/>
<property name="password" value="passwd"/>
</bean>
<int-sftp:inbound-channel-adapter id="sftpInboundAdapter"
channel="receiveChannel"
session-factory="sftpSessionFactory"
local-directory="/home/sftpUser"
remote-directory="/home/input"
delete-remote-files="true"
auto-create-local-directory="true"
filename-pattern="*.csv"
>
<int:poller trigger="customCronTrigger"/>
</int-sftp:inbound-channel-adapter>
<int:channel id="receiveChannel">
<int:queue/>
</int:channel>
public class CustomCronTrigger implements Trigger{
private CronSequenceGenerator sequenceGenerator;
private String intervalInMin;
public void setIntervalInMin(String intervalInMin) throws Exception
{
this.intervalInMin = intervalInMin;
createCronExpression();
}
/** The logger instance */
private Logger logger = LoggerFactory.getLogger(getClass());
public CronSequenceGenerator getSequenceGenerator() {
return sequenceGenerator;
}
public void createCronExpression() throws Exception {
String cronExpression = getCronExpression(intervalInMin);
logger.debug("CustomCronTrigger.setSequenceGeneratorStr()::cronExpression="+cronExpression);
this.sequenceGenerator = new CronSequenceGenerator(cronExpression, TimeZone.getDefault());
}
public Date nextExecutionTime(TriggerContext triggerContext) {
Date date = triggerContext.lastCompletionTime();
if (date != null) {
Date scheduled = triggerContext.lastScheduledExecutionTime();
if (scheduled != null && date.before(scheduled)) {
// Previous task apparently executed too early...
// Let's simply use the last calculated execution time then,
// in order to prevent accidental re-fires in the same second.
date = scheduled;
}
}
else {
date = new Date();
}
return this.sequenceGenerator.next(date);
}
public String getCronExpression(String min) throws Exception
{
if(Integer.parseInt(min)<0 || Integer.parseInt(min)>59 )
{
throw new Exception(String.format("Minute %s not valid. Please enter minute between 0-59",min));
}
String cronExpression = "0 0/"+min+" * * * ?";
return cronExpression;
}
@Override
public boolean equals(Object obj) {
return (this == obj || (obj instanceof CustomCronTrigger &&
this.sequenceGenerator.equals(((CustomCronTrigger) obj).sequenceGenerator)));
}
@Override
public int hashCode() {
return this.sequenceGenerator.hashCode();
}
@Override
public String toString() {
return this.sequenceGenerator.toString();
}
}
On the first interval the files get transferred properly and then deleted from source directory, though the log shows that only one file is being transferred. 在第一个间隔中,文件会正确传输,然后从源目录中删除,尽管日志显示仅传输了一个文件。 On successive intervals the files do not get transferred and get transferred only after some intervals.
在连续的时间间隔中,文件不会被传输,只有在一定的时间间隔后才会被传输。 Reading the logs do not give any indication as to why the subsequent files are failing to get transferred on time.
读取日志并不能说明为什么后续文件未能按时传输。 Below are the logs for the sftp.
以下是sftp的日志。
*** 18 Nov 2013 11:49:00[L=INFO] [T=task-scheduler-1] (org.springframework.integration.file.FileReadingMessageSource:264 receive())
Created message: [[Payload=/home/sftpUser/abc.csv][Headers={timestamp=1384755540001, id=fce1bea5-6444-4000-9b29-9a48e6027814}]]
*** 18 Nov 2013 11:50:00[L=INFO] [T=task-scheduler-2] (org.springframework.integration.file.FileReadingMessageSource:264 receive())
Created message: [[Payload=/home/sftpUser/xyz.csv][Headers={timestamp=1384755600000, id=7a9c38b0-163c-473d-842b-58b41af8ad2d}]]
*** 18 Nov 2013 11:51:00[L=INFO] [T=task-scheduler-1] (org.springframework.integration.file.FileReadingMessageSource:264 receive())
Created message: [[Payload=/home/sftpUser/def.csv][Headers={timestamp=1384755660001, id=3b10b954-3c16-4bf8-9b69-7fc213c68f37}]]
*** 18 Nov 2013 11:52:00[L=INFO] [T=task-scheduler-3] (org.springframework.integration.file.FileReadingMessageSource:264 receive())
Created message: [[Payload=/home/sftpUser/ghi.csv][Headers={timestamp=1384755720028, id=e1031613-788d-45f9-a2ae-30f953832ff7}]]
Please suggest alternative's as I am at my wit's end with this problem. 请提出替代方案,因为我对这个问题不知所措。
It's not clear what you mean by "On successive intervals the files do not get transferred and get transferred only after some intervals". 目前尚不清楚“连续的时间间隔不会传输文件,而是仅在一定的间隔后才传输文件”的含义。
Be aware that if you expect to transfer a file with the same name, it won't work because the default local filter will disallow the generation of a message for it. 请注意,如果您希望传输具有相同名称的文件,则该文件将无法工作,因为默认的本地过滤器将禁止为其生成消息。 The upcoming 3.0 release has a new
local-filter
attribute to change that behavior, or you can use a gateway instead, as I suggested in this answer . 即将发布的3.0版本具有新的
local-filter
属性来更改该行为,或者您可以改用网关,如我在此答案中所建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.