Java 如何解决“RotatingServerAdvice”多次未提取文件的问题?
我使用Java 如何解决“RotatingServerAdvice”多次未提取文件的问题?,java,spring,spring-integration,dsl,spring-integration-sftp,Java,Spring,Spring Integration,Dsl,Spring Integration Sftp,我使用IntegrationFlow作为Sftp入站DSL配置使用CustomTriggerAdvice处理手动触发。请参阅下面的代码片段以供参考 我还使用RotatingServerAdvice处理同一主机中的多个路径 但是,当我启动sftpinbound时,它第一次从每个路径获取文件,但第二次和以后都不起作用。Sftp入站启动,但不从路径获取文件。我想不出这个问题。有什么我遗漏的吗 SftpConfiguration public IntegrationFlow fileFlow() {
IntegrationFlow
作为Sftp入站DSL配置使用CustomTriggerAdvice
处理手动触发。请参阅下面的代码片段以供参考
我还使用RotatingServerAdvice
处理同一主机中的多个路径
但是,当我启动sftpinbound时,它第一次从每个路径获取文件,但第二次和以后都不起作用。Sftp入站启动,但不从路径获取文件。我想不出这个问题。有什么我遗漏的吗
SftpConfiguration
public IntegrationFlow fileFlow() {
SftpInboundChannelAdapterSpec spec = Sftp
.inboundAdapter(dSF())
.preserveTimestamp(true)
.remoteDirectory(".")
.autoCreateLocalDirectory(true)
.deleteRemoteFiles(false)
.localDirectory(new File(getDestinationLocation()));
return IntegrationFlows
.from(spec, e -> e.id(BEAN_ID)
.autoStartup(false)
.poller(Pollers
.fixedDelay(5000)
.advice(
customRotatingServerAdvice(dSF()),
customTriggerAdvice()
)
)
)
.channel(sftpReceiverChannel())
.handle(sftpInboundMessageHandler())
.get();
}
private MessageChannel sftpReceiverChannel() {
return MessageChannels.direct().get();
}
public class CustomTriggerAdvice extends AbstractMessageSourceAdvice {
private final MessageChannel controlChannel;
private final String BEAN_ID;
public CustomTriggerAdvice(MessageChannel controlChannel, String beanID) {
this.controlChannel = controlChannel;
this.BEAN_ID = beanID;
}
@Override
public boolean beforeReceive(MessageSource<?> source) {
return true;
}
@Override
public Message<?> afterReceive(Message<?> result, MessageSource<?> source) {
if (result == null) {
controlChannel.send(new GenericMessage<>("@" + BEAN_ID + ".stop()"));
}
return result;
}
}
@Bean
public RotatingServerAdvice customRotatingServerAdvice(
DelegatingSessionFactory<LsEntry> dSF
) {
List<String> pathList = getSourcePathList();
for (String path : pathList) {
keyDirectories.add(new RotationPolicy.KeyDirectory(KEY, path));
}
return new RotatingServerAdvice(
dSF,
keyDirectories
);
}
@Bean
public CustomTriggerAdvice customTriggerAdvice() {
return new CustomTriggerAdvice(customControlChannel(),BEAN_ID);
}
@Bean
public IntegrationFlow customControlBus() {
return IntegrationFlows.from(customControlChannel())
.controlBus()
.get();
}
@Bean
public MessageChannel customControlChannel() {
return MessageChannels.direct().get();
}
使用MessageChannel启动Sftp入站
@Qualifier("customControlChannel") MessageChannel controlChannel;
public void startSftpInbound(){
controlChannel.send(new GenericMessage<>("@" + beanID + ".start()"));
}
@限定符(“customControlChannel”)消息通道控制通道;
public void startSftpInbound(){
发送(新的GenericMessage(“@”+beanID+”.start()”);
}
我需要系统启动的需求和获取文件完成一个周期。如果之后没有停止,它将继续轮询,不会停止,我的系统将陷入无限循环。当RotatingServerAdvice至少从一台服务器完成所有服务器的轮询时,是否有任何方法可以实现这一点?它会抛出任何事件或类似的东西吗
您可能误解了afterReceive(@Nullable Message result,MessageSource source)契约的逻辑。当其中一台服务器未返回任何要轮询的内容时,无法根据您的要求停止通道适配器。这样,您就没有机会让其他服务器在下一个轮询周期进行轮询
我认为您的想法是只在所有服务器上迭代一次,然后停止。可能与它们的结果无关。看起来最好的停止方式是使用
RotatingServerAdvice
和fair=true
每次移动到下一台服务器。当您看到RotationPolicy.getCurrent()
与列表中的最后一个相等时,可以从自定义的afterReceive()
执行停止,而不依赖于结果。因此,通过这种方式,您可以迭代所有这些参数,并停止在下一个极化周期中移动第一个参数。您的CustomTriggerAdvice
使用BEAN\u ID
停止SourcePollingChannelAdapter
的SftpInboundChannelAdapterSpec
,但我看不出您将其称为StartSpinBound()
…我尝试了各种调试,但找不到任何东西,并且非常确定以前没有获取该文件,并且该文件没有存储在元数据存储中。
。它正常工作。它会在轮询每个路径后的一瞬间获取新文件。是的。当我删除controlChannel.send(新的GenericMessage(“@”+BEAN\u ID+”.stop()”)时,它就开始工作了
从afterReceive()
重新启动并获取新文件。我使用手动触发器停止Sftp入站。希望您了解我的逻辑,以及在至少轮询每个目录一次后如何使用stop Sftp入站。是否有类似于RotationPolicy.getCurrent()
的内容?或者在何处找到此RotationPolicy.getCurrent()
?RotatingServerAdvice
具有RotationPolicy
属性。您需要覆盖带有afterReceive()
的自定义RotatingServerAdvice
,以执行逻辑,确保您处于停止通道适配器的状态。