说我有以下内容:
配置
设置guava EventBus bean。
@Configuration
public class AppConfig {
@Bean
public EventBus eventBus() {
return new EventBus(); // guava event bus
}
}
事件监听器
侦听并处理放在事件总线上的部分/全部事件。 可能有几个类似于此的订阅应用程序中的事件的类。
@Component
public class EventListener {
@Autowired
public EventListener(EventBus eventBus) {
eventBus.register(this); // register this instance with the event bus so it receives any events
}
@Subscribe
public void onTestEvent(TestEvent e) {
System.out.println("Caught event");
}
}
事件调度程序
将事件放在事件总线上的东西。
@Component
public class EventDispatcher {
@Autowired
public EventDispatcher(EventBus eventBus) {
eventBus.post(new TestEvent());
}
}
主要应用入口点
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如何确保在发布事件的类之前实例化所有正在侦听事件的类?即目前,可以实例化事件调度程序类并在实例化侦听器之前发布事件,这意味着侦听器将错过事件。
我在考虑创建另一个注释,比如@EventListener,然后让spring以某种方式使用这个注释优先处理所有类,但不知道如何实现它或者无论如何这实际上是一个好主意。
感谢。
答案 0 :(得分:2)
我决定扩展Guava EventBus
类,创建BufferedEventBus
。
这将简单地缓冲发布到它的任何事件对象,直到应用程序上下文完成加载。当发生这种情况时,它会发布任何缓冲的事件。任何未来事件都会立即发布而不是缓冲。
<强> BufferedEventBus 强>
package tjenkinson.events;
import java.util.ArrayList;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import com.google.common.eventbus.EventBus;
/**
* A guava event bus that holds back any events that are posted to it until the application
* context has finished loading meaning all eager-loaded beans will have been constructed.
*
* This means any eager-loaded beans that want to listen to events on this event bus will be able
* to and not miss any events that were posted from other beans before they had a chance to listen.
*/
@Service
public class BufferedEventBus extends EventBus implements ApplicationListener<ContextRefreshedEvent> {
private boolean applicationContextLoaded = false;
private final ArrayList<Object> bufferedEvents = new ArrayList<>();
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
synchronized(bufferedEvents) {
if (applicationContextLoaded) {
// context already loaded. maybe it's been refreshed.
return;
}
postBufferedEvents();
applicationContextLoaded = true;
}
}
@Override
public void post(Object event) {
synchronized(bufferedEvents) {
if (applicationContextLoaded) {
super.post(event);
}
else {
bufferedEvents.add(event);
}
}
}
private void postBufferedEvents() {
synchronized(bufferedEvents) {
for (Object event : bufferedEvents) {
super.post(event);
}
bufferedEvents.clear();
}
}
}
我现在只是在我的应用程序中使用它而不是标准EventBus
。