Skip to content

Commit

Permalink
Redis client example improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 19, 2024
1 parent 8735c2d commit 5906857
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.vertx.examples.redis;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;
import io.vertx.redis.client.Redis;
import io.vertx.redis.client.RedisAPI;
Expand All @@ -12,26 +12,27 @@
/**
* A verticle setting and reading a value in Redis.
*/
public class RedisClientVerticle extends AbstractVerticle {

public class RedisClientVerticle extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{RedisClientVerticle.class.getName()});
}

private RedisAPI redis;

@Override
public void start() {
public Future<?> start() {
// If a config file is set, read the host and port.
String host = Vertx.currentContext().config().getString("host");
String host = config().getString("host");
if (host == null) {
host = "127.0.0.1";
}

// Create the redis client
Redis client = Redis.createClient(vertx, new RedisOptions().addConnectionString(host));
RedisAPI redis = RedisAPI.api(client);
redis = RedisAPI.api(client);

client.connect()
return client.connect()
.compose(conn ->
redis.set(Arrays.asList("key", "value"))
.compose(v -> {
Expand All @@ -40,9 +41,6 @@ public void start() {
}))
.onSuccess(result -> {
System.out.println("Retrieved value: " + result);
})
.onFailure(err -> {
System.out.println("Connection or Operation Failed " + err);
});
}
}

0 comments on commit 5906857

Please sign in to comment.