forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redia): provide Redis hosts programmatically
This allows for configuration of properties like redis connection password coming from other sources. Closes quarkusio#16284
- Loading branch information
Showing
9 changed files
with
223 additions
and
8 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
18 changes: 18 additions & 0 deletions
18
...nsions/redis-client/runtime/src/main/java/io/quarkus/redis/client/RedisHostsProvider.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,18 @@ | ||
package io.quarkus.redis.client; | ||
|
||
import java.net.URI; | ||
import java.util.Set; | ||
|
||
/** | ||
* Programmatically provides redis hosts | ||
*/ | ||
public interface RedisHostsProvider { | ||
/** | ||
* Returns the hosts for this provider. | ||
* <p> | ||
* The host provided uses the following schema `redis://[:password@][host][:port][/database]` | ||
* | ||
* @return the hosts | ||
*/ | ||
Set<URI> getHosts(); | ||
} |
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
19 changes: 19 additions & 0 deletions
19
integration-tests/redis-client/src/main/java/io/quarkus/redis/it/RedisLocalHostProvider.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,19 @@ | ||
package io.quarkus.redis.it; | ||
|
||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Named; | ||
|
||
import io.quarkus.redis.client.RedisHostsProvider; | ||
|
||
@ApplicationScoped | ||
@Named("test-hosts-provider") | ||
public class RedisLocalHostProvider implements RedisHostsProvider { | ||
@Override | ||
public Set<URI> getHosts() { | ||
return Collections.singleton(URI.create("redis://localhost:6379/3")); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...-tests/redis-client/src/main/java/io/quarkus/redis/it/RedisWithProvidedHostsResource.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,62 @@ | ||
package io.quarkus.redis.it; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
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.RedisClient; | ||
import io.quarkus.redis.client.RedisClientName; | ||
import io.quarkus.redis.client.reactive.ReactiveRedisClient; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.redis.client.Response; | ||
|
||
@Path("/quarkus-redis-provided-hosts") | ||
@ApplicationScoped | ||
public class RedisWithProvidedHostsResource { | ||
private RedisClient redisClient; | ||
private ReactiveRedisClient reactiveRedisClient; | ||
|
||
@Inject | ||
public RedisWithProvidedHostsResource(@RedisClientName("provided-hosts") RedisClient redisClient, | ||
@RedisClientName("provided-hosts") ReactiveRedisClient reactiveRedisClient) { | ||
this.redisClient = redisClient; | ||
this.reactiveRedisClient = reactiveRedisClient; | ||
} | ||
|
||
// synchronous | ||
@GET | ||
@Path("/sync/{key}") | ||
public String getSync(@PathParam("key") String key) { | ||
Response response = redisClient.get(key); | ||
return response == null ? null : response.toString(); | ||
} | ||
|
||
@POST | ||
@Path("/sync/{key}") | ||
public void setSync(@PathParam("key") String key, String value) { | ||
this.redisClient.set(Arrays.asList(key, value)); | ||
} | ||
|
||
// reactive | ||
@GET | ||
@Path("/reactive/{key}") | ||
public Uni<String> getReactive(@PathParam("key") String key) { | ||
return reactiveRedisClient | ||
.get(key) | ||
.map(response -> response == null ? null : response.toString()); | ||
} | ||
|
||
@POST | ||
@Path("/reactive/{key}") | ||
public Uni<Void> setReactive(@PathParam("key") String key, String value) { | ||
return this.reactiveRedisClient | ||
.set(Arrays.asList(key, value)) | ||
.map(response -> null); | ||
} | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
...tests/redis-client/src/test/java/io/quarkus/redis/it/QuarkusRedisWithProvidedHostsIT.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,8 @@ | ||
package io.quarkus.redis.it; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
class QuarkusRedisWithProvidedHostsIT extends QuarkusRedisWithProvidedHostsTest { | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...sts/redis-client/src/test/java/io/quarkus/redis/it/QuarkusRedisWithProvidedHostsTest.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,62 @@ | ||
package io.quarkus.redis.it; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTest | ||
class QuarkusRedisWithProvidedHostsTest { | ||
static final String SYNC_KEY = "named-sync-key"; | ||
static final String SYNC_VALUE = "named-sync-value"; | ||
|
||
static final String REACTIVE_KEY = "named-reactive-key"; | ||
static final String REACTIVE_VALUE = "named-reactive-value"; | ||
|
||
@Test | ||
public void sync() { | ||
RestAssured.given() | ||
.when() | ||
.get("/quarkus-redis-provided-hosts/sync/" + SYNC_KEY) | ||
.then() | ||
.statusCode(204); // the key is not set yet | ||
|
||
RestAssured.given() | ||
.body(SYNC_VALUE) | ||
.when() | ||
.post("/quarkus-redis-provided-hosts/sync/" + SYNC_KEY) | ||
.then() | ||
.statusCode(204); | ||
|
||
RestAssured.given() | ||
.when() | ||
.get("/quarkus-redis-provided-hosts/sync/" + SYNC_KEY) | ||
.then() | ||
.statusCode(200) | ||
.body(CoreMatchers.is(SYNC_VALUE)); | ||
} | ||
|
||
@Test | ||
public void reactive() { | ||
RestAssured.given() | ||
.when() | ||
.get("/quarkus-redis-provided-hosts/reactive/" + REACTIVE_KEY) | ||
.then() | ||
.statusCode(204); // the reactive key is not set yet | ||
|
||
RestAssured.given() | ||
.body(REACTIVE_VALUE) | ||
.when() | ||
.post("/quarkus-redis-provided-hosts/reactive/" + REACTIVE_KEY) | ||
.then() | ||
.statusCode(204); | ||
|
||
RestAssured.given() | ||
.when() | ||
.get("/quarkus-redis-provided-hosts/reactive/" + REACTIVE_KEY) | ||
.then() | ||
.statusCode(200) | ||
.body(CoreMatchers.is(REACTIVE_VALUE)); | ||
} | ||
} |