Skip to content

Commit 0ed9528

Browse files
committed
Add a "marker" config property to force the discovery of named clients
1 parent 68e8081 commit 0ed9528

File tree

8 files changed

+110
-5
lines changed

8 files changed

+110
-5
lines changed

docs/src/main/asciidoc/elasticsearch-dev-services.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ In that case, you will need to stop and remove these containers manually.
139139
If you want to reuse containers for some Quarkus applications but not all of them,
140140
or some Dev Services but not all of them,
141141
you can disable this feature for a specific Dev Service by setting the configuration property
142-
xref:elasticsearch-dev-services.adoc#quarkus-elasticsearch-rest-client_quarkus-elasticsearch-devservices_quarkus-elasticsearch-devservices-reuse[`quarkus.elasticsearch.devservices.reuse`]
142+
xref:elasticsearch-dev-services.adoc#quarkus-elasticsearch-rest-client_quarkus-elasticsearch-devservices-reuse[`quarkus.elasticsearch.devservices.reuse`]
143143
to `false`.
144144

145145
== Multiple Elasticsearch service instances
146146

147147
Dev services for Elasticsearch can start multiple instances as necessary.
148-
Currently this is only compatible with the `quarkus-elasticsearch-rest-client` extension.
148+
Currently, this is only compatible with the `quarkus-elasticsearch-rest-client` extension.
149149
A separate Dev Service will be started for each named Elasticsearch REST client.
150150

151151
== Current limitations
@@ -154,4 +154,4 @@ Currently, only the default backend for Hibernate Search Elasticsearch is suppor
154154

155155
== Configuration reference
156156

157-
include::{generated-dir}/config/quarkus-elasticsearch-rest-client_quarkus.elasticsearch.devservices.adoc[opts=optional, leveloffset=+1]
157+
include::{generated-dir}/config/quarkus-elasticsearch-rest-client_quarkus.elasticsearch.adoc[opts=optional, leveloffset=+1]

docs/src/main/asciidoc/elasticsearch.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,16 @@ public class FruitService {
640640
----
641641
<1> We specify the name of the client through the `@Identifier` when injecting a `ElasticsearchClient` within the service.
642642

643+
[NOTE]
644+
====
645+
While Quarkus will attempt to find all the named clients at the build time and prepare the required resources for them,
646+
in some cases, e.g. when the client is only accessed programmatically, it might not be possible to discover all of them.
647+
To ensure that all clients are correctly processed, you can use the `quarkus.elasticsearch.[client name].force-discovery`
648+
configuration property for each required client name.
649+
650+
Those clients defined at the build time that won't be necessary at runtime can be <<client-active,deactivated>>.
651+
====
652+
643653
== Hibernate Search Elasticsearch
644654

645655
Quarkus supports Hibernate Search with Elasticsearch via the `quarkus-hibernate-search-orm-elasticsearch`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.quarkus.elasticsearch.javaclient.deployment;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.elasticsearch.client.RestClient;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.RegisterExtension;
9+
10+
import co.elastic.clients.elasticsearch.ElasticsearchClient;
11+
import io.quarkus.arc.Arc;
12+
import io.quarkus.test.QuarkusUnitTest;
13+
import io.smallrye.common.annotation.Identifier;
14+
15+
public class ActiveInactiveClientProgrammaticAccessOnlyTest {
16+
17+
@RegisterExtension
18+
static final QuarkusUnitTest test = new QuarkusUnitTest()
19+
.withApplicationRoot((jar) -> jar
20+
.addAsResource("active-inactive-programmatic.properties", "application.properties"));
21+
22+
@Test
23+
void smoke() {
24+
assertTrue(Arc.container().select(RestClient.class).getHandle().getBean().isActive());
25+
assertFalse(
26+
Arc.container().select(RestClient.class, Identifier.Literal.of("client2")).getHandle().getBean().isActive());
27+
assertTrue(Arc.container().select(RestClient.class, Identifier.Literal.of("client3")).getHandle().getBean().isActive());
28+
29+
assertTrue(Arc.container().select(ElasticsearchClient.class).getHandle().getBean().isActive());
30+
assertFalse(Arc.container().select(ElasticsearchClient.class, Identifier.Literal.of("client2")).getHandle().getBean()
31+
.isActive());
32+
assertFalse(Arc.container().select(ElasticsearchClient.class, Identifier.Literal.of("client3")).getHandle().getBean()
33+
.isActive());
34+
}
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
quarkus.elasticsearch.devservices.enabled=false
2+
3+
quarkus.elasticsearch.active=true
4+
# We shouldn't need the "discovery flag" for the default client,
5+
# since it is supposed to be always "discovered":
6+
# quarkus.elasticsearch.force-discovery=true
7+
quarkus.elasticsearch."client2".active=false
8+
quarkus.elasticsearch."client2".force-discovery=true
9+
quarkus.elasticsearch."client3".active=true
10+
quarkus.elasticsearch."client3".force-discovery=true
11+
12+
quarkus.elasticsearch-java.active=true
13+
quarkus.elasticsearch-java."client2".active=false
14+
quarkus.elasticsearch-java."client3".active=false

extensions/elasticsearch-rest-client-common/deployment/src/main/java/io/quarkus/elasticsearch/restclient/common/deployment/ElasticsearchClientProcessorUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static Set<String> collectReferencedClientNames(CombinedIndexBuildItem in
3737
BeanRegistrationPhaseBuildItem registrationPhase,
3838
Set<DotName> clientTypeNames, Set<DotName> configAnnotationNames) {
3939
Set<String> referencedNames = new HashSet<>();
40-
// Always start with the default:
41-
referencedNames.add(ElasticsearchClientBeanUtil.DEFAULT_ELASTICSEARCH_CLIENT_NAME);
40+
// Always start with the default:
41+
referencedNames.add(ElasticsearchClientBeanUtil.DEFAULT_ELASTICSEARCH_CLIENT_NAME);
4242
IndexView indexView = indexBuildItem.getIndex();
4343
for (DotName annotationName : configAnnotationNames) {
4444
for (AnnotationInstance annotation : indexView.getAnnotations(annotationName)) {

extensions/elasticsearch-rest-client/deployment/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/deployment/ElasticsearchLowLevelClientBuildTimeConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ public interface ElasticsearchLowLevelClientBuildTimeConfig {
1212
@WithName("health.enabled")
1313
@WithDefault("true")
1414
boolean healthEnabled();
15+
16+
/**
17+
* This property is to help Quarkus identify any low-level Elasticsearch REST clients
18+
* that possibly are not explicitly injected in the application code with their corresponding identifier, e.g.
19+
* {@code @Inject @Identifier("client-name") RestClient client;}.
20+
* In such cases, this property is required to help Quarkus detect all the necessary clients at build time.
21+
* The value of this configuration property is ignored.
22+
*/
23+
@WithDefault("true")
24+
boolean forceDiscovery();
1525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.quarkus.elasticsearch.restclient.lowlevel.runtime;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.elasticsearch.client.RestClient;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.RegisterExtension;
9+
10+
import io.quarkus.arc.Arc;
11+
import io.quarkus.test.QuarkusUnitTest;
12+
import io.smallrye.common.annotation.Identifier;
13+
14+
public class ActiveInactiveClientProgrammaticAccessOnlyTest {
15+
16+
@RegisterExtension
17+
static final QuarkusUnitTest test = new QuarkusUnitTest()
18+
.withApplicationRoot((jar) -> jar
19+
.addAsResource("active-inactive-programmatic.properties", "application.properties"));
20+
21+
@Test
22+
void smoke() {
23+
assertTrue(Arc.container().select(RestClient.class).getHandle().getBean().isActive());
24+
assertFalse(
25+
Arc.container().select(RestClient.class, Identifier.Literal.of("client2")).getHandle().getBean().isActive());
26+
assertTrue(Arc.container().select(RestClient.class, Identifier.Literal.of("client3")).getHandle().getBean().isActive());
27+
}
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
quarkus.elasticsearch.active=true
2+
# We shouldn't need the "discovery flag" for the default client,
3+
# since it is supposed to be always "discovered":
4+
# quarkus.elasticsearch.force-discovery=true
5+
quarkus.elasticsearch."client2".active=false
6+
quarkus.elasticsearch."client2".force-discovery=true
7+
quarkus.elasticsearch."client3".active=true
8+
quarkus.elasticsearch."client3".force-discovery=true

0 commit comments

Comments
 (0)