Spring集成同步多个文件

huangapple go评论54阅读模式
英文:

Spring integration synchronize multiple files

问题

我通过Samba从Windows服务器同步一个包含多个文件的目录。我每天同步一次。这是使用Pollers.cron(..)完成的。然而,每次轮询器运行时只同步一个文件。我想同步所有文件,所以我添加了.maxMessagesPerPoll(..)。这是否是同步目录中所有文件的正确方法?

另外,我想知道应用程序是否可以在启动时进行轮询,除了每天一次的cron调用,如果可能的话?

以下是代码:

public IntegrationFlow fileSync() {
  return IntegrationFlow
    .from(Smb.inboundAdapter(smbSessionFactory())
      .remoteDirectory(remoteDirectory)
      .regexFilter(regexFilter)
      .localDirectory(new File(localDirectory)),
      e -> e.id("smbInboundAdapter")
      .autoStartup(true)
    .poller(Pollers
      .cron(cronExpression)
      .maxMessagesPerPoll(maxMessagesPerPoll))
  )
  .handle(message -> {
    var file = (File) message.getPayload();
    // 处理文件
  })
  .get();
}

希望这些信息对你有帮助。

英文:

I sync a directory with several files from a Windows server via Samba. I synchronize once per day. This is done with Pollers.cron(..). However only one file is synchronized each time the poller runs. I want to synchronize all files so I added .maxMessagesPerPoll(..). Is this the correct way to sync all files in the directory?

Also I would like to know if the application can do a poll when application starts in addition to the cron once per day call, if possible?

This is the code

public IntegrationFlow fileSync() {
  return IntegrationFlow
    .from(Smb.inboundAdapter(smbSessionFactory())
      .remoteDirectory(remoteDirectory)
      .regexFilter(regexFilter)
      .localDirectory(new File(localDirectory)),
      e -> e.id("smbInboundAdapter")
      .autoStartup(true)
    .poller(Pollers
      .cron(cronExpression)
      .maxMessagesPerPoll(maxMessagesPerPoll))
  )
  .handle(message -> {
    var file = (File) message.getPayload();
    // do something with the file
  })
  .get();
}

答案1

得分: 1

这是正确的:入站通道适配器的默认合理行为是每次轮询生成一条消息。请参阅文档:https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-adapter-namespace-inbound

但是,在SourcePollingChannelAdapter中,情况有些不同。max-messages-per-poll的默认值为1,除非您将其明确设置为负值(如-1)。

没有办法立即使用CronTrigger开始轮询。您可以考虑自定义Trigger实现,在第一次调用时返回Instant.now(),然后在所有后续的轮询周期中委托给CronTrigger

英文:

That's correct: the default sensible behavior for Inbound Channel Adapter is to produce a single message per poll. See docs: https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-adapter-namespace-inbound

> However, in the SourcePollingChannelAdapter, it is a bit different. The default value for max-messages-per-poll is 1, unless you explicitly set it to a negative value (such as -1).

There is no way to start polling immediately with a CronTrigger. You may look into a custom Trigger impl where you would return Instant.now() for a first call and delegate to the CronTrigger for all subsequent polling cycles.

huangapple
  • 本文由 发表于 2023年6月5日 17:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定