Skip to content

Instantly share code, notes, and snippets.

@goofyahead
Last active October 23, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goofyahead/6e98944e922084413840b0ac6d492123 to your computer and use it in GitHub Desktop.
Save goofyahead/6e98944e922084413840b0ac6d492123 to your computer and use it in GitHub Desktop.
Mono usage for coordinator spring boot sample
public class OrderHandlers {
private OrderService orderService;
private KitchenService kitchenService;
private DeliveryService deliveryService;
private AccountingService accountingService;
public OrderHandlers(OrderService orderService,
KitchenService kitchenService,
DeliveryService deliveryService,
AccountingService accountingService) {
this.orderService = orderService;
this.kitchenService = kitchenService;
this.deliveryService = deliveryService;
this.accountingService = accountingService;
}
public Mono<ServerResponse> getOrderDetails(ServerRequest serverRequest) {
String orderId = serverRequest.pathVariable("orderId");
Mono<OrderInfo> orderInfo = orderService.findOrderById(orderId);
Mono<Optional<TicketInfo>> ticketInfo =
kitchenService
.findTicketByOrderId(orderId)
.map(Optional::of)
.onErrorReturn(Optional.empty());
Mono<Optional<DeliveryInfo>> deliveryInfo =
deliveryService
.findDeliveryByOrderId(orderId)
.map(Optional::of)
.onErrorReturn(Optional.empty());
Mono<Optional<BillInfo>> billInfo = accountingService
.findBillByOrderId(orderId)
.map(Optional::of)
.onErrorReturn(Optional.empty());
Mono<Tuple4<OrderInfo, Optional<TicketInfo>,
Optional<DeliveryInfo>, Optional<BillInfo>>> combined =
Mono.when(orderInfo, ticketInfo, deliveryInfo, billInfo);
Mono<OrderDetails> orderDetails =
combined.map(OrderDetails::makeOrderDetails);
return orderDetails.flatMap(person -> ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(fromObject(person)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment