-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($EventBus): support auto-configured Guava event bus
- Loading branch information
1 parent
65f0022
commit 6938193
Showing
8 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
auth-center/auth-center-biz/src/main/kotlin/com/jmsoftware/maf/authcenter/event/EventTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...-starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EnableEventBus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
42 changes: 42 additions & 0 deletions
42
...r/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventBugConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...starter/src/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriber.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
65 changes: 65 additions & 0 deletions
65
...rc/main/kotlin/com/jmsoftware/maf/springcloudstarter/eventbus/EventSubscriberRegistrar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |