Issue
I have a scheduler class in my Spring boot project which was properly working.
@Component
public class MyCustomScheduler {
@Autowired
private CustomService customeService;
@Scheduled(cron = "${cron.expression}")
public void pickFiles() {
customeService.handle();
}
}
Now I have implemented another class to read Emails from a dedicated inbox. The configuration as below
@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;
}
}
My Spring Boot Application code
@SpringBootApplication
@ComponentScan(value = "com.test.notification")
@EnableScheduling
public class NotificationApplication {
public static void main(String[] args) {
SpringApplication.run(NotificationApplication.class, args);
}
}
Once this code is implemented, My Scheduler stopped working.
Is it bacause the default pool size of TaskScheduler is "1" or is there any other configuration which is shared by Scheduler and the email adaptor?
Solution
I think the problem is really just because a default thread pool size for that scheduler is really 1
. It is that sensitive default which is enough do not abuse environment resources for nothing. Plus Spring Boot is really a Microservices framework, so better to develop a single task in the target project to avoid conflicting problems and so on.
Answered By - Artem Bilan
Answer Checked By - Pedro (JavaFixing Volunteer)