我需要从SFTP位置收集文件并定期将其放入本地输入目录。传输文件后,文件将从SFTP位置删除。对于触发器,我使用自定义的Cron Trigger,我在其中动态更改间隔。下面是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();
}
}
在第一个时间间隔内,文件正确传输,然后从源目录中删除,但日志显示只传输了一个文件。在连续的时间间隔内,文件不会被传输,只能在一定时间间隔后传输。读取日志并不表示后续文件无法按时传输的原因。以下是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}]]
请提出替代方案,因为我在这个问题上已经结束了。
答案 0 :(得分:0)
目前还不清楚你的意思是“在连续的时间间隔内,文件不会被转移,只能在一段时间后才能转移。”
请注意,如果您希望传输具有相同名称的文件,则它将无效,因为默认本地过滤器将禁止为其生成消息。即将发布的3.0版本有一个新的local-filter
属性可以改变这种行为,或者您可以使用网关,正如我在this answer中所建议的那样。