VGH
VGH

Reputation: 425

Java mail listener using Spring Integration

I'm using below code in a Springboot application:

@Bean
    public IntegrationFlow mailListener() {

        return IntegrationFlows.from(Mail.imapInboundAdapter(receiver()), e -> e.poller(Pollers.fixedRate(60000)))
                .<Message>handle(message -> logMail(message)).get();
    }

    private org.springframework.messaging.Message<?> logMail(org.springframework.messaging.Message<?> message) {
        System.out.println("received a mail********** !");
        // System.out.println(message.getPayload());
        // process message
        return message;
    }

    @Bean
    public ImapMailReceiver receiver() {

        ImapMailReceiver receiver = new ImapMailReceiver(
                "imaps://username:[email protected]/INBOX");
        receiver.setShouldMarkMessagesAsRead(true);
        receiver.setJavaMailProperties(javaMailProperties());
        return receiver;
    }

    private Properties javaMailProperties() {
        Properties javaMailProperties = new Properties();

        /*
         * javaMailProperties.setProperty("mail.imap.socketFactory.class",
         * "javax.net.ssl.SSLSocketFactory");
         * javaMailProperties.setProperty("mail.imap.socketFactory.fallback","false");
         * javaMailProperties.setProperty("mail.store.protocol","imaps");
         */
        // javaMailProperties.setProperty("mail.debug","true");

        return javaMailProperties;
    }

My requirement is for every Pollers.fixedRate(say every 1 minute) how many new mails have arrived, that many number of times logMail should get executed by passing new mail as argument (before next polling) But it's not happening. logMail method being invoked only once per Pollers.fixedRate(every 1 minute) hence only one mail is getting processed.If i had received say 3 mails in last 1 minute, 1st mail will be processed now. 2nd mail will be processed in next 1 min and so on.

Or is there any way logMail can be invoked by sending the list of Messages that are newly arrived within that period (1 minute) ?Can you please let me know how to do it?

Upvotes: 0

Views: 811

Answers (1)

Gary Russell
Gary Russell

Reputation: 174494

Increase the poller maxMessagesPerPoll from its default 1.

Upvotes: 1

Related Questions