【问题标题】:Vaadin and receiving email asynchronouoslyVaadin 和异步接收电子邮件
【发布时间】:2021-08-25 06:24:15
【问题描述】:

我的 Vaadin 14 应用程序应该在后台接收电子邮件。如果收到带有特定主题的电子邮件,则应通过 UI 上的 PUSH 消息通知用户。 对于整个电子邮件处理,我从 Spring 集成中实现了电子邮件/消息处理,这也很有效。两个 bean(IntegrationFlow 和一个 ServiceActivator)通过 Spring Application Context 中的 @Configuration 和 @Bean 注解生成,如下所示:

@Configuration
public class EmailReceiver {

    @Bean
    public HeaderMapper<MimeMessage> mailHeaderMapper() {
        return new DefaultMailHeaderMapper();
    }

    @Bean
    public IntegrationFlow imapMailFlow() {    
        IntegrationFlow flow =  IntegrationFlows
                .from(Mail.imapInboundAdapter("imaps://user:pass@imap.ionos.de/INBOX")
                                .userFlag("testSIUserFlag")
                                .javaMailProperties(new Properties()),
                        e -> e.autoStartup(true)
                                .poller(p -> p.fixedDelay(5000)))
                .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;
    }
    
    @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 received");
            }
        };
        return messageHandler;
    }
}

参见此处:https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail-java-dsl-configuration

有了这样一个@Configuration 注释类,电子邮件在 Vaadin 应用程序的后台接收。检查。

但是如何在方法EmailReceiver.processNewEmail 中将回调集成到 Vaadin 视图中?

    @Bean
    @ServiceActivator(inputChannel = "imapChannel")
    public MessageHandler processNewEmail(UI ui) {

这总是在应用程序启动时引发错误:Scope vaadin-ui is not active for the current thread; consider defining a scoped proxy for this bean

有使用 Vaadin https://vaadin.com/docs/v14/flow/advanced/tutorial-push-access 进行异步更新的示例。 与此相反,我必须为@ServiceActivator 处理创建一个@Bean。一旦出现这种情况,总是会出现错误There is no UI available. The UI scope is not active

如果我将方法 processNewEmail() 移动到单独的类中,我仍然无法引用 Vaadin UI:

    @MessageEndpoint
    class EmailMessageHandler  {

        private UI ui;
        
        public EmailMessageHandler(UI ui) {
            this.ui = ui;
        }
        
        @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 received" + message);
            }
        };
        return messageHandler;
        }
    }

如何结合 Vaadin 异步处理和 Spring-Integration Email/ServiceActivator 处理?

【问题讨论】:

  • 它如何知道使用哪个 UI?每个用户的每个选项卡/窗口都是一个单独的 UI。当UI.getCurrent() 不返回null 时,UI 范围基本上在请求期间处于活动状态。您可能希望 UI 添加某种侦听器或以其他方式将自己注册到 MessageHandler

标签: spring-integration spring-integration-dsl vaadin-flow vaadin14


【解决方案1】:

关键是您的邮件接收功能是每个应用程序的单例。另一方面,您将拥有与向您的应用程序发出 HTTP 请求的用户一样多的 UI。因此,您需要考虑一些中介来转储电子邮件并在 UI 请求发生时从那里获取它们。

您已经拥有 imapChannel 作为 QueueChannel。因此,您可以从您的 UI 范围代码中获取它并调用其receive() API 来提取下一条消息。唯一的问题是它是一个队列:只要一个呼叫receive(),另一个就不会看到相同的消息。到目前为止,这可能对您来说还可以,但最好考虑一下在消息传递方面可以被视为 topic 的东西。一个易于使用的好候选是 Reactor 的 Sinks.Many: https://projectreactor.io/docs/core/release/reference/#sinks

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多