Search code examples
javaspringspring-integrationjakarta-mail

Start & Stop - Spring imap inbound-channel-adapter from main args


I've configured an imap channel-adapter to receive mail with 20000 fixed delay. So it's working as expected. But the problem is, it's running continuously. So I have a couple of questions:

  1. How can I start and stop this channel adapter using main()?
  2. When we do configure this job in batch server (unix), how can I stop this job using main program itself (It's this channel adapter running continuously).
  3. How can I return something to the main method, which is starting the poller as 'success' or 'failure' so that I can return system.exit(0) or system.exit(1) from main.
  4. How can I have complete control over main() by running channel adapter?

Thanks in advance for your help.

<int-mail:imap-idle-channel-adapter id="customAdapter"
        store-uri="imaps://[userid]:[pasword]@imap.gmail.com:993/inbox"
        channel="receiveChannel"
        auto-startup="true"
        should-delete-messages="false"
        should-mark-messages-as-read="false"
        java-mail-properties="javaMailProperties"auto-startup="false">
    <int:poller max-messages-per-poll="10" fixed-delay="20000"/>

Solution

    1. Start and stop can be controlled via Control Bus component.

    2. You can even stop that adapter from downstream flow by some condition and sending a message to the same Control Bus channel.

    3. If you need to run it only once, you can consider a trigger option instead of fixed-delay and implement it like:

      private final AtomicBoolean invoked = new AtomicBoolean();
      
      public Date nextExecutionTime(TriggerContext triggerContext) {
          return this.invoked.getAndSet(true) ? null : new Date();
      }
      
    4. Return something to main you can via simple CountDonwLatch and some AtomicBoolean (or Reference) bean.

    Sorry, so much questions in one topic... That isn't appropriate for SO.