Vert.x JOOQ RxJava Postgres顺序数据库调用

时间:2019-08-25 16:28:27

标签: postgresql kotlin rx-java2 vert.x jooq

我使用带有rx-async选项的Vert.x和JOOQ: https://github.com/jklingsporn/vertx-jooq/tree/master/vertx-jooq-rx-async

我有一个HTTP verticle,它将/ jooqtest路由到用Kotlin编写的jooqRxAsyncHandler:

private fun jooqRxAsyncHandler(context: RoutingContext) {
    val jooqConfig: Configuration = DefaultConfiguration().set(SQLDialect.POSTGRES)
    val dbCredentials = JsonObject()
        .put("host", "localhost")
        .put("username", "username")
        .put("password", "password")
        .put("database", "dbname")

    dbClient = PostgreSQLClient.createNonShared(vertx, dbCredentials)

    val dao = UserDao(jooqConfig, dbClient)
    println("getting. client: " + dbClient)

    dao.deleteByCondition(USER.EMAIL.eq("f@g.com"))
        .concatWith(
            dao.insert(User("f@g.com",
                "f@g.com",
                "accessToken",
                OffsetDateTime.now(),
                "f@g.com"))
        )
        .subscribe()

    dao.findOneByCondition(USER.EMAIL.eq("test@test.com.zz"))
        .doOnEvent { something, x ->
            if (x == null) {
                context.response().putHeader("content-type", "text/html").end("JOOQ test $something")

            } else {
                System.err.println("Something failed badly: " + x.message)
                context.response().putHeader("content-type", "text/html").end("JOOQ error")
            }
        }
        .subscribe()

    println("done")
}

两个问题: 1)这是顺序触发还是并行触发,我如何使所有顺序触发(删除,插入,选择)?

2)调用.subscribe()时,这是否会阻塞主线程?

1 个答案:

答案 0 :(得分:1)

在Clement Escoffier的帮助下进行解答:

1)两个.subscribe()调用并发。与.concatWith()组合的delete和insert调用按顺序触发。

2)这是非阻塞的。

另外,要依次运行delete,insert和findByOne调用,请使用.ignoreElement()和.andThen(),如下所示:

private fun jooqRxAsyncHandler(context: RoutingContext) {
    val jooqConfig: Configuration = DefaultConfiguration().set(SQLDialect.POSTGRES)
    val dbCredentials = JsonObject()
        .put("host", "localhost")
        .put("username", "username")
        .put("password", "password")
        .put("database", "dbname")

    dbClient = PostgreSQLClient.createNonShared(vertx, com.datadeploytool.database.dbCredentials)

    val dao = UserDao(jooqConfig, dbClient)
    println("getting. client: $dbClient")

    dao.deleteByCondition(USER.EMAIL.eq("f@g.com")).ignoreElement()
        .andThen(
            dao.insert(User("f@g.com",
                "f@g.com",
                "accessToken",
                OffsetDateTime.now(),
                "f@g.com"))
        ).ignoreElement()
        .andThen(
            dao.findOneByCondition(USER.EMAIL.eq("test@test.com.zz"))
        )
        .doOnEvent { something, x ->
            if (x == null) {
                context.response().putHeader("content-type", "text/html").end("JOOQ test $something")

            } else {
                System.err.println("Something failed badly: " + x.message)
                context.response().putHeader("content-type", "text/html").end("JOOQ error")
            }
        }
        .subscribe()

    println("done")
}