user1552545 user1552545 - 2 months ago 40
Java Question

Receiving mail with in Spring integration with Java config

I want to receive mails with Spring Integration. I've found a lot of examples with xml config, but I haven't found any with Java DSL config. How can I write the following xml config with Java DSL?

<int-mail:inbound-channel-adapter id="imapAdapter"
store-uri="imaps://[username]:[password]@imap.gmail.com/INBOX"
channel="receiveChannel"
should-delete-messages="true">
<int:poller max-messages-per-poll="1" fixed-rate="5000"/>
</int-mail:inbound-channel-adapter>


I've tried the following solution, but I have no idea, how to add poller to it.

@Bean
public IntegrationFlow mailListener() {
return IntegrationFlows.from(Mail.imapInboundAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX").shouldDeleteMessages(true).get())
.<Message>handle((payload, header) -> logMail(payload))
.get();
}

Answer

See the DSL reference.

@Bean
public IntegrationFlow mailListener() {
    return IntegrationFlows.from(Mail.imapInboundAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX")
             .shouldDeleteMessages(true).get(), 
             e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
        .<Message>handle((payload, header) -> logMail(payload))
        .get();
}
Comments