package examples;
import io.vertx.ext.consul.*;
public class Sessions {
public void sessionOpts() {
SessionOptions opts = new SessionOptions()
.setNode("nodeId")
.setBehavior(SessionBehavior.RELEASE);
}
public void create(ConsulClient consulClient, SessionOptions opts) {
consulClient.createSessionWithOptions(opts, res -> {
if (res.succeeded()) {
System.out.println("Session successfully created");
System.out.println("id: " + res.result());
} else {
res.cause().printStackTrace();
}
});
}
public void destroy(ConsulClient consulClient, String sessionId) {
consulClient.destroySession(sessionId, res -> {
if (res.succeeded()) {
System.out.println("Session successfully destroyed");
} else {
res.cause().printStackTrace();
}
});
}
public void nodeSessions(ConsulClient consulClient) {
consulClient.listNodeSessions("nodeId", res -> {
if (res.succeeded()) {
for(Session session: res.result().getList()) {
System.out.println("Session id: " + session.getId());
System.out.println("Session node: " + session.getNode());
System.out.println("Session create index: " + session.getCreateIndex());
}
} else {
res.cause().printStackTrace();
}
});
}
public void blockingQuery(ConsulClient consulClient, long lastIndex) {
BlockingQueryOptions blockingOpts = new BlockingQueryOptions()
.setIndex(lastIndex);
consulClient.listSessionsWithOptions(blockingOpts, res -> {
if (res.succeeded()) {
System.out.println("Found " + res.result().getList().size() + " sessions");
} else {
res.cause().printStackTrace();
}
});
}
}