增加eventbus,默认guava实现

This commit is contained in:
2021-04-05 23:00:08 +08:00
parent 0c33f6a919
commit c1d2359ef3
6 changed files with 122 additions and 1 deletions

View File

@@ -21,7 +21,7 @@ allprojects {
subprojects {
ext {
version '0.7.0'
version '0.8.0'
spring_version = "2.3.0.RELEASE"
}

View File

@@ -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 {

View File

@@ -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)
}
}

View File

@@ -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<Method> = bean.javaClass.methods
for (method in methods) {
// check the annotations on that method
val annotations: Array<Annotation> = 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
}
}

View File

@@ -0,0 +1,4 @@
package com.synebula.gaea.event
interface IEvent {
}

View File

@@ -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)
}