reactor.core.publisher.Mono.get()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(4.6k)|赞(0)|评价(0)|浏览(319)

本文整理了Java中reactor.core.publisher.Mono.get()方法的一些代码示例,展示了Mono.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mono.get()方法的具体详情如下:
包路径:reactor.core.publisher.Mono
类名称:Mono
方法名:get

Mono.get介绍

暂无

代码示例

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

/**
 * Incorporates a new {@link CartEvent} and updated the shopping cart
 *
 * @param cartEvent is the {@link CartEvent} that will alter the state of the cart
 * @return the state of the {@link ShoppingCart} after applying the new {@link CartEvent}
 */
public ShoppingCart incorporate(CartEvent cartEvent) {
  // Remember that thing about safety properties in microservices?
  Flux<CartEventType> validCartEventTypes =
      Flux.fromStream(Stream.of(CartEventType.ADD_ITEM,
          CartEventType.REMOVE_ITEM));
  // The CartEvent's type must be either ADD_ITEM or REMOVE_ITEM
  if (validCartEventTypes.exists(cartEventType ->
      cartEvent.getCartEventType().equals(cartEventType)).get()) {
    // Update the aggregate view of each line item's quantity from the event type
    productMap.put(cartEvent.getProductId(),
        productMap.getOrDefault(cartEvent.getProductId(), 0) +
            (cartEvent.getQuantity() * (cartEvent.getCartEventType()
                .equals(CartEventType.ADD_ITEM) ? 1 : -1)));
  }
  // Return the updated state of the aggregate to the reactive stream's reduce method
  return this;
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

public Order getOrder(String orderId, Boolean validate) {
  // Get the order for the event
  Order order = orderRepository.findOne(orderId);
  if (validate) {
    try {
      // Validate the account number of the event's order belongs to the user
      validateAccountNumber(order.getAccountNumber());
    } catch (Exception ex) {
      return null;
    }
  }
  Flux<OrderEvent> orderEvents =
      Flux.fromStream(orderEventRepository.findOrderEventsByOrderId(order.getOrderId()));
  // Aggregate the state of order
  return orderEvents
      .takeWhile(orderEvent -> orderEvent.getType() != OrderEventType.DELIVERED)
      .reduceWith(() -> order, Order::incorporate)
      .get();
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

/**
 * Aggregate the cart events of a user and return a {@link ShoppingCart} object
 *
 * @param user    is the user to retrieve the shopping cart for
 * @param catalog is the catalog used to generate the shopping cart
 * @return a shopping cart representing the aggregate state of the user's cart
 * @throws Exception
 */
public ShoppingCart aggregateCartEvents(User user, Catalog catalog) throws Exception {
  Flux<CartEvent> cartEvents =
      Flux.fromStream(cartEventRepository.getCartEventStreamByUser(user.getId()));
  // Aggregate the state of the shopping cart
  ShoppingCart shoppingCart = cartEvents
      .takeWhile(cartEvent -> !ShoppingCart.isTerminal(cartEvent.getCartEventType()))
      .reduceWith(() -> new ShoppingCart(catalog), ShoppingCart::incorporate)
      .get();
  shoppingCart.getLineItems();
  return shoppingCart;
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

/**
 * Incorporates a new {@link CartEvent} and update the shopping cart
 * @param cartEvent 是将要改变购物车状态的{@link CartEvent}
 * @return 在应用了新的 {@link CartEvent} 后返回 {@link ShoppingCart} 的状态
 */
public ShoppingCart incorporate(CartEvent cartEvent) {
  Flux<CartEventType> validCartEventTypes =
      Flux.fromStream(Stream.of(CartEventType.ADD_ITEM, CartEventType.REMOVE_ITEM));
  // CartEvent类型必须是 ADD_ITEM or REMOVE_ITEM
  if(validCartEventTypes.exists(cartEventType ->
      cartEvent.getCartEventType().equals(cartEventType)).get()) {
    // 根据事件类型更新购物车每个产品的数量的聚合
    productMap.put(cartEvent.getProductId(),
        productMap.getOrDefault(cartEvent.getProductId(), 0) +
            (cartEvent.getQuantity() * (cartEvent.getCartEventType()
                .equals(CartEventType.ADD_ITEM) ? 1 : -1))
    );
  }
  // Return the updated state of the aggregate to the reactive stream's reduce method
  return this;
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

public Order getOrder(String orderId, Boolean validate) {
  // 获取订单
  Order order = orderRepositroy.findOne(orderId);
  if(validate) {
    // 验证事件对应的订单的账户号(account number)属于用户
    try {
      validateAccountNumber(order.getAccountNumber());
    } catch (Exception e) {
      return null;
    }
  }
  Flux<OrderEvent> orderEvents = Flux.fromStream(orderEventRepository.findOrderEventsByOrderId(orderId));
  // 聚合订单状态
  return orderEvents.takeWhile(orderEvent -> orderEvent.getType() != OrderEventType.DELIVERED)
      .reduceWith(() -> order, Order::incorporate)
      .get();
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

/**
 * 聚合(Aggregate)一个用户的cart events,返回一个 {@link ShoppingCart}
 * @param user 获取购物车的用户
 * @param catalog 用于生成购物车的目录
 * @return 一个表示用户购物车聚合状态的购物车
 * @throws Exception 如果在购物车中的一个产品不在目录里面,则抛出异常
 */
public ShoppingCart aggregateCartEvents(User user, Catalog catalog) throws Exception {
  Flux<CartEvent> cartEvents = Flux.fromStream(cartEventRepository.getCartEventStreamByUserId(user.getId()));
  // 聚合购物车的状态
  ShoppingCart shoppingCart = cartEvents.takeWhile(cartEvent -> !ShoppingCart.isTerminal(cartEvent.getCartEventType()))
      .reduceWith(() -> new ShoppingCart(catalog), ShoppingCart::incorporate)
      .get();
  shoppingCart.getCartItems();
  return shoppingCart;
}

相关文章

Mono类方法