接收Spring Boot和Spring集成的异步邮件

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

我正在尝试从gmail服务器接收电子邮件,然后以某种方式处理它们。因此,每当新电子邮件到达邮件服务器时,我的应用程序都应下载并处理它,这意味着将异步地调用其他服务,这些服务将被注册为侦听器。

我在Spring集成方面还很陌生,我不完全了解它是如何工作的或者它是否正确。

[1)到目前为止,我有这段代码-我能够阅读所有电子邮件,但不确定是否<,或者这是否正确? ListeningExample类

@Configuration public class ListeningExample { @Bean public HeaderMapper<MimeMessage> mailHeaderMapper() { return new DefaultMailHeaderMapper(); } @Bean public IntegrationFlow imapMailFlow() { IntegrationFlow flow = IntegrationFlows .from(Mail.imapInboundAdapter("imap://user:[email protected]/INBOX") .userFlag("testSIUserFlag") .javaMailProperties(javaMailProperties()), e -> e.autoStartup(true) .poller(p -> p.fixedDelay(5000)))dostane to detailni zpravy .transform(Mail.toStringTransformer()) .channel(MessageChannels.queue("imapChannel")) .get(); return flow; } @Bean(name = PollerMetadata.DEFAULT_POLLER) public PollerMetadata defaultPoller() { PollerMetadata pollerMetadata = new PollerMetadata(); pollerMetadata.setTrigger(new PeriodicTrigger(1000)); return pollerMetadata; } }

MailRecieverService类

@Service public class MailRecieverService { private List<EmailAction> services; @Bean @ServiceActivator(inputChannel = "imapChannel") public MessageHandler processNewEmail() { MessageHandler messageHandler = new MessageHandler() { @Override public void handleMessage(org.springframework.messaging.Message<?> message) throws MessagingException { System.out.println("New email:" + message.toString()); //Processing emails do with them something.. for (EmailAction emailAction : services) { emailAction.performAction(null); } } }; return messageHandler; } }

主类

@SpringBootApplication @EnableIntegration public class Main extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Main.class); } public static void main(String[] args) throws Exception { SpringApplicationBuilder builder = new SpringApplicationBuilder(Main.class); builder.headless(false).run(args); } }

2)在春季启动中,是否还可以移动/删除电子邮件,创建文件夹以及使用电子邮件帐户执行其他操作,或者我必须自己使用javax.mail库?

如果是,请提供一些例子吗?我正在尝试从gmail服务器接收电子邮件,然后以某种方式处理它们。因此,每当新电子邮件到达邮件服务器时,我的应用程序都应下载并处理它,这意味着调用...
java spring spring-boot spring-integration javamail
1个回答
0
投票
您的集成流程正确。 IMAP入站通道适配器本质上是异步的,它产生了一个线程来循环新电子邮件。但是,如果您担心for (EmailAction emailAction : services) {,则需要考虑自己将这一部分设为异步。您绝对可以将PublishSubscribeChannelExecutor和几个订户一起使用,以真正在那些服务中并行处理同一封电子邮件。
© www.soinside.com 2019 - 2024. All rights reserved.