Skip to content

Commit

Permalink
feat(spring): Support automatic initialization of index templates (#991)
Browse files Browse the repository at this point in the history
* feat(spring): Support automatic initialization of index templates
  • Loading branch information
Ahoo-Wang authored Nov 12, 2024
1 parent b16799c commit ec2ac79
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package me.ahoo.wow.elasticsearch

import com.fasterxml.jackson.databind.JsonNode
import com.google.common.io.Resources
import me.ahoo.wow.messaging.dispatcher.SafeSubscriber
import me.ahoo.wow.serialization.toJsonString
import me.ahoo.wow.serialization.toObject
import org.springframework.core.io.ClassPathResource
Expand Down Expand Up @@ -69,4 +70,11 @@ class IndexTemplateInitializer(private val elasticsearchOperations: ReactiveElas
return elasticsearchOperations.indexOps(IndexCoordinates.of(name))
.putIndexTemplate(putIndexTemplateRequest)
}

fun initAll() {
initEventStreamTemplate().subscribe(InitSubscriber("InitEventStreamTemplate"))
initSnapshotTemplate().subscribe(InitSubscriber("InitEventStreamTemplate"))
}

class InitSubscriber(override val name: String) : SafeSubscriber<Boolean>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package me.ahoo.wow.spring.boot.starter.elasticsearch

import co.elastic.clients.json.jackson.JacksonJsonpMapper
import me.ahoo.wow.elasticsearch.IndexTemplateInitializer
import me.ahoo.wow.elasticsearch.WowJsonpMapper
import me.ahoo.wow.elasticsearch.eventsourcing.ElasticsearchEventStore
import me.ahoo.wow.elasticsearch.eventsourcing.ElasticsearchSnapshotRepository
Expand All @@ -35,12 +36,13 @@ import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestCli
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations

@AutoConfiguration(after = [ElasticsearchRestClientAutoConfiguration::class])
@ConditionalOnWowEnabled
@ConditionalOnClass(ElasticsearchEventStore::class)
@EnableConfigurationProperties(ElasticsearchProperties::class)
class ElasticsearchEventSourcingAutoConfiguration {
class ElasticsearchEventSourcingAutoConfiguration(private val elasticsearchProperties: ElasticsearchProperties) {

@Bean
fun jacksonJsonpMapper(): JacksonJsonpMapper {
Expand All @@ -59,6 +61,20 @@ class ElasticsearchEventSourcingAutoConfiguration {
return ElasticsearchEventStore(elasticsearchClient)
}

@Bean
@ConditionalOnProperty(
EventStoreProperties.STORAGE,
matchIfMissing = true,
havingValue = StorageType.ELASTICSEARCH_NAME,
)
fun indexTemplateInitializer(elasticsearchOperations: ReactiveElasticsearchOperations): IndexTemplateInitializer {
val initializer = IndexTemplateInitializer(elasticsearchOperations)
if (elasticsearchProperties.autoInitTemplate) {
initializer.initAll()
}
return initializer
}

@Bean
@ConditionalOnProperty(
EventStoreProperties.STORAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package me.ahoo.wow.spring.boot.starter.elasticsearch

import co.elastic.clients.json.jackson.JacksonJsonpMapper
import io.mockk.every
import io.mockk.mockk
import me.ahoo.wow.elasticsearch.IndexTemplateInitializer
import me.ahoo.wow.elasticsearch.eventsourcing.ElasticsearchEventStore
import me.ahoo.wow.elasticsearch.eventsourcing.ElasticsearchSnapshotRepository
import me.ahoo.wow.elasticsearch.query.event.ElasticsearchEventStreamQueryServiceFactory
Expand All @@ -28,19 +31,31 @@ import org.mockito.Mockito.mock
import org.springframework.boot.test.context.assertj.AssertableApplicationContext
import org.springframework.boot.test.context.runner.ApplicationContextRunner
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates
import reactor.kotlin.core.publisher.toMono

internal class ElasticsearchEventSourcingAutoConfigurationTest {
private val contextRunner = ApplicationContextRunner()

@Test
fun contextLoads() {
val elasticsearchTemplate = mockk<ReactiveElasticsearchOperations> {
every { indexOps(any<IndexCoordinates>()) } returns mockk<ReactiveIndexOperations> {
every { putIndexTemplate(any()) } returns true.toMono()
}
}
contextRunner
.enableWow()
.withPropertyValues("${SnapshotProperties.STORAGE}=${StorageType.ELASTICSEARCH_NAME}")
.withPropertyValues("${EventStoreProperties.STORAGE}=${StorageType.ELASTICSEARCH_NAME}")
.withBean(ReactiveElasticsearchClient::class.java, {
mock(ReactiveElasticsearchClient::class.java)
})
.withBean(ReactiveElasticsearchOperations::class.java, {
elasticsearchTemplate
})
.withUserConfiguration(
ElasticsearchEventSourcingAutoConfiguration::class.java,
)
Expand All @@ -49,6 +64,7 @@ internal class ElasticsearchEventSourcingAutoConfigurationTest {
.hasSingleBean(JacksonJsonpMapper::class.java)
.hasSingleBean(ElasticsearchEventStore::class.java)
.hasSingleBean(ElasticsearchEventStreamQueryServiceFactory::class.java)
.hasSingleBean(IndexTemplateInitializer::class.java)
.hasSingleBean(ElasticsearchSnapshotRepository::class.java)
.hasSingleBean(ElasticsearchSnapshotQueryServiceFactory::class.java)
}
Expand Down

0 comments on commit ec2ac79

Please sign in to comment.