Hi Team,
I am totally new to temporal
I am building the food delivery workflow using temporal. A typical sequence of workflow looks like this:
Order Food β Restaurant accepts the order β pick up the order β order delivered.
As per my understanding, Signals are operated in multi-threaded env, so we can trigger any signal at any point in time. I need to know how we maintain the sequence in the workflow.
Like before the order has been picked up we can not send the signal of an order being delivered or before accepting the order we can deliver the order.
Please find the below code I have used. Please let me know your thoughts about this.
@WorkflowInterface
public interface WorkFlow {
public static final String QUEUE_NAME = "Customer_Order";
@WorkflowMethod
void startApprovalWorkflow();
@SignalMethod
void signalOrderAccepted();
@SignalMethod
void signalOrderPickedUp();
@SignalMethod
void signalOrderDelivered();
}
public class WorkflowImpl implements WorkFlow {
private final RetryOptions retryoptions = RetryOptions.newBuilder().setInitialInterval(Duration.ofSeconds(1))
.setMaximumInterval(Duration.ofSeconds(100)).setBackoffCoefficient(2).setMaximumAttempts(50000).build();
private final ActivityOptions options = ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(30))
.setRetryOptions(retryoptions).build();
private final Activity activity = Workflow.newActivityStub(Activity.class, options);
public boolean isOrderConfirmed = false;
public boolean isOrderPickedUp = false;
public boolean isOrderDelivered = false;
@Override
public void startApprovalWorkflow() {
activity.placeOrder();
System.out.println("***** Waiting for Restaurant to confirm your order");
Workflow.await(() -> isOrderConfirmed);
System.out.println("***** Please wait till we assign a delivery executive");
Workflow.await(() -> isOrderPickedUp);
System.out.println("***** Delivery executive is on the way");
Workflow.await(() -> isOrderDelivered);
}
@Override
public void signalOrderAccepted() {
activity.setOrderAccepted();
this.isOrderConfirmed = true;
}
@Override
public void signalOrderPickedUp() {
activity.setOrderPickedUp();
this.isOrderPickedUp = true;
}
@Override
public void signalOrderDelivered() {
activity.setOrderDelivered();
this.isOrderDelivered = true;
}
}
@ActivityInterface
public interface Activity {
@ActivityMethod
void placeOrder();
@ActivityMethod
void setOrderAccepted();
@ActivityMethod
void setOrderPickedUp();
@ActivityMethod
void setOrderDelivered();
}
public class ActivityImpl implements Activity {
@Override
public void placeOrder() {
System.out.println("***** Order has been placed");
}
@Override
public void setOrderAccepted() {
System.out.println("***** Restaurant has accepted your order");
}
@Override
public void setOrderPickedUp() {
System.out.println("***** Order has been picked up");
}
@Override
public void setOrderDelivered() {
System.out.println("***** Order Delivered");
}
}
public class OrderService {
@Autowired
WorkflowServiceStubs workflowServiceStubs;
@Autowired
WorkflowClient workflowClient;
public void placeOrder(String workflowId) {
WorkFlow workflow = createWorkFlowConnection(workflowId);
WorkflowClient.start(workflow::startApprovalWorkflow);
}
public void makeOrderAccepted(String workflowId) {
WorkFlow workflow = workflowClient.newWorkflowStub(WorkFlow.class, "Order_" + workflowId);
workflow.signalOrderAccepted();
}
public void makeOrderPickedUp(String workflowId) {
WorkFlow workflow = workflowClient.newWorkflowStub(WorkFlow.class, "Order_" + workflowId);
workflow.signalOrderPickedUp();
}
public void makeOrderDelivered(String workflowId) {
WorkFlow workflow = workflowClient.newWorkflowStub(WorkFlow.class, "Order_" + workflowId);
workflow.signalOrderDelivered();
}
public WorkFlow createWorkFlowConnection(String id) {
WorkflowOptions options = WorkflowOptions.newBuilder().setTaskQueue(WorkFlow.QUEUE_NAME)
.setWorkflowId("Order_" + id).build();
return workflowClient.newWorkflowStub(WorkFlow.class, options);
}
}