From c1d2359ef3e54ab5bea1c0d06b41d35e3c44f849 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 5 Apr 2021 23:00:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0eventbus=EF=BC=8C=E9=BB=98?= =?UTF-8?q?=E8=AE=A4guava=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- src/gaea.app/build.gradle | 2 + .../synebula/gaea/app/component/EventBus.kt | 42 +++++++++++++++++ .../component/EventBusSubscriberProcessor.kt | 45 +++++++++++++++++++ .../kotlin/com/synebula/gaea/event/IEvent.kt | 4 ++ .../com/synebula/gaea/event/IEventBus.kt | 28 ++++++++++++ 6 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBus.kt create mode 100644 src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBusSubscriberProcessor.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/event/IEvent.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/event/IEventBus.kt diff --git a/build.gradle b/build.gradle index 9ec2799..2076d88 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ allprojects { subprojects { ext { - version '0.7.0' + version '0.8.0' spring_version = "2.3.0.RELEASE" } diff --git a/src/gaea.app/build.gradle b/src/gaea.app/build.gradle index 0cd2a26..d748678 100644 --- a/src/gaea.app/build.gradle +++ b/src/gaea.app/build.gradle @@ -6,6 +6,8 @@ dependencies { compile group: 'net.sf.dozer', name: 'dozer', version: '5.5.1' compile group: 'org.apache.poi', name: 'poi', version: '4.1.2' compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6' + compile group: 'com.google.guava', name: 'guava', version: '30.1.1-jre' + } publishing { diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBus.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBus.kt new file mode 100644 index 0000000..8448054 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBus.kt @@ -0,0 +1,42 @@ +package com.synebula.gaea.app.component + +import com.google.common.eventbus.AsyncEventBus +import com.google.common.eventbus.EventBus +import com.synebula.gaea.event.IEvent +import com.synebula.gaea.event.IEventBus +import org.springframework.stereotype.Component +import java.util.concurrent.Executors + +@Component +class EventBus : IEventBus { + + /** + * 同步事件总线 + */ + var eventBus = EventBus() + + /** + * 异步事件总线 + */ + var asyncEventBus = AsyncEventBus(Executors.newFixedThreadPool(2)) + + override fun register(obj: Any) { + eventBus.register(obj) + asyncEventBus.register(obj) + } + + override fun unregister(obj: Any) { + eventBus.unregister(obj) + asyncEventBus.unregister(obj) + } + + override fun publish(event: IEvent) { + eventBus.post(event) + } + + override fun publishAsync(event: IEvent) { + asyncEventBus.post(event) + } + +} + diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBusSubscriberProcessor.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBusSubscriberProcessor.kt new file mode 100644 index 0000000..98226c4 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/EventBusSubscriberProcessor.kt @@ -0,0 +1,45 @@ +package com.synebula.gaea.app.component + +import com.google.common.eventbus.Subscribe +import com.synebula.gaea.event.IEventBus +import org.springframework.beans.BeansException +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.config.BeanPostProcessor +import org.springframework.stereotype.Component +import java.lang.reflect.Method + + +@Component +class EventBusSubscriberProcessor : BeanPostProcessor { + + // 事件总线bean由Spring IoC容器负责创建,这里只需要通过@Autowired注解注入该bean即可使用事件总线 + @Autowired + var eventBus: IEventBus? = null + + @Throws(BeansException::class) + override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any { + return bean + } + + //对于每个容器执行了初始化的 bean,如果这个 bean 的某个方法注解了@Subscribe,则将该 bean 注册到事件总线 + @Throws(BeansException::class) + override fun postProcessAfterInitialization(bean: Any, beanName: String): Any { + // for each method in the bean + val methods: Array = bean.javaClass.methods + for (method in methods) { + // check the annotations on that method + val annotations: Array = method.getAnnotations() + for (annotation in annotations) { + // if it contains the Subscribe annotation + if (annotation.annotationClass == Subscribe::class) { + // 如果这是一个Guava @Subscribe注解的事件监听器方法,说明所在bean实例 + // 对应一个Guava事件监听器类,将该bean实例注册到Guava事件总线 + eventBus?.register(bean) + return bean + } + } + } + return bean + } + +} \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/event/IEvent.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/event/IEvent.kt new file mode 100644 index 0000000..59132cb --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/event/IEvent.kt @@ -0,0 +1,4 @@ +package com.synebula.gaea.event + +interface IEvent { +} \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/event/IEventBus.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/event/IEventBus.kt new file mode 100644 index 0000000..c0b6850 --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/event/IEventBus.kt @@ -0,0 +1,28 @@ +package com.synebula.gaea.event + +interface IEventBus { + + /** + * 注册事件Listener + * @param obj Listener所在类 + */ + fun register(obj: Any) + + /** + * 取消注册事件Listener + * @param obj Listener所在类 + */ + fun unregister(obj: Any) + + /** + * 同步发布事件 + * @param event 事件 + */ + fun publish(event: IEvent) + + /** + * 异步发布事件 + * @param event 事件 + */ + fun publishAsync(event: IEvent) +} \ No newline at end of file