From af6543615f6fa7bb613e274c8598f205e3d13b2f Mon Sep 17 00:00:00 2001 From: Giselle van Dongen Date: Fri, 12 Jan 2024 10:59:58 +0100 Subject: [PATCH] Address feedback --- .../sdk/examples/OrderStatusService.java | 3 +- .../restate/sdk/examples/OrderWorkflow.java | 76 +++++++++---------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderStatusService.java b/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderStatusService.java index da951490..0b190949 100644 --- a/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderStatusService.java +++ b/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderStatusService.java @@ -11,7 +11,8 @@ public class OrderStatusService extends OrderStatusServiceRestate.OrderStatusServiceRestateImplBase { - private static final StateKey ORDER_STATUS = StateKey.of("order-status", CoreSerdes.STRING_UTF8); + private static final StateKey ORDER_STATUS = + StateKey.of("order-status", CoreSerdes.STRING_UTF8); private static final StateKey ORDER_ETA = StateKey.of("order-eta", CoreSerdes.LONG); /** Gets called by the webUI frontend to display the status of an order. */ diff --git a/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderWorkflow.java b/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderWorkflow.java index 00c2c695..26f3e7f3 100644 --- a/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderWorkflow.java +++ b/java/food-ordering/app/restate-app/src/main/java/dev/restate/sdk/examples/OrderWorkflow.java @@ -32,52 +32,52 @@ public void handleOrderCreationEvent(RestateContext ctx, KafkaOrderEvent event) throws TerminalException { var orderStatusSend = OrderStatusServiceRestate.newClient(ctx); + ObjectMapper mapper = new ObjectMapper(); + OrderRequest order; try { - ObjectMapper mapper = new ObjectMapper(); - OrderRequest order = mapper.readValue(event.getPayload().toStringUtf8(), OrderRequest.class); - String id = order.getOrderId(); - - // 1. Set status - orderStatusSend.oneWay().setStatus(statusToProto(id, StatusEnum.CREATED)); + order = mapper.readValue(event.getPayload().toStringUtf8(), OrderRequest.class); + } catch (JsonProcessingException e) { + throw new TerminalException("Parsing raw JSON order failed: " + e.getMessage()); + } + String id = order.getOrderId(); - // 2. Handle payment - String token = ctx.sideEffect(CoreSerdes.STRING_UTF8, () -> UUID.randomUUID().toString()); - boolean paid = - ctx.sideEffect( - CoreSerdes.BOOLEAN, () -> paymentClnt.charge(id, token, order.getTotalCost())); + // 1. Set status + orderStatusSend.oneWay().setStatus(statusToProto(id, StatusEnum.CREATED)); - if (!paid) { - orderStatusSend.oneWay().setStatus(statusToProto(id, StatusEnum.REJECTED)); - return; - } + // 2. Handle payment + String token = ctx.sideEffect(CoreSerdes.STRING_UTF8, () -> UUID.randomUUID().toString()); + boolean paid = + ctx.sideEffect( + CoreSerdes.BOOLEAN, () -> paymentClnt.charge(id, token, order.getTotalCost())); - // 3. Schedule preparation - orderStatusSend.setStatus(statusToProto(order.getOrderId(), StatusEnum.SCHEDULED)); - ctx.sleep(Duration.ofMillis(order.getDeliveryDelay())); + if (!paid) { + orderStatusSend.oneWay().setStatus(statusToProto(id, StatusEnum.REJECTED)); + return; + } - // 4. Trigger preparation - var preparationAwakeable = ctx.awakeable(CoreSerdes.VOID); - ctx.sideEffect(() -> restaurant.prepare(id, preparationAwakeable.id())); - orderStatusSend.setStatus(statusToProto(id, StatusEnum.IN_PREPARATION)); + // 3. Schedule preparation + orderStatusSend.setStatus(statusToProto(order.getOrderId(), StatusEnum.SCHEDULED)); + ctx.sleep(Duration.ofMillis(order.getDeliveryDelay())); - preparationAwakeable.await(); - orderStatusSend.setStatus(statusToProto(id, StatusEnum.SCHEDULING_DELIVERY)); + // 4. Trigger preparation + var preparationAwakeable = ctx.awakeable(CoreSerdes.VOID); + ctx.sideEffect(() -> restaurant.prepare(id, preparationAwakeable.id())); + orderStatusSend.setStatus(statusToProto(id, StatusEnum.IN_PREPARATION)); - // 5. Find a driver and start delivery - var deliveryAwakeable = ctx.awakeable(CoreSerdes.VOID); + preparationAwakeable.await(); + orderStatusSend.setStatus(statusToProto(id, StatusEnum.SCHEDULING_DELIVERY)); - var deliveryRequest = - DeliveryRequest.newBuilder() - .setOrderId(id) - .setRestaurantId(order.getRestaurantId()) - .setCallback(deliveryAwakeable.id()) - .build(); - DeliveryManagerRestate.newClient(ctx).oneWay().start(deliveryRequest); - deliveryAwakeable.await(); - orderStatusSend.setStatus(statusToProto(order.getOrderId(), StatusEnum.DELIVERED)); + // 5. Find a driver and start delivery + var deliveryAwakeable = ctx.awakeable(CoreSerdes.VOID); - } catch (JsonProcessingException e) { - throw new TerminalException("Parsing raw JSON order failed: " + e.getMessage()); - } + var deliveryRequest = + DeliveryRequest.newBuilder() + .setOrderId(id) + .setRestaurantId(order.getRestaurantId()) + .setCallback(deliveryAwakeable.id()) + .build(); + DeliveryManagerRestate.newClient(ctx).oneWay().start(deliveryRequest); + deliveryAwakeable.await(); + orderStatusSend.setStatus(statusToProto(order.getOrderId(), StatusEnum.DELIVERED)); } }