【发布时间】: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;
}
}
有了这样一个@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