package examples;
import examples.SomeDatabaseService;
import io.vertx.core.Vertx;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.ReplyException;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import io.vertx.serviceproxy.ProxyHelper;
import io.vertx.serviceproxy.ProxyHandler;
import io.vertx.serviceproxy.ServiceException;
import io.vertx.serviceproxy.ServiceExceptionMessageCodec;
import io.vertx.serviceproxy.HelperUtils;
import examples.SomeDatabaseService;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
@SuppressWarnings({"unchecked", "rawtypes"})
public class SomeDatabaseServiceVertxProxyHandler extends ProxyHandler {
public static final long DEFAULT_CONNECTION_TIMEOUT = 5 * 60;
private final Vertx vertx;
private final SomeDatabaseService service;
private final long timerID;
private long lastAccessed;
private final long timeoutSeconds;
public SomeDatabaseServiceVertxProxyHandler(Vertx vertx, SomeDatabaseService service){
this(vertx, service, DEFAULT_CONNECTION_TIMEOUT);
}
public SomeDatabaseServiceVertxProxyHandler(Vertx vertx, SomeDatabaseService service, long timeoutInSecond){
this(vertx, service, true, timeoutInSecond);
}
public SomeDatabaseServiceVertxProxyHandler(Vertx vertx, SomeDatabaseService service, boolean topLevel, long timeoutSeconds) {
this.vertx = vertx;
this.service = service;
this.timeoutSeconds = timeoutSeconds;
try {
this.vertx.eventBus().registerDefaultCodec(ServiceException.class,
new ServiceExceptionMessageCodec());
} catch (IllegalStateException ex) {}
if (timeoutSeconds != -1 && !topLevel) {
long period = timeoutSeconds * 1000 / 2;
if (period > 10000) {
period = 10000;
}
this.timerID = vertx.setPeriodic(period, this::checkTimedOut);
} else {
this.timerID = -1;
}
accessed();
}
private void checkTimedOut(long id) {
long now = System.nanoTime();
if (now - lastAccessed > timeoutSeconds * 1000000000) {
close();
}
}
@Override
public void close() {
if (timerID != -1) {
vertx.cancelTimer(timerID);
}
super.close();
}
private void accessed() {
this.lastAccessed = System.nanoTime();
}
public void handle(Message<JsonObject> msg) {
try{
JsonObject json = msg.body();
String action = msg.headers().get("action");
if (action == null) throw new IllegalStateException("action not specified");
accessed();
switch (action) {
case "save": {
service.save((java.lang.String)json.getValue("collection"),
(io.vertx.core.json.JsonObject)json.getValue("document"),
HelperUtils.createHandler(msg));
break;
}
case "foo": {
service.foo((java.lang.String)json.getValue("collection"),
(io.vertx.core.json.JsonObject)json.getValue("document"),
HelperUtils.createHandler(msg));
break;
}
default: throw new IllegalStateException("Invalid action: " + action);
}
} catch (Throwable t) {
msg.reply(new ServiceException(500, t.getMessage()));
throw t;
}
}
}