• reactive reactor


    package geektime.spring.springbucks;
    
    import geektime.spring.springbucks.model.Coffee;
    import geektime.spring.springbucks.model.CoffeeOrder;
    import geektime.spring.springbucks.model.OrderState;
    import geektime.spring.springbucks.service.CoffeeService;
    import geektime.spring.springbucks.service.OrderService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    import java.util.Arrays;
    import java.util.Date;
    
    @Component
    @Slf4j
    public class SpringbucksRunner implements ApplicationRunner {
        @Autowired
        private CoffeeService coffeeService;
        @Autowired
        private OrderService orderService;
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            coffeeService.initCache()
                    .then(
                            coffeeService.findOneCoffee("mocha")
                                    .flatMap(c -> {
                                        CoffeeOrder order = createOrder("Li Lei", c);
                                        return orderService.create(order);
                                    })
                                    .doOnError(t -> log.error("error", t)))
                    .subscribe(o -> log.info("Create Order: {}", o));
            log.info("After Subscribe");
            Thread.sleep(5000);
        }
    
        private CoffeeOrder createOrder(String customer, Coffee... coffee) {
            return CoffeeOrder.builder()
                    .customer(customer)
                    .items(Arrays.asList(coffee))
                    .state(OrderState.INIT)
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
        }
    }
    
    

    就像昨天的网站说的一样
    返回的数据流调用flatMap 使用lambda接收Function
    doOnError为发生error异常会怎么办
    subscribe(Function)参数二值,参数一lambda

  • 相关阅读:
    297. Serialize and Deserialize Binary Tree
    331. Verify Preorder Serialization of a Binary Tree
    332. Reconstruct Itinerary
    329. Longest Increasing Path in a Matrix
    319. Bulb Switcher
    292. Nim Game
    299. Bulls and Cows
    Ice Cream Tower Gym
    B
    C
  • 原文地址:https://www.cnblogs.com/ukzq/p/13461480.html
Copyright © 2020-2023  润新知