2

I have a Spring Integration Mail Inbound Adapter configured to poll a POP3 email inbox for processing incoming emails with large XML file attachments. The configuration is as follows:

    <int:channel id="emailInputChannel"/> 
    <int-mail:inbound-channel-adapter id="pop3EmailAdapter" store-uri="pop3://${pop3.user}:${pop3.pwd}@${pop3.server.host}/Inbox" 
    channel="emailInputChannel" should-delete-messages="true" auto-startup="true" java-mail-properties="javaMailProperties">               
<int:poller max-messages-per-poll="1" fixed-rate="${email.poller.rate}" />
    </int-mail:inbound-channel-adapter>

    <!-- Java Mail POP3 properties -->
    <util:properties id="javaMailProperties">
      <beans:prop key="mail.debug">true</beans:prop> 
      <beans:prop key="mail.pop3.port">${pop3.server.port}</beans:prop> 
    </util:properties>

With this configuration I have noticed that the adapter uses multiple task schedulers (essentially pollers) by default. As a result, the same email gets picked up multiple times even though I have indicated that the email needs to be deleted after pickup with the should-delete-message="true". The polling rate is set to 120 seconds. The logs below show the same email being picked up within 2 seconds of each other

 07-15-2015 05:41:46,817 INFO [task-scheduler-7] AbstractMailReceiver:receive:229 | attempting to receive mail from folder [Inbox]

07-15-2015 05:51:45,455 INFO [task-scheduler-10] AbstractMailReceiver:receive:229 | attempting to receive mail from folder [Inbox]

 07-15-2015 05:51:46,994 INFO [task-scheduler-4] AbstractMailReceiver:receive:229 | attempting to receive mail from folder [Inbox]

 07-15-2015 05:51:47,655 INFO [task-scheduler-10] EmailAttachmentSplitter:extractEmailAttachments:53 | EmailAttachmentSplitter.extractEmailAttachments: Received Message with Payload javax.mail.internet.MimeMessage@15e8cbba

 07-15-2015 05:51:49,707 INFO [task-scheduler-4] EmailAttachmentSplitter:extractEmailAttachments:53 | EmailAttachmentSplitter.extractEmailAttachments: Received Message with Payload javax.mail.internet.MimeMessage@15ef73f6

Is there a way to restrict the adapter to use only one poller instead of the default multiple pollers? If so, please provide an example of how to setup the inbound mail adapter with just one poller.

1 Answers1

0

The fixed-delay is for you. From PeriodicTrigger JavaDocs:

 * To measure the interval between the
 * scheduled <emphasis>start</emphasis> time of each execution instead, set the
 * 'fixedRate' property to {@code true}.

And its source code:

@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
    if (triggerContext.lastScheduledExecutionTime() == null) {
        return new Date(System.currentTimeMillis() + this.initialDelay);
    }
    else if (this.fixedRate) {
        return new Date(triggerContext.lastScheduledExecutionTime().getTime() + this.period);
    }
    return new Date(triggerContext.lastCompletionTime().getTime() + this.period);
}

Since TaskScheduler relies on the fact of date for the task to schedule, you can reach your single-thread requirements only with fixed-delay. And the next polling task will be initiated only after the finish of previous one.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thanks a lot. Will try this out. I was also experimenting with an alternate approach which required the following change in the SI configuration. – Venky Ramachandran Jul 16 '15 at 18:10
  • Doesn't fit to comments. Please, edit your question to be more clear and readable – Artem Bilan Jul 16 '15 at 18:18
  • Basically, I defined a task executor with a pool size of 1. Is this a valid approach? – Venky Ramachandran Jul 16 '15 at 18:25
  • Basically, I defined a task executor with a pool size of 1. Is this a valid approach? ` ` – Venky Ramachandran Jul 16 '15 at 18:31
  • I think so. It doesn't matter for `Pop3Receiver` because it does `MailTransportUtils.closeFolder()` within the same single `fixed-delay` `TaskScheduler`'s Thread. And it doesn't impact to the next polling task. – Artem Bilan Jul 16 '15 at 18:32
  • Based on your input is the following poller configuration for the mail adapter work? `' – Venky Ramachandran Jul 16 '15 at 18:36
  • ??? `fixed-rate="true"` ? It's wrong - there is no such a property. As I said to you before: you must have single polling thread. That's why `fixed-delay` helps you. – Artem Bilan Jul 16 '15 at 18:39
  • @ArtemBilan..Hey I have tried solution listed here, but it's not working in my case. My spring configuration is almost similar to the listed one. I am also getting same email twice. Could you provide some more solution for this. I am using gmail as email provider. – ved Jul 04 '17 at 16:53
  • I'm not sure how to help you now. Consider to use Idempotent Receiver if duplicate messages aren't for you. – Artem Bilan Jul 04 '17 at 17:06