Search code examples
spring-integrationenterprise-integrationcontrol-bus

Stopping and restarting Spring Integration input endpoints via control bus


In my former life I worked on a few Apache Camel projects, so I'm not entirely new to EIPs, but I am now trying to learn & understand Spring Integration. I have (what I think is) a small snippet of code for a "flow" that:

  1. Defines a control bus for managing & monitoring the flow
  2. Flow starts by fetching PNG images out of a folder (polling for new ones once a day); then
  3. Uploads them to a directory on an FTP server
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setBeanName("fileMessageSource");
fileSource.setDirectory(new File("C:/DestDir"));
fileSource.setAutoCreateDirectory(true);

DefaultFtpSessionFactory ftpSessionFactory = new DefaultFtpSessionFactory();

IntegrationFlow flow = IntegrationFlows.from(fileSource, configurer -> configurer.poller(Pollers.cron("0 0 * * *")))
    .filter("*.png")    // only allow PNG files through
    .controlBus()       // add a control bus
    .handle(Ftp.outboundAdapter(ftpSessionFactory, FileExistsMode.FAIL)
        .useTemporaryFileName(false)
        .remoteDirectory("uploadDir"))
    .get();

Although admittedly I am a little unsure of the differences between "flows" and "channels" in Spring Integration parlance (I believe a flow is a composition of channels, and channels connect individual endpoints, maybe?), I am not understanding how, given my code above, the control bus can be leverage to turn the fileSource input endpoint on/off.

I know that with control buses, you send SPeL messages to channels and the control bus takes those SPeL messages and uses them to figure out which beans/methods to invoke, but above I am starting my flow from a FileReadingMessageSource. So what is the exact message I would need to send to which channel so that it stops/pauses or starts/restarts the FileReadingMessageSource endpoint?

The idea would be that if I used the control bus to turn the FileReadingMessageSource off, then days and days could pass and no PNG files would ever be uploaded to the FTP server, until I used the control bus to turn it back on.

Thanks for any-and-all help here!


Solution

  • The control bus needs to be in its own flow, not a component in another flow; you send control messages to it, to control other endpoints.

    See a recent example in this question and answer.

    Using Control Bus EIP in Spring Integration to start/stop channels dynamically