Vertx -将参数从一个组合传递到另一个组合



我需要一些帮助来传递参数从一个组成到另一个。我想将第二次撰写中的labelparmeters传递到最后一次撰写中,如下面的代码所示。

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());
});
}

您可以创建一个context对象,该对象具有在Futures中传递的数据的setter/getter。Compose保证了期货的串行执行,您可以在下一个Compose部分中设置结果并假设结果存在于上下文对象中。例子:

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!!"));
}

你也可以使用java记录。

或者,您可以按照如下方式嵌套组合部分:

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()));

}

最新更新