vert.xvertx-httpclient

Vertx - passing parameter from one compose to another


I need some help to pass parameter from one compose to another. I want to pass the labelParemeters in the second compose into the last compose, as I have shown in the code below.

public Future<JsonArray> startTest(int jobID, RoutingContext context) {
    LOG.info("-----INside startTest() method-----");
    return jobDao.getJob(jobID, context)
            .compose(job -> productDao.getLabelParameters(job, context))
            .compose(labelParameters -> jobDao.getJobParentChildForPrint(jobID, context, true, labelParameters))
            .compose(parentChildSerials -> {
                    LOG.debug(" Future Returned Job Parent-Child to Print: ");
                    prepareSerialForPrint(parentChildSerials, labelParameters); //Pass Here 
                    return Future.succeededFuture(parentChildSerials);
            })
        .onFailure(error -> {
                LOG.debug("startTest() Failed: ", error);
              })
        .onSuccess(server -> {
                LOG.debug("Finished startTest!!");
                LOG.debug(server.encodePrettily());
              });
}

Solution

  • You can create a context object the has setters/getter for the data that is passed around in the Futures. Compose guareantees serial execution of your Futures, you can set the results and assume that the result is present in your context object in the next compose section. Example:

    public Future<JsonArray> startTest(int jobID, RoutingContext context) {
        MyTestContext myTestCtx = new MyTestConext();
    
        return jobDao.getJob(jobID, context)
                .compose(job -> {
                    myTestCtx.setjob(job);
                    return productDao.getLabelParameters(job, context);
                })
                .compose(labelParameters -> {
                    myTestCtx.setLabelParameters(labelparameters);
                    return jobDao.getJobParentChildForPrint(jobID, context, true, labelParameters);
                })
                .compose(parentChildSerials -> {
                    var labelParameters = myTestCtx.getLabelParameters();
                    prepareSerialForPrint(parentChildSerials, labelParameters); //Pass Here 
                    return Future.succeededFuture(parentChildSerials);
                })
            .onSuccess(server -> LOG.debug("Finished startTest!!"));
    
    }
    

    You can also make use of the java records for this.

    Alternatively you can nest the compose parts as follows:

        public Future<JsonArray> startTest(int jobID, RoutingContext context) {
            return jobDao.getJob(jobID, context)
                    .compose(job -> productDao.getLabelParameters(job, context))
                    .compose(labelParameters -> {
                        return jobDao.getJobParentChildForPrint(jobID, context, true, labelParameters)
                                .compose(parentChildSerials -> {
                                    LOG.debug(" Future Returned Job Parent-Child to Print: ");
                                    prepareSerialForPrint(parentChildSerials, labelParameters); //Pass Here 
                                    return Future.succeededFuture(parentChildSerials);
                                })
                    })    
                .onFailure(error -> LOG.debug("startTest() Failed: ", error))
                .onSuccess(server -> LOG.debug(server.encodePrettily()));
        
        }