Guest User

Untitled

a guest
Aug 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import io.vertx.core.Future;
  2. import io.vertx.core.json.JsonArray;
  3. import io.vertx.core.json.JsonObject;
  4. import io.vertx.ext.sql.ResultSet;
  5. import io.vertx.rxjava.core.AbstractVerticle;
  6. import io.vertx.rxjava.core.Vertx;
  7. import io.vertx.rxjava.ext.jdbc.JDBCClient;
  8. import rx.Single;
  9.  
  10. import java.util.List;
  11.  
  12. public class JDBC extends AbstractVerticle {
  13.  
  14. private static final String query1 = "Select 1;";
  15.  
  16. private static JDBCClient createJDBCClient() {
  17. return JDBCClient.createShared(Vertx.currentContext().owner(), new JsonObject()
  18. .put("url", "jdbc:mysql://127.0.0.1/mysql?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&failOverReadOnly=false&maxReconnects=10")
  19. .put("user", "root")
  20. .put("password", "root1234")
  21. .put("driver_class", "com.mysql.cj.jdbc.Driver")
  22. .put("max_pool_size", 5)
  23. );
  24. }
  25.  
  26. @Override
  27. public void start(Future<Void> startFuture) throws Exception {
  28. JDBCClient jdbcClient = createJDBCClient();
  29.  
  30. vertx.setPeriodic(1000, handler -> {
  31. Single<ResultSet> mainSingle = jdbcClient
  32. .rxGetConnection()
  33. .flatMap(sqlConnection ->
  34. sqlConnection.rxQueryWithParams(query1, new JsonArray())
  35. .doAfterTerminate(() -> {
  36. System.out.println("Closing the connection.");
  37. sqlConnection.close();
  38. })
  39. );
  40.  
  41. Single<JsonArray> s1 = mainSingle
  42. .flatMap(resultSet -> resultSet.getNumRows() < 2 ?
  43. Single.error(new Exception("Got Error.")) :
  44. Single.just(resultSet.getOutput()));
  45.  
  46. Single<List<JsonObject>> s2 = mainSingle
  47. .map(ResultSet::getRows);
  48.  
  49.  
  50. Single.zip(s1, s2, (rs1, rs2) -> new JsonObject().put("rs1", rs1).put("rs2", rs2))
  51. .subscribe(System.out::println, Throwable::printStackTrace);
  52. });
  53.  
  54. super.start(startFuture);
  55. }
  56. }
Add Comment
Please, Sign In to add comment