【问题标题】:Java mail listener using Spring Integration使用 Spring Integration 的 Java 邮件监听器
【发布时间】:2020-05-27 21:20:19
【问题描述】:

我在 Springboot 应用程序中使用以下代码:

@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:pwd@mail.company.com/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;
    }

我的要求是每个 Pollers.fixedRate(比如每 1 分钟)有多少新邮件到达,logMail 应该通过传递新邮件作为参数来执行多次(在下一次轮询之前)但它没有发生。每个 Pollers.fixedRate(每 1 分钟)仅调用一次 logMail 方法,因此只有一封邮件得到处理。如果我在过去 1 分钟内收到了 3 封邮件,那么现在将处理第一封邮件。第二封邮件将在接下来的 1 分钟内处理,以此类推。

或者有什么方法可以通过发送在该期间(1 分钟)内新到达的消息列表来调用 logMail 吗?你能告诉我怎么做吗?

【问题讨论】:

    标签: java spring-integration jakarta-mail spring-dsl


    【解决方案1】:

    将轮询器 maxMessagesPerPoll 从其默认值 1 增加。

    【讨论】:

    • 谢谢 :) 我打算使用 maxMessagesPerPoll(-1) 因为我不想对邮件数量设置任何限制。请让我知道这是否是正确的处理方式?
    • 是的; -1 表示无穷大。
    • 感谢您的建议:)
    猜你喜欢
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2012-06-30
    • 2018-11-26
    • 1970-01-01
    • 2013-03-26
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多