Last active
October 23, 2018 06:49
-
-
Save goofyahead/6e98944e922084413840b0ac6d492123 to your computer and use it in GitHub Desktop.
Mono usage for coordinator spring boot sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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