-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add autoconfigure-adapter for spring-data-elasticsearch for hig…
…h level rest client and reactive client feat: Add Jafu Dsl for spring-data elasticsearch and reactive elasticsearch feat: Add Kofu dsl for spring-data elasticsearch and reactive elasticsearch
- Loading branch information
Showing
15 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
...g/springframework/boot/autoconfigure/data/elasticsearch/ElasticSearchDataInitializer.java
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,21 @@ | ||
package org.springframework.boot.autoconfigure.data.elasticsearch; | ||
|
||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.data.elasticsearch.client.ClientConfiguration; | ||
import org.springframework.data.elasticsearch.client.RestClients; | ||
|
||
public class ElasticSearchDataInitializer implements ApplicationContextInitializer<GenericApplicationContext> { | ||
|
||
private final ClientConfiguration clientConfiguration; | ||
|
||
public ElasticSearchDataInitializer(ClientConfiguration clientConfiguration) { | ||
this.clientConfiguration = clientConfiguration; | ||
} | ||
|
||
@Override | ||
public void initialize(GenericApplicationContext context) { | ||
context.registerBean(RestHighLevelClient.class, () -> RestClients.create(clientConfiguration).rest()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...framework/boot/autoconfigure/data/elasticsearch/ReactiveElasticSearchDataInitializer.java
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,21 @@ | ||
package org.springframework.boot.autoconfigure.data.elasticsearch; | ||
|
||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.data.elasticsearch.client.ClientConfiguration; | ||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; | ||
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients; | ||
|
||
public class ReactiveElasticSearchDataInitializer implements ApplicationContextInitializer<GenericApplicationContext> { | ||
|
||
private final ClientConfiguration clientConfiguration; | ||
|
||
public ReactiveElasticSearchDataInitializer(ClientConfiguration clientConfiguration) { | ||
this.clientConfiguration = clientConfiguration; | ||
} | ||
|
||
@Override | ||
public void initialize(GenericApplicationContext context) { | ||
context.registerBean(ReactiveElasticsearchClient.class, () -> ReactiveRestClients.create(clientConfiguration)); | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
jafu/src/main/java/org/springframework/fu/jafu/elasticsearch/AbstractElasticSearchDsl.java
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,50 @@ | ||
package org.springframework.fu.jafu.elasticsearch; | ||
|
||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.data.elasticsearch.client.ClientConfiguration; | ||
import org.springframework.fu.jafu.AbstractDsl; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
public abstract class AbstractElasticSearchDsl<SELF extends AbstractElasticSearchDsl<SELF>> extends AbstractDsl { | ||
|
||
private final SELF self; | ||
private final Consumer<SELF> dsl; | ||
private Optional<String> hostAndPort = Optional.empty(); | ||
private Boolean useSsl = false; | ||
|
||
protected abstract SELF getSelf(); | ||
|
||
protected AbstractElasticSearchDsl(Consumer<SELF> dsl) { | ||
this.dsl = dsl; | ||
this.self = getSelf(); | ||
} | ||
|
||
public SELF hostAndPort(String hostAndPort) { | ||
this.hostAndPort = Optional.ofNullable(hostAndPort); | ||
return self; | ||
} | ||
|
||
public SELF useSsl(Boolean useSsl) { | ||
this.useSsl = useSsl; | ||
return self; | ||
} | ||
|
||
protected ClientConfiguration createClientConfiguration() { | ||
ClientConfiguration.ClientConfigurationBuilderWithRequiredEndpoint baseBuilder = ClientConfiguration.builder(); | ||
ClientConfiguration.MaybeSecureClientConfigurationBuilder maybeSecureBuilder = | ||
hostAndPort.map(baseBuilder::connectedTo).orElseGet(baseBuilder::connectedToLocalhost); | ||
ClientConfiguration.TerminalClientConfigurationBuilder terminalBuilder = maybeSecureBuilder; | ||
if (useSsl) { | ||
terminalBuilder = maybeSecureBuilder.usingSsl(); | ||
} | ||
return terminalBuilder.build(); | ||
} | ||
|
||
@Override | ||
public void initialize(GenericApplicationContext context) { | ||
super.initialize(context); | ||
dsl.accept(self); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
jafu/src/main/java/org/springframework/fu/jafu/elasticsearch/ElasticSearchDsl.java
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,46 @@ | ||
package org.springframework.fu.jafu.elasticsearch; | ||
|
||
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticSearchDataInitializer; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.data.elasticsearch.client.ClientConfiguration; | ||
import org.springframework.fu.jafu.AbstractDsl; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
public class ElasticSearchDsl extends AbstractElasticSearchDsl<ElasticSearchDsl> { | ||
|
||
public ElasticSearchDsl(Consumer<ElasticSearchDsl> dsl) { | ||
super(dsl); | ||
} | ||
|
||
/** | ||
* Configure Spring-data ElasticSearch support with default properties. | ||
* @see org.springframework.fu.jafu.ConfigurationDsl#enable(ApplicationContextInitializer) | ||
* @see org.springframework.fu.jafu.elasticsearch.ElasticSearchDsl | ||
*/ | ||
public static ApplicationContextInitializer<GenericApplicationContext> elasticSearch() { | ||
return new ElasticSearchDsl(mongoDsl -> {}); | ||
} | ||
|
||
/** | ||
* Configure Spring-data ElasticSearch with customized properties. | ||
* @see org.springframework.fu.jafu.ConfigurationDsl#enable(ApplicationContextInitializer) | ||
* @see org.springframework.fu.jafu.elasticsearch.ElasticSearchDsl | ||
*/ | ||
public static ApplicationContextInitializer<GenericApplicationContext> elasticSearch(Consumer<ElasticSearchDsl> dsl) { | ||
return new ElasticSearchDsl(dsl); | ||
} | ||
|
||
@Override | ||
protected ElasticSearchDsl getSelf() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public void initialize(GenericApplicationContext context) { | ||
super.initialize(context); | ||
new ElasticSearchDataInitializer(createClientConfiguration()).initialize(context); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
jafu/src/main/java/org/springframework/fu/jafu/elasticsearch/ReactiveElasticSearchDsl.java
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,43 @@ | ||
package org.springframework.fu.jafu.elasticsearch; | ||
|
||
import org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticSearchDataInitializer; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class ReactiveElasticSearchDsl extends AbstractElasticSearchDsl<ReactiveElasticSearchDsl> { | ||
|
||
public ReactiveElasticSearchDsl(Consumer<ReactiveElasticSearchDsl> dsl) { | ||
super(dsl); | ||
} | ||
|
||
/** | ||
* Configure Spring-data ElasticSearch support with default properties. | ||
* @see org.springframework.fu.jafu.ConfigurationDsl#enable(ApplicationContextInitializer) | ||
* @see org.springframework.fu.jafu.elasticsearch.ReactiveElasticSearchDsl | ||
*/ | ||
public static ApplicationContextInitializer<GenericApplicationContext> reactiveElasticSearch() { | ||
return new ReactiveElasticSearchDsl(mongoDsl -> {}); | ||
} | ||
|
||
/** | ||
* Configure Spring-data ElasticSearch with customized properties. | ||
* @see org.springframework.fu.jafu.ConfigurationDsl#enable(ApplicationContextInitializer) | ||
* @see org.springframework.fu.jafu.elasticsearch.ReactiveElasticSearchDsl | ||
*/ | ||
public static ApplicationContextInitializer<GenericApplicationContext> reactiveElasticSearch(Consumer<ReactiveElasticSearchDsl> dsl) { | ||
return new ReactiveElasticSearchDsl(dsl); | ||
} | ||
|
||
@Override | ||
protected ReactiveElasticSearchDsl getSelf() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public void initialize(GenericApplicationContext context) { | ||
super.initialize(context); | ||
new ReactiveElasticSearchDataInitializer(createClientConfiguration()).initialize(context); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
jafu/src/test/java/org/springframework/fu/jafu/elasticsearch/ElasticSearchDslTest.java
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 org.springframework.fu.jafu.elasticsearch; | ||
|
||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.fu.jafu.Jafu; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.springframework.fu.jafu.elasticsearch.ElasticSearchDsl.elasticSearch; | ||
|
||
class ElasticSearchDslTest { | ||
|
||
@Test | ||
public void enableElasticSearch() throws IOException { | ||
var es = new GenericContainer("elasticsearch:7.9.3") | ||
.withExposedPorts(9200) | ||
.withEnv("discovery.type", "single-node"); | ||
|
||
es.start(); | ||
var app = Jafu.application(a -> | ||
a.enable(elasticSearch(esDsl -> | ||
esDsl.hostAndPort("localhost:" + es.getFirstMappedPort())))); | ||
|
||
var context = app.run(); | ||
var client = context.getBean(RestHighLevelClient.class); | ||
assertNotNull(client); | ||
|
||
var request = new IndexRequest("spring-data") | ||
.source(Collections.singletonMap("feature", "high-level-rest-client")); | ||
var response = client.index(request, RequestOptions.DEFAULT); | ||
assertEquals(201, response.status().getStatus()); | ||
|
||
es.stop(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...src/test/java/org/springframework/fu/jafu/elasticsearch/ReactiveElasticSearchDslTest.java
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,44 @@ | ||
package org.springframework.fu.jafu.elasticsearch; | ||
|
||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; | ||
import org.springframework.fu.jafu.Jafu; | ||
import org.testcontainers.containers.GenericContainer; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.springframework.fu.jafu.elasticsearch.ReactiveElasticSearchDsl.reactiveElasticSearch; | ||
|
||
class ReactiveElasticSearchDslTest { | ||
|
||
@Test | ||
public void enableReactiveElasticSearch() throws IOException { | ||
var es = new GenericContainer("elasticsearch:7.9.3") | ||
.withExposedPorts(9200) | ||
.withEnv("discovery.type", "single-node"); | ||
|
||
es.start(); | ||
var app = Jafu.application(a -> | ||
a.enable(reactiveElasticSearch(esDsl -> | ||
esDsl.hostAndPort("localhost:" + es.getFirstMappedPort())))); | ||
|
||
var context = app.run(); | ||
var reactiveClient = context.getBean(ReactiveElasticsearchClient.class); | ||
assertNotNull(reactiveClient); | ||
|
||
var request = new IndexRequest("spring-data") | ||
.source(Collections.singletonMap("feature", "high-level-rest-client")); | ||
StepVerifier | ||
.create(reactiveClient.index(request)) | ||
.assertNext(next -> assertEquals(RestStatus.CREATED, next.status())) | ||
.verifyComplete(); | ||
es.stop(); | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
kofu/src/main/kotlin/org/springframework/fu/kofu/elasticsearch/AbstractElasticSearchDsl.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,21 @@ | ||
package org.springframework.fu.kofu.elasticsearch | ||
|
||
import org.springframework.data.elasticsearch.client.ClientConfiguration | ||
import org.springframework.fu.kofu.AbstractDsl | ||
|
||
abstract class AbstractElasticSearchDsl: AbstractDsl() { | ||
|
||
var hostAndPort: String? = null | ||
var usingSsl: Boolean = false | ||
|
||
protected fun createClientConfiguration() = | ||
ClientConfiguration.builder() | ||
.let { | ||
if (hostAndPort != null) it.connectedTo(hostAndPort) | ||
else it.connectedToLocalhost() | ||
} | ||
.let { | ||
if (usingSsl) it.usingSsl() | ||
else it | ||
}.build() | ||
} |
20 changes: 20 additions & 0 deletions
20
kofu/src/main/kotlin/org/springframework/fu/kofu/elasticsearch/ElasticSearchDsl.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,20 @@ | ||
package org.springframework.fu.kofu.elasticsearch | ||
|
||
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticSearchDataInitializer | ||
import org.springframework.context.support.GenericApplicationContext | ||
import org.springframework.data.elasticsearch.client.ClientConfiguration | ||
import org.springframework.fu.kofu.AbstractDsl | ||
import org.springframework.fu.kofu.ConfigurationDsl | ||
|
||
class ElasticSearchDsl(private val dsl: ElasticSearchDsl.() -> Unit): AbstractElasticSearchDsl() { | ||
|
||
override fun initialize(context: GenericApplicationContext) { | ||
super.initialize(context) | ||
apply(dsl) | ||
ElasticSearchDataInitializer(createClientConfiguration()).initialize(context) | ||
} | ||
} | ||
|
||
fun ConfigurationDsl.elasticSearch(dsl: ElasticSearchDsl.() -> Unit) { | ||
ElasticSearchDsl(dsl).initialize(context) | ||
} |
17 changes: 17 additions & 0 deletions
17
kofu/src/main/kotlin/org/springframework/fu/kofu/elasticsearch/ReactiveElasticSearchDsl.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 org.springframework.fu.kofu.elasticsearch | ||
|
||
import org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticSearchDataInitializer | ||
import org.springframework.context.support.GenericApplicationContext | ||
import org.springframework.fu.kofu.ConfigurationDsl | ||
|
||
class ReactiveElasticSearchDsl(private val dsl: ReactiveElasticSearchDsl.() -> Unit): AbstractElasticSearchDsl() { | ||
override fun initialize(context: GenericApplicationContext) { | ||
super.initialize(context) | ||
apply(dsl) | ||
ReactiveElasticSearchDataInitializer(createClientConfiguration()).initialize(context) | ||
} | ||
} | ||
|
||
fun ConfigurationDsl.reactiveElasticSearch(dsl: ReactiveElasticSearchDsl.() -> Unit) { | ||
ReactiveElasticSearchDsl(dsl).initialize(context) | ||
} |
Oops, something went wrong.