Skip to content

Commit

Permalink
feat($EventBus): support auto-configured Guava event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 23, 2022
1 parent 65f0022 commit 6938193
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<BeanDefinitionHolder> {
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)
}
}

0 comments on commit 6938193

Please sign in to comment.