0

I have a method which returns a Future like so,

private Future<Void> generateChildSerial(RoutingContext context, Long createJobID)

and after I insert data in to the database I return future like so,

db
      .preparedQuery(sql)
      .executeBatch(batch, res -> {
      if (res.succeeded()) {

        // Process rows
        RowSet<Row> rows = res.result();
        LOG.info("rows.rowCount():"+ rows.rowCount());
        
      } else {
        System.out.println("Batch failed " + res.cause());
      }
      promise.complete();
    });
    
    return promise.future();

Then in my compose method where I am chaining it, I am trying to check the status of the future like so,

createJob(context)
            .compose(jobID -> 
                    {
                        LOG.debug("jobID "+jobID);
                        Future<Void> generateChildSerial = generateChildSerial(context, jobID);
                        LOG.debug("generateChildSerial.succeeded() "+generateChildSerial.succeeded()+" "+generateChildSerial.result());
                        LOG.debug("generateChildSerial.isComplete() "+generateChildSerial.isComplete());
                        return generateChildSerial;
                    });

The database operation succeeds but for some reason I get false for both the methods, console shows like so,

[vert.x-eventloop-thread-1] DEBUG com.job.CreateJobHandler - generateChildSerial.succeeded() false null
2021-12-06 11:42:41.709+0330 [vert.x-eventloop-thread-1] DEBUG com.job.CreateJobHandler - generateChildSerial.isComplete() false
2021-12-06 11:42:41.914+0330 [vert.x-eventloop-thread-1] INFO  com.job.CreateJobHandler - rows.rowCount():1

Any help will be appreciated!! cheers

ZAJ
  • 793
  • 3
  • 23
  • 50

2 Answers2

0

From your logs you can see that first you log your Future status, and only then you have the code inside your asynchronous method being printed. That's because you don't actually wait for it to complete. So Vert.x is correct there, your Future hasn't completed yet.

You can use map instead:

LOG.debug("jobID "+jobID);
Future<Void> generateChildSerial = generateChildSerial(context, jobID);
return generateChildSerial.map { f -> 
   // Do something here
};

It's not so clear from your example what are your returning, since your return type is Void.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
0

As you can see from the logs

2021-12-06 11:42:41.709+0330 [vert.x-eventloop-thread-1] DEBUG com.job.CreateJobHandler - generateChildSerial.isComplete() false

isComplete() result is false.

This means your future is not yet completed and hence, you cannot yet expect to have generateChildSerial.result() (hence null) and likewise generateChildSerial.succeeded() (hence false).

If your aim is to log the success or failure status of the future, you could have something like

...
    LOG.debug("jobID "+jobID);
    Future<Void> generateChildSerial = generateChildSerial(context, jobID);
    generateChildSerial.onComplete(result -> {
      if (result.succeeded()){
        LOG.debug("generateChildSerial.succeeded() "+generateChildSerial.succeeded()+" "+generateChildSerial.result());
      } else {
        // log the error message you want
      }
      LOG.debug("generateChildSerial.isComplete() "+generateChildSerial.isComplete());
    })
    return generateChildSerial;
....