当我配置 ImapIdleChannelAdapter 以从收件箱读取电子邮件时,Spring 中的调度程序停止工作

Scheduler in Spring Boot stopped working when I configure an ImapIdleChannelAdapter to read emails from an Inbox

我的 Spring 引导项目中有一个调度程序 class,它工作正常。

    @Component
    public class MyCustomScheduler {
   
      @Autowired
      private CustomService customeService;

      @Scheduled(cron = "${cron.expression}")
      public void pickFiles() {
        
        customeService.handle();
      } 
    }

现在我已经实现了另一个 class 从专用收件箱读取电子邮件。配置如下

@Configuration
@EnableIntegration
public class EmailAdapterConfig {

    private static final Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(EmailAdapterConfig.class);
    
    @Value( "${mail.username}" )
    private String MAIL_USERNAME;
    
    @Value( "${mail.password}" )
    private String MAIL_PASSWORD;   
    
    @Value( "${mail.protocol}" )
    private String MAIL_PROTOCOL;   

    @Value( "${mail.host}" )
    private String MAIL_HOST;       

    @Value( "${mail.port}" )
    private String MAIL_PORT;   
    
    @Value( "${mail.folder}" )
    private String MAIL_FOLDER;
    
    @Value( "${mail.debug}" )
    private String MAIL_DEBUG;  
    
    @Value( "${mail.ssl.trust}" )
    private String MAIL_SSL_TRUST;
    
    @Bean
    public ImapIdleChannelAdapter mailAdapter() {
        
        ImapIdleChannelAdapter imapAdapter = new ImapIdleChannelAdapter(imapMailReceiver());
        imapAdapter.setAutoStartup(true);
        imapAdapter.setOutputChannelName("emailReceiveChannel");
        
        return imapAdapter;
    }

    @Bean
    public ImapMailReceiver imapMailReceiver() {

        StringBuilder impaURI = new StringBuilder();
        impaURI.append(MAIL_PROTOCOL).append("://").append(MAIL_USERNAME).append(":").append(MAIL_PASSWORD)
        .append("@").append(MAIL_HOST).append(":").append(MAIL_PORT).append("/").append(MAIL_FOLDER);
        
        logger.info("IMAP URL = " + impaURI.toString());
        
        ImapMailReceiver mailReceiver = new ImapMailReceiver(impaURI.toString());
        mailReceiver.setShouldDeleteMessages(true);
        mailReceiver.setShouldMarkMessagesAsRead(true);
        mailReceiver.setJavaMailProperties(javaMailProperties());
        mailReceiver.setAutoCloseFolder(false);

        return mailReceiver;
    }    
     
    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", MAIL_PROTOCOL);
        javaMailProperties.setProperty("mail.debug", MAIL_DEBUG);
        javaMailProperties.setProperty("mail.imap.starttls.enable", "true");
        javaMailProperties.setProperty("mail.imaps.ssl.trust", MAIL_SSL_TRUST);
        
        return javaMailProperties;
    }
}

我的 Spring 启动应用程序代码

@SpringBootApplication
@ComponentScan(value = "com.test.notification")
@EnableScheduling
public class NotificationApplication {

    public static void main(String[] args) {

        SpringApplication.run(NotificationApplication.class, args);
    }
}

执行此代码后,我的调度程序停止工作。

是因为 TaskScheduler 的默认池大小为“1”还是调度程序和电子邮件适配器共享任何其他配置?

我认为问题实际上只是因为该调度程序的默认线程池大小实际上是 1。这是足够敏感的默认值,不会白白滥用环境资源。另外SpringBoot真的是一个微服务框架,所以最好在目标项目中单独开发任务,避免冲突等问题。