Skip to content

Commit

Permalink
Configure redis client beans from config if a programmatic injection …
Browse files Browse the repository at this point in the history
…point exists.
  • Loading branch information
ozangunalp committed Nov 17, 2022
1 parent 13f3dce commit e9ec185
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.quarkus.redis.client.RedisOptionsCustomizer;
import io.quarkus.redis.client.reactive.ReactiveRedisClient;
import io.quarkus.redis.runtime.client.RedisClientRecorder;
import io.quarkus.redis.runtime.client.config.RedisConfig;
import io.quarkus.smallrye.health.deployment.spi.HealthBuildItem;
import io.quarkus.vertx.deployment.VertxBuildItem;
import io.vertx.redis.client.impl.types.BulkType;
Expand Down Expand Up @@ -97,6 +98,8 @@ UnremovableBeanBuildItem makeHostsProviderAndOptionsCustomizerUnremovable() {
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
public void init(RedisClientRecorder recorder,
RedisBuildTimeConfig buildTimeConfig,
RedisConfig redisConfig,
BeanArchiveIndexBuildItem indexBuildItem,
BeanDiscoveryFinishedBuildItem beans,
ShutdownContextBuildItem shutdown,
Expand All @@ -117,6 +120,12 @@ public void init(RedisClientRecorder recorder,
.findAny()
.ifPresent(x -> names.add(DEFAULT_CLIENT_NAME));

beans.getInjectionPoints().stream()
.filter(i -> SUPPORTED_INJECTION_TYPE.contains(i.getRequiredType().name()))
.filter(InjectionPointInfo::isProgrammaticLookup)
.findAny()
.ifPresent(x -> names.addAll(configuredClientNames(buildTimeConfig, redisConfig)));

// Inject the creation of the client when the application starts.
recorder.initialize(vertxBuildItem.getVertx(), names);

Expand Down Expand Up @@ -144,6 +153,18 @@ public void init(RedisClientRecorder recorder,
recorder.cleanup(shutdown);
}

static Set<String> configuredClientNames(RedisBuildTimeConfig buildTimeConfig, RedisConfig redisConfig) {
Set<String> names = new HashSet<>();
// redis client names from dev services
if (buildTimeConfig.defaultDevService.devservices.enabled) {
names.add(DEFAULT_CLIENT_NAME);
}
names.addAll(buildTimeConfig.additionalDevServices.keySet());
// redis client names declared in config
names.addAll(redisConfig.clientNames());
return names;
}

static <T> SyntheticBeanBuildItem configureAndCreateSyntheticBean(String name,
Class<T> type,
Supplier<T> supplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.quarkus.redis.client.deployment.RedisClientProcessor.REDIS_CLIENT_ANNOTATION;
import static io.quarkus.redis.client.deployment.RedisClientProcessor.configureAndCreateSyntheticBean;
import static io.quarkus.redis.client.deployment.RedisClientProcessor.configuredClientNames;
import static io.quarkus.redis.runtime.client.config.RedisConfig.DEFAULT_CLIENT_NAME;

import java.util.Collection;
Expand All @@ -25,6 +26,7 @@
import io.quarkus.redis.datasource.ReactiveRedisDataSource;
import io.quarkus.redis.datasource.RedisDataSource;
import io.quarkus.redis.runtime.client.RedisClientRecorder;
import io.quarkus.redis.runtime.client.config.RedisConfig;
import io.quarkus.vertx.deployment.VertxBuildItem;

public class RedisDatasourceProcessor {
Expand All @@ -36,6 +38,8 @@ public class RedisDatasourceProcessor {
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
public void init(RedisClientRecorder recorder,
RedisBuildTimeConfig buildTimeConfig,
RedisConfig redisConfig,
BeanArchiveIndexBuildItem indexBuildItem,
BeanDiscoveryFinishedBuildItem beans,
ShutdownContextBuildItem shutdown,
Expand All @@ -56,6 +60,12 @@ public void init(RedisClientRecorder recorder,
.findAny()
.ifPresent(x -> names.add(DEFAULT_CLIENT_NAME));

beans.getInjectionPoints().stream()
.filter(i -> SUPPORTED_INJECTION_TYPE.contains(i.getRequiredType().name()))
.filter(InjectionPointInfo::isProgrammaticLookup)
.findAny()
.ifPresent(x -> names.addAll(configuredClientNames(buildTimeConfig, redisConfig)));

// Inject the creation of the client when the application starts.
recorder.initialize(vertxBuildItem.getVertx(), names);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class RedisNoConfTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class))
.overrideConfigKey("quarkus.redis.devservices.enabled", "false");

@Inject
@Any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public RedisClientRecorder(RedisConfig rc) {

public void initialize(RuntimeValue<io.vertx.core.Vertx> vertx, Set<String> names) {
this.vertx = Vertx.newInstance(vertx.getValue());
names.addAll(config.clientNames());
_initialize(this.vertx, names);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.quarkus.redis.runtime.client.config;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(phase = ConfigPhase.RUN_TIME, name = RedisConfig.REDIS_CONFIG_ROOT_NAME)
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED, name = RedisConfig.REDIS_CONFIG_ROOT_NAME)
public class RedisConfig {
public final static String REDIS_CONFIG_ROOT_NAME = "redis";
public final static String HOSTS_CONFIG_NAME = "hosts";
Expand Down Expand Up @@ -53,4 +55,17 @@ public static boolean isDefaultClient(String name) {
return DEFAULT_CLIENT_NAME.equalsIgnoreCase(name);
}

public Set<String> clientNames() {
Set<String> names = new HashSet<>();
if (this.namedRedisClients != null) {
names.addAll(this.namedRedisClients.keySet());
}
if (this.defaultRedisClient != null) {
if (this.defaultRedisClient.hosts.isPresent()) {
names.add(DEFAULT_CLIENT_NAME);
}
}
return names;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

Expand Down Expand Up @@ -65,7 +66,7 @@ public CheckpointStateStore create(KafkaConnectorIncomingConfiguration config, V
.orElse(null);
ReactiveRedisDataSource rds = clientName != null
? redisDataSource.select(RedisClientName.Literal.of(clientName)).get()
: redisDataSource.get();
: redisDataSource.select(Default.Literal.INSTANCE).get();
ProcessingStateCodec stateCodec = CDIUtils.getInstanceById(stateCodecFactory, config.getChannel(), () -> {
if (stateCodecFactory.isUnsatisfied()) {
return VertxJsonProcessingStateCodec.FACTORY;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.redis.it;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import io.quarkus.redis.client.RedisClientName;
import io.quarkus.redis.datasource.ReactiveRedisDataSource;
import io.quarkus.redis.datasource.RedisDataSource;
import io.quarkus.redis.datasource.value.ReactiveValueCommands;
import io.quarkus.redis.datasource.value.ValueCommands;
import io.smallrye.mutiny.Uni;

@Path("/quarkus-redis-with-instance")
@ApplicationScoped
public class RedisWithInstanceInjectionResource {

private final ValueCommands<String, String> blocking;
private final ReactiveValueCommands<String, String> reactive;

public RedisWithInstanceInjectionResource(@Any Instance<RedisDataSource> ds,
@Any Instance<ReactiveRedisDataSource> reactiveDs) {
blocking = ds.select(RedisClientName.Literal.of("instance-client")).get().value(String.class);
reactive = reactiveDs.select(RedisClientName.Literal.of("instance-client")).get().value(String.class);
}

// synchronous
@GET
@Path("/sync/{key}")
public String getSync(@PathParam("key") String key) {
return blocking.get(key);
}

@POST
@Path("/sync/{key}")
public void setSync(@PathParam("key") String key, String value) {
blocking.set(key, value);
}

// reactive
@GET
@Path("/reactive/{key}")
public Uni<String> getReactive(@PathParam("key") String key) {
return reactive.get(key);
}

@POST
@Path("/reactive/{key}")
public Uni<Void> setReactive(@PathParam("key") String key, String value) {
return this.reactive.set(key, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ quarkus.redis.named-reactive-client.hosts=redis://localhost:6379/1
quarkus.redis.named-client-legacy.hosts=redis://localhost:6379/4
quarkus.redis.named-reactive-client-legacy.hosts=redis://localhost:6379/4
quarkus.redis.parameter-injection.hosts=redis://localhost:6379/2
quarkus.redis.instance-client.hosts=redis://localhost:6379/5
# use DB 3
quarkus.redis.provided-hosts.hosts-provider-name=test-hosts-provider
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class QuarkusRedisTest {
"/quarkus-redis",
"/quarkus-redis-with-name",
"/quarkus-redis-with-name-legacy",
"/quarkus-redis-with-instance",
"/quarkus-redis-provided-hosts",
"/quarkus-redis-parameter-injection-legacy"
};
Expand Down

0 comments on commit e9ec185

Please sign in to comment.