通过 IMAPS 异步读取电子邮件时无法请求 Spring 的 API-启动

Cannot requesting API of Spring-Boot while reading asynchronously E-Mail via IMAPS

以下情况。我有一个 Spring-Boot 应用程序,它处理两个主要功能。

  1. 正在通过 IMAPS 从服务器异步读取电子邮件。 (配置见下文)
  2. 提供一个API用于生成和读取订单(这是我的业务对象)

The Problem is curious. If I start my Application without connection to IMAP and reading E-Mails I'm able to make any request to my API. But in the case of starting the E-mail Connection and reading E-Mails I'm not able to make any request to my API.

我总是收到以下错误:

Error: connect ECONNREFUSED 127.0.0.1:8081

任何人都可以帮助我并给我建议如何处理这个问题吗?

// Email Connection
        Store store = null;
        var props = new Properties();
        props.setProperty("mail.store.protocol", mailProperties.getProtocol());
        try {
            var session = Session.getInstance(props, null);
            store = session.getStore();
            store.connect(
                    mailProperties.getHost(),
                    mailProperties.getUsername(),
                    decodeAsString(mailProperties.getPassword())
            );
            var inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);
            waitForNewMessages(inbox);
       ...

       inbox.addMessageCountListener(new MessageCountAdapter() {
            @Override
            @SneakyThrows
            public void messagesAdded(MessageCountEvent ev) {
                for (Message message : ev.getMessages()) {
                    log.info("Message received subject={}, from={}", message.getSubject(), message.getFrom());
                    emailMessageHandler.handle(message);
                }
            }
        });
       ...


    private void waitForNewMessages(Folder inbox) throws MessagingException {
        while (inbox.isOpen()) {
            //every 25 minutes poke the server with a inbox.getMessageCount() to keep the connection active/open
            var scheduler = Executors.newScheduledThreadPool(1);
            final Runnable pokeInbox = () -> {
                try {
                    inbox.getMessageCount();
                } catch (MessagingException ex) {
                    //nothing doin'
                }
            };
            scheduler.schedule(pokeInbox, 25, TimeUnit.MINUTES);
            ((IMAPFolder) inbox).idle();
        }
    }


在研究了整个 www 之后,我找到了以下解决方案 IdleManager。 IdleManager 使用 ExecutorService 来侦听来自 SMTP 服务器的所有事件,并且它不会阻止 Web 应用程序接收其他传入请求。

如需联系方式和更多信息,您可以查看以下文档。 IdleManager