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
public SQLConnection queryWithParams(String sql, | |
JsonArray params, | |
Handler<AsyncResult<ResultSet>> resultHandler) { | |
new JDBCQuery(vertx, | |
helper, | |
options, | |
ctx, | |
sql, | |
params).execute(conn, | |
statementsQueue, |
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
conn.updateWithParams("insert into user (email, name, password) values (?, ?, ?)", params, | |
(r) -> { | |
if (r.succeeded()) { | |
System.out.println("Ok!"); | |
} | |
else { | |
if (r.cause() instanceof MySQLException) { | |
MySQLException cause = (MySQLException) r.cause(); | |
if (cause.errorMessage().errorCode() == 1062) { | |
// Duplicate key, ignore |
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
public class WebSocks { | |
public static void main(String[] args) { | |
Vertx vertx = Vertx.vertx(); | |
vertx.deployVerticle(new WebSocksServerVerticle(), (__) -> { | |
vertx.deployVerticle(new WebSocksClientVerticle()); | |
}); | |
} | |
} |
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
private void restart(HttpClient client, int delay) { | |
client.close(); | |
vertx.setTimer(TimeUnit.SECONDS.toMillis(delay), (__) -> { | |
startClient(vertx); | |
}); | |
} |
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
client.websocket(8080, "localhost", "/", (ctx) -> { | |
ctx.textMessageHandler((msg) -> { | |
System.out.println("Client " + msg); | |
ctx.writeTextMessage("pong"); | |
}).exceptionHandler((e) -> { | |
System.out.println("Closed, restarting in 10 seconds"); | |
restart(client, 5); | |
}).closeHandler((__) -> { | |
System.out.println("Closed, restarting in 10 seconds"); | |
restart(client, 10); |
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
class WebSocksClientVerticle extends AbstractVerticle { | |
@Override | |
public void start() { | |
startClient(vertx); | |
} | |
private void startClient(Vertx vertx) { | |
HttpClient client = vertx.createHttpClient(); | |
client.websocket(8080, "localhost", "/", (ctx) -> { |
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
server.websocketHandler((ctx) -> { | |
ctx.writeTextMessage("ping"); | |
ctx.textMessageHandler((msg) -> { | |
System.out.println("Server " + msg); | |
if ((new Random()).nextInt(100) == 0) { | |
ctx.close(); | |
} | |
else { |
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
private void startServer(Vertx vertx) { | |
HttpServer server = vertx.createHttpServer(); | |
server.websocketHandler((ctx) -> { | |
ctx.writeTextMessage("ping"); | |
//... | |
}).listen(8080); | |
} |
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
class WebSocksServerVerticle extends AbstractVerticle { | |
@Override | |
public void start() { | |
startServer(vertx); | |
} | |
private void startServer(Vertx vertx) { | |
//... | |
} | |
} |
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
vertx.eventBus().registerDefaultCodec(BigSerializedObject.class, | |
new BigSerializedObjectCodec()); |