From 693819322194611de504804fed951ba9dd0a862d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Thu, 23 Jun 2022 22:54:34 +0800 Subject: [PATCH] feat($EventBus): support auto-configured Guava event bus --- .../maf/authcenter/event/EventTest.kt | 17 +++++ .../maf/authcenter/AuthCenterApplication.kt | 2 + .../maf/mafmis/MafMisApplication.kt | 2 + .../maf/osscenter/OssCenterApplication.kt | 2 + .../eventbus/EnableEventBus.kt | 16 +++++ .../eventbus/EventBugConfiguration.kt | 42 ++++++++++++ .../eventbus/EventSubscriber.kt | 10 +++ .../eventbus/EventSubscriberRegistrar.kt | 65 +++++++++++++++++++ 8 files changed, 156 insertions(+) create mode 100644 auth-center/auth-center-biz/src/main/kotlin/com/jmsoftware/maf/authcenter/event/EventTest.kt create mode 100644 spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EnableEventBus.kt create mode 100644 spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventBugConfiguration.kt create mode 100644 spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriber.kt create mode 100644 spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriberRegistrar.kt diff --git a/auth-center/auth-center-biz/src/main/kotlin/com/jmsoftware/maf/authcenter/event/EventTest.kt b/auth-center/auth-center-biz/src/main/kotlin/com/jmsoftware/maf/authcenter/event/EventTest.kt new file mode 100644 index 00000000..11564935 --- /dev/null +++ b/auth-center/auth-center-biz/src/main/kotlin/com/jmsoftware/maf/authcenter/event/EventTest.kt @@ -0,0 +1,17 @@ +package com.jmsoftware.maf.authcenter.event + +import com.google.common.eventbus.EventBus +import com.jmsoftware.maf.springcloudstarter.eventbus.EventSubscriber + +/** + * # EventTest + * + * Change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/23/22 7:46 AM + **/ +@EventSubscriber +class EventTest( + private val eventBus: EventBus +) { +} diff --git a/auth-center/auth-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/authcenter/AuthCenterApplication.kt b/auth-center/auth-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/authcenter/AuthCenterApplication.kt index b691506a..0a1d8d15 100644 --- a/auth-center/auth-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/authcenter/AuthCenterApplication.kt +++ b/auth-center/auth-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/authcenter/AuthCenterApplication.kt @@ -1,5 +1,6 @@ package com.jmsoftware.maf.authcenter +import com.jmsoftware.maf.springcloudstarter.eventbus.EnableEventBus import com.jmsoftware.maf.springcloudstarter.helper.SpringBootStartupHelper import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @@ -14,6 +15,7 @@ import org.springframework.util.StopWatch * * @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com, date: 4/10/22 11:44 AM */ +@EnableEventBus @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication diff --git a/maf-mis/maf-mis-bootstrap/src/main/kotlin/com/jmsoftware/maf/mafmis/MafMisApplication.kt b/maf-mis/maf-mis-bootstrap/src/main/kotlin/com/jmsoftware/maf/mafmis/MafMisApplication.kt index b73ae5a7..c8304217 100644 --- a/maf-mis/maf-mis-bootstrap/src/main/kotlin/com/jmsoftware/maf/mafmis/MafMisApplication.kt +++ b/maf-mis/maf-mis-bootstrap/src/main/kotlin/com/jmsoftware/maf/mafmis/MafMisApplication.kt @@ -1,5 +1,6 @@ package com.jmsoftware.maf.mafmis +import com.jmsoftware.maf.springcloudstarter.eventbus.EnableEventBus import com.jmsoftware.maf.springcloudstarter.helper.SpringBootStartupHelper import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @@ -14,6 +15,7 @@ import org.springframework.util.StopWatch * * @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com, date: 4/16/22 11:23 PM */ +@EnableEventBus @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication diff --git a/oss-center/oss-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/osscenter/OssCenterApplication.kt b/oss-center/oss-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/osscenter/OssCenterApplication.kt index d847568b..1b680a79 100644 --- a/oss-center/oss-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/osscenter/OssCenterApplication.kt +++ b/oss-center/oss-center-bootstrap/src/main/kotlin/com/jmsoftware/maf/osscenter/OssCenterApplication.kt @@ -1,5 +1,6 @@ package com.jmsoftware.maf.osscenter +import com.jmsoftware.maf.springcloudstarter.eventbus.EnableEventBus import com.jmsoftware.maf.springcloudstarter.helper.SpringBootStartupHelper import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @@ -14,6 +15,7 @@ import org.springframework.util.StopWatch * * @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com, date: 4/17/22 8:47 AM */ +@EnableEventBus @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication diff --git a/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EnableEventBus.kt b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EnableEventBus.kt new file mode 100644 index 00000000..c4db868e --- /dev/null +++ b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EnableEventBus.kt @@ -0,0 +1,16 @@ +package com.jmsoftware.maf.springcloudstarter.eventbus + +import org.springframework.context.annotation.Import + +/** + * # EnableEventBus + * + * Change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/23/22 7:52 AM + **/ +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@MustBeDocumented +@Import(EventBugConfiguration::class) +annotation class EnableEventBus diff --git a/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventBugConfiguration.kt b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventBugConfiguration.kt new file mode 100644 index 00000000..560aa18c --- /dev/null +++ b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventBugConfiguration.kt @@ -0,0 +1,42 @@ +package com.jmsoftware.maf.springcloudstarter.eventbus + +import com.google.common.eventbus.EventBus +import com.jmsoftware.maf.common.util.logger +import com.jmsoftware.maf.springcloudstarter.property.MafProjectProperties +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass +import org.springframework.boot.context.event.ApplicationReadyEvent +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Import +import org.springframework.context.event.EventListener + +/** + * # EventBugConfiguration + * + * Change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/23/22 7:52 AM + **/ +@ConditionalOnClass(EventBus::class) +@Import(EventSubscriberRegistrar::class) +class EventBugConfiguration( + private val mafProjectProperties: MafProjectProperties, + private val applicationContext: ApplicationContext +) { + companion object { + private val log = logger() + } + + @Bean + fun eventBus(): EventBus = EventBus("event-bus-${mafProjectProperties.projectArtifactId}") + + @EventListener + fun onApplicationEvent(event: ApplicationReadyEvent) { + log.info("All Beans have been initialized. Elapsed: ${event.timeTaken.seconds} s") + val eventBus = applicationContext.getBean(EventBus::class.java) + applicationContext.getBeansWithAnnotation(EventSubscriber::class.java).forEach { (key, value) -> + eventBus.register(value) + log.info("Event bus registered subscriber: $key") + } + } +} diff --git a/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriber.kt b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriber.kt new file mode 100644 index 00000000..fd55be52 --- /dev/null +++ b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriber.kt @@ -0,0 +1,10 @@ +package com.jmsoftware.maf.springcloudstarter.eventbus + +/** + * # EventSubscriber + * + * Change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/23/22 7:52 AM + **/ +annotation class EventSubscriber diff --git a/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriberRegistrar.kt b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriberRegistrar.kt new file mode 100644 index 00000000..153d0cda --- /dev/null +++ b/spring-cloud-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriberRegistrar.kt @@ -0,0 +1,65 @@ +package com.jmsoftware.maf.springcloudstarter.eventbus + +import com.google.common.eventbus.Subscribe +import com.jmsoftware.maf.common.util.logger +import org.springframework.beans.factory.config.BeanDefinitionHolder +import org.springframework.beans.factory.support.AbstractBeanDefinition +import org.springframework.beans.factory.support.BeanDefinitionRegistry +import org.springframework.context.ResourceLoaderAware +import org.springframework.context.annotation.ClassPathBeanDefinitionScanner +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar +import org.springframework.core.io.ResourceLoader +import org.springframework.core.type.AnnotationMetadata +import org.springframework.core.type.filter.AnnotationTypeFilter + +/** + * # EventSubscriberRegistrar + * + * Change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/22/22 10:44 PM + * @see Subscribe + **/ +class EventSubscriberRegistrar : ImportBeanDefinitionRegistrar, ResourceLoaderAware { + companion object { + private val log = logger() + } + + private lateinit var resourceLoaderMember: ResourceLoader + + override fun registerBeanDefinitions(importingClassMetadata: AnnotationMetadata, registry: BeanDefinitionRegistry) { + EventSubscriberBeanDefinitionScanner(registry).apply { + this.resourceLoader = resourceLoaderMember + }.scan("com.jmsoftware.maf").apply { + log.warn("Number of beans registered for @${EventSubscriber::class.simpleName}: $this") + } + } + + override fun setResourceLoader(resourceLoader: ResourceLoader) { + resourceLoaderMember = resourceLoader + } +} + +/** + * # EventSubscriberBeanDefinitionScanner + * + * Change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/22/22 10:51 PM + **/ +private class EventSubscriberBeanDefinitionScanner(registry: BeanDefinitionRegistry) : + ClassPathBeanDefinitionScanner(registry, false) { + companion object { + private val log = logger() + } + + override fun doScan(vararg basePackages: String?): MutableSet { + addIncludeFilter(AnnotationTypeFilter(EventSubscriber::class.java)) + return super.doScan(*basePackages) + } + + override fun postProcessBeanDefinition(beanDefinition: AbstractBeanDefinition, beanName: String) { + log.info("Post process bean definition: $beanName") + super.postProcessBeanDefinition(beanDefinition, beanName) + } +}