0

I got a pretty small Verticle that should connect to a database and regularly poll a table and send the objects to the event bus. Thus far I can connect to the database, but the handler afterwards gets not executed and my polling timer does not start. I guess it's something obvious and appreciate every help.

I use Vert.x 3.8 Promises as Futures are deprecated (just like in the example behind that link). As you can see in my code, using deprecated Futures works just fine! But who want's to use deprecated code, heh? Either I'm doing something wrong or that's a bug in Vert.x, what I don't assume.

My Vert.x is at 3.8.1

class JdbcVerticle extends AbstractVerticle {

    private SQLConnection connection

    @Override
    void start(Promise<Void> startPromise) {
        def jdbcParams = config().getJsonObject('connection')
        // This gets executed:
        testFuture().handler = { println "Test Future handler runs!" }

        // This is never executed :-(
        connect(jdbcParams).handler = { println "Connected..." }
    }

    Future<Void> connect(JsonObject jdbcParams) {
        def promise = Promise.<Void>promise()
        def client = JDBCClient.createShared(vertx, jdbcParams)

        client.getConnection() { connection ->
            if(connection.failed()) {
                promise.fail(connection.cause())
            } else {
                this.connection = connection.result()
                promise.complete()
            }
        }

        return promise.future()
    }

    Future<Void> testFuture() {
        def future = Future.<Void>future()
        vertx.setTimer(200) { future.complete() }
        return future
    }
}
Jazzschmidt
  • 989
  • 12
  • 27
  • why your `connect()` returns a Future instead of Promise? – injecteer Oct 01 '19 at 16:22
  • Are you sure it's connecting? Is the handler for the future called at all? Is it called with `failed()` set to return true? – tim_yates Oct 01 '19 at 18:20
  • @injecteer because Future is the readers side of the Promise [as of Vert.x 3.8](https://github.com/vert-x3/wiki/wiki/3.8.0-Deprecations-and-breaking-changes#future-creation-and-completion) – Jazzschmidt Oct 02 '19 at 06:52
  • @tim_yates pretty sure, since a simple `println` statement in the `client.getConnection()` is executed. Either way, `failed()` or `complete()`, the handler should be called. – Jazzschmidt Oct 02 '19 at 06:54
  • I agree the handler should be called, but if it's `failed()`, the return will prevent you reaching your println marked by `// This is never executed :-(` – tim_yates Oct 02 '19 at 07:34
  • @tim_yates you are right and I stripped down my code to show, that the problem is but before. – Jazzschmidt Oct 02 '19 at 08:10

0 Answers0