I need to trigger some activities from a workflow at certain intervals.
I have read the HelloPeriodic.java and I have also see this feature request.
Can someone let me know if below is the correct way to implement it using Activity
? Is there any advantage of implementing it using a Scheduled Child Workflow?
private final List<Promise<Boolean>> promiseList = new ArrayList<>();
private final AtomicBoolean exitRequested = new AtomicBoolean(false);
// Finishes the entire workflow
@SignalMethod
public void exitWorkflow() {
exitRequested.set(true);
}
// Workflow method
@Override
public void initiateRefund(final Message message) {
log.info("Begin processing refunds for pnrId: {}", message.getPnrId());
try {
// Create refund promises for each refund info.
message
.getRefundInfoList()
.forEach(
refundInfo ->
createRefundPromise(
refundInfo,
message.getPnrId()));
promiseList.forEach(Promise::get);
} catch (Exception e) {
log.error("Exception occurred during initiateRefund, pnrId : {}", message.getPnrId(), e);
raise e;
}
log.info("Done processing refunds for pnrId : {}", message.getPnrId());
}
private void createRefundPromise(
final RefundInfo refundInfo,
final String pnrId) {
val refundPromise =
Async.function(
() -> {
val duration =
Duration.between(currentTime(), refundInfo.getRefundDateTime());
val shouldAbort = Workflow.await(duration, exitRequested::get);
if (!shouldAbort) {
refundActivity.processRefund(refundInfo, pnrId);
log.info(
"Completed processing refund, seqId: {}, pnrId: {}",
refundInfo.getSeqId(),
pnrId);
} else {
log.info(
"Exit requested for refund, seqId: {}, pnrId: {}",
refundInfo.getSeqId(),
pnrId);
}
return true;
});
promiseList.add(refundPromise);
}
private OffsetDateTime currentTime() {
return OffsetDateTime.ofInstant(
Instant.ofEpochMilli(Workflow.currentTimeMillis()), ZoneOffset.UTC);
}