使用Spring和IMAP闲置多个电子邮件帐户

问题描述 投票:0回答:1

我正在尝试无限地接收来自多个邮件帐户的邮件。这里的问题是,邮件没有并行接收。如果我正在将邮件发送到受监视的电子邮件帐户,则它只有一个闲置,并且如果捕获到邮件,则另一个闲置。

我试图用多个线程创建一个ScheduleTaskExecutor并用多个帐户启动它,但是我不明白,为什么我一次只闲置一个帐户。

我在哪里开始任务

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableScheduling
@EnableAsync
public class MailApplication{

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MailApplication.class, args);
        try {
            String[] accounts = {"testmail1%40web.de:123456", "testmail2%40web.de:123456"};
            context.getBean(MailGuard.class).start(accounts);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我的任务类别

@Component
public class MailGuard {

    @Autowired
    private IntegrationFlowContext flowContext;

    private static final Logger log = LoggerFactory.getLogger(MailGuard.class);
    private TaskScheduler scheduler;

    @Async
    public void start(String[] data) {
        ScheduledExecutorService localExecutor = Executors.newScheduledThreadPool(Integer.MAX_VALUE);
        scheduler = new ConcurrentTaskScheduler(localExecutor);

        for(String acc : data) {
            scheduler.schedule(new MailRunnable(acc, flowContext),
                        new Date());

        }


    }

    class MailRunnable implements Runnable {
        String account;
        IntegrationFlowContext flowContext;

        MailRunnable(String account, IntegrationFlowContext flowContext) {
            this.account = account;
            this.flowContext = flowContext;
        }

        public void run() {
            String[] split = account.split(":");
            startMail(split[0], split[1]);
            log.info("started mail monitoring for: " + split[0]);
        }


        public void startMail(String user, String pw) {
            IntegrationFlow flow = IntegrationFlows
                    .from(Mail.imapIdleAdapter(imapUrl(user, pw))
                            .javaMailProperties(p -> {
                                p.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                                p.put("mail.store.protocol", "imaps");
                                p.put("mail.debug", "false");
                                p.put("mail.imaps.timout", "100");
                            })
                            .shouldDeleteMessages(true)
                            .shouldReconnectAutomatically(true)
                            .selectorExpression(new SpelExpressionParser()
                            .headerMapper(new DefaultMailHeaderMapper()))
                    .handle(this::handle)
                    .get();
            this.flowContext.registration(flow).register();
        }

        private <P> Object handle(P p, MessageHeaders messageHeaders) {
            System.out.println(messageHeaders.get("mail_from"));
            return null;
        }

        private String imapUrl(String user, String pw) {

            return "imap://"
                    + user + ":" + pw
                    + "@imap.web.de:993/INBOX";
        }

    }
}
java spring spring-boot javamail imap
1个回答
0
投票

这是我的问题的一个例子

  1. 将邮件发送到Testmail1->应用程序获取Testmail1的邮件
  2. 再次发送邮件到Testmail1->应用没有任何作用
  3. 将邮件发送到Testmail2->应用程序获取Testmail2的邮件
  4. 将邮件发送到Testmail2->应用程序不执行任何操作

因此它以某种特定的顺序而不是并行地空闲

© www.soinside.com 2019 - 2024. All rights reserved.