[英]dealing with non-serializable Guava EventBus in Vaadin with session serialization
[英]Guava EventBus in globally (singleton) and session based in Vaadin with Spring Boot
我正在嘗試將Guava EventBus
作為Singleton和基於Spring引導的Vaadin
應用程序中的會話進行獲取,但到目前為止我無法正常工作。 基於會話的會話有效,但不基於單會話的會話有效。該想法是實現全局通知服務,以便特定用戶在發生新事件時得到通知。 我嘗試了以下操作:
public class Configuration{
@Scope("singleton")
@Bean
public EventBus globalEventBus(){
return new EventBus("globalEventBus");
}
@SessionScoped
@Bean
public EventBus eventBus(){
return new EventBus();
}
}
嘿,我這樣解決了:
該代碼如下所示:
@SpringUI
public class TimeSaverUI extends UI {
private TimeSaverEventBus eventBus = new TimeSaverEventBus();
@Override
protected void init(VaadinRequest vaadinRequest) {
TimeSaverEventBus.register(this);
updateContent();
}
private void updateContent() {
...
}
...
public static TimeSaverEventBus getTimeSaverEventbus() {
return ((TimeSaverUI) getCurrent()).eventBus;
}
}
和EvenBusWrapper:
public class TimeSaverEventBus implements SubscriberExceptionHandler {
private final EventBus eventBus = new EventBus(this);
public static void post(final Object event) {
TimeSaverUI.getTimeSaverEventbus().eventBus.post(event);
}
public static void register(final Object object) {
TimeSaverUI.getTimeSaverEventbus().eventBus.register(object);
}
public static void unregister(final Object object) {
TimeSaverUI.getTimeSaverEventbus().eventBus.unregister(object);
}
@Override
public final void handleException(final Throwable exception,
final SubscriberExceptionContext context) {
exception.printStackTrace();
}
}
通過這種方式實現,您可以通過調用TimeSaverEventBus的靜態方法來注冊和注銷事件偵聽器或發布事件。
我真的希望這是您想要的。
干杯,菲利克斯
我認為您需要做的就是為您的bean使用一個名稱。 所以它會像這樣工作。
public class Configuration{
@Scope("singleton")
@Bean(name="globalEventBus")
public EventBus globalEventBus(){
return new EventBus("globalEventBus");
}
@SessionScoped
@Bean(name="sessionEventBus")
public EventBus eventBus(){
return new EventBus();
}
}
然后,當您想自動接線時,使用此
@Autowired
@Qualifier("globalEventBus")
private EventBus globalEventBus;
@Autowired
@Qualifier("sessionEventBus")
private EventBus sessionEventBus;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.