简体   繁体   中英

How to listen to multiple mail aliases of the same mail server using Sping-integration-mail and imapIdleAdapter

I need to receive incoming messages from several mail aliases.

At the moment I implemented it in such a way that I created multiple independent IntegrationFlows, where each has the same message handler. Is there any other optimization that can be done?

@Bean
public IntegrationFlow googleListener() {
  return IntegrationFlows.from(Mail.imapIdleAdapter(configuration.getGoogleUrl()))
      .handle(messageHandler::process)
      .get();
}

@Bean
public IntegrationFlow outlookListener() {
  return IntegrationFlows.from(Mail.imapIdleAdapter(configuration.getOutlookUrl()))
      .handle(messageHandler::process)
      .get();
}

@Bean
public IntegrationFlow yandexListener() {
  return IntegrationFlows.from(Mail.imapIdleAdapter(configuration.getYandexUrl()))
      .handle(messageHandler::process)
      .get();
}

Also, when using imapIdleAdapter, it happened that some new emails did not trigger the event and processing. Solved this problem by setting the spring.task.scheduling.pool.size setting to the amount of IntegrationFlow. But I do not quite understand what it is connected with. It turns out that by default in Spring Boot there is only one thread pool, and therefore it was not possible to execute several parallel tasks in one thread?

That's correct. Since Spring Boot is a framework for microservices, it is really expected that sensible default number of tasks in a service is one. Therefore task scheduler in Spring is configured just with one thread in pool. It is indeed better to have that number equal to the number of those concurrent IMAP IDLE tasks you are going to start.

To optimize your configuration better see if you can extract that .handle(messageHandler::process) to another independent flow and use its input channel as an output from these IMAP flows.

@Bean
public IntegrationFlow processFlow() {
  return f -> f.handle(messageHandler::process);
}

@Bean
public IntegrationFlow googleListener() {
  return IntegrationFlows.from(Mail.imapIdleAdapter(configuration.getGoogleUrl()))
      .channel("processFlow.input")
      .get();
}

@Bean
public IntegrationFlow outlookListener() {
  return IntegrationFlows.from(Mail.imapIdleAdapter(configuration.getOutlookUrl()))
      .channel("processFlow.input")
      .get();
}

@Bean
public IntegrationFlow yandexListener() {
  return IntegrationFlows.from(Mail.imapIdleAdapter(configuration.getYandexUrl()))
      .channel("processFlow.input")
      .get();
}

This way you will have only one handling endpoint. All the IMAP flows are going to send their messages to the same channel. No worries about concurrency: the channel is direct by default with a meaning that handleMessage() happens exactly on a thread sending a message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM