用于调用 Pojo 服务的自定义入站通道适配器

Custom Inbound channel adapter to invoke a Pojo service

我需要 start/stop 一个 Ftp 流程,具体取决于传入文件夹的大小。 我有一项验证文件夹大小的服务:

@Service
public class IncomingFolderChecker  {

  private static final int MAX_ALLOWED_SIZE = 2000000;

  @Value("${sftp.incoming}")
  private String incomingDir;

  @Autowired
  private MessageChannel controlChannel;

  public void checkFolderSize() {
    if (FileUtils.sizeOfDirectory(new File(this.incomingDir)) > MAX_ALLOWED_SIZE) {
      controlChannel.send(new GenericMessage<>("@sftpInboundAdapter.stop()")); // typo: was 'start()'
    } else {
      controlChannel.send(new GenericMessage<>("@sftpInboundAdapter.start()"));
    }
  }
}

我知道控制总线允许这样做。 但这就是我对 Spring 集成的全部了解。 我如何使用 Java-DSL 将其连接起来?

首先,您的条件的两个分支都使用相同的 start() 命令。我猜其中之一应该是stop()。只要 controlChannel 是控制总线组件的输入通道,您的代码就是正确的。要使用 Java DSL 做到这一点,您只需要这样一个简单的 bean:

    @Bean
    public IntegrationFlow controlBusFlow() {
        return IntegrationFlows.from("controlChannel")
                .controlBus()
                .get();
    }

如果这不是问题,请澄清。

更新

所有内容以及 Spring 集成风格及其 Java DSL:

    @Bean
    public IntegrationFlow controlSftpInboundAdapter(@Value("${sftp.incoming}") String incomingDir) {
        return IntegrationFlows
                .from(() -> FileUtils.sizeOfDirectory(new File(incomingDir)) > MAX_ALLOWED_SIZE,
                        e -> e.poller(pollerFactory -> pollerFactory.fixedDelay(1000)))
                .<Boolean, String>transform(p -> "@sftpInboundAdapter." + (p ? "start()" : "stop()"))
                .controlBus()
                .get();
    }