examples.Examples Maven / Gradle / Ivy
package examples;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
import java.util.Arrays;
/**
*
* These are the examples used in the documentation.
*
* @author Paulo Lopes
*/
public class Examples {
public void example1(Vertx vertx) {
RedisOptions config = new RedisOptions()
.setHost("127.0.0.1");
RedisClient redis = RedisClient.create(vertx, config);
}
public void example2(Vertx vertx) {
RedisClient redis = RedisClient.create(vertx, new RedisOptions());
redis.get("mykey", res -> {
if (res.succeeded()) {
// so something...
}
});
}
public void example3(Vertx vertx) {
// register a handler for the incoming message the naming the Redis module will use is base address + '.' + redis channel
vertx.eventBus().consumer("redis.sub.channel1", received -> {
// do whatever you need to do with your message
JsonObject value = received.body().getJsonObject("value");
// the value is a JSON doc with the following properties
// channel - The channel to which this message was sent
// pattern - Pattern is present if you use psubscribe command and is the pattern that matched this message channel
// message - The message payload
});
RedisClient redis = RedisClient.create(vertx, new RedisOptions());
redis.subscribe("channel1", res -> {
if (res.succeeded()) {
// so something...
}
});
}
public void example4(Vertx vertx) {
RedisClient redis = RedisClient.create(vertx, new RedisOptions());
redis.publish("channel1", "Hello World!", res -> {
if (res.succeeded()) {
// so something...
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy