I have the following problem:
I have a service that starts a workflow. I would like the service to wait for an intermediate result of the workflow before continuing.
To solve this, i do the following:
- Start an AwaitWorkflow that waits for a signal:
@WorkflowInterface
public interface AwaitOrderWorkflow {
@WorkflowMethod
OrderResponse executeAwaitOrderResponse();
@SignalMethod
void orderResponseReceived(OrderResponse orderResponse);
}
public class AwaitOrderWorkflowImpl implements AwaitOrderWorkflow {
private Optional<OrderResponse> orderResponse = Optional.empty();
@Override
public CreateOrderResponse executeAwaitOrderResponse() {
Workflow.await(Duration.ofSeconds(30), () -> orderResponse.isPresent());
return orderResponse.orElseThrow();
}
@Override
public void orderResponseReceived(OrderResponse orderResponse) {
this.orderResponse = Optional.of(orderResponse);
}
}
- Start the main workflow
- Once the main workflow is at a certain intermediate state, it uses an activity to send a signal to the associated AwaitOrderWorkflow:
@Override
public void signalOrderCreated(OrderResponse orderResponse, UUID orderReference) {
var workFlowId = String.format("AwaitOrderWorkflow-%s", orderReference);
var workflow = workflowClient.newWorkflowStub(AwaitOrderResponseWorkflow.class, workFlowId);
workflow.orderResponseReceived(orderResponse);
}
- The external service does a .get on the completable future response executeAwaitOrderResponse() method:
var orderResponse = orderResponseCompletableFuture.get(30, TimeUnit.SECONDS);
Question:
Is this a right way to do this, or is there something in place to perform this in an easier way?