zoukankan      html  css  js  c++  java
  • 《Pro Spring Boot 2》第六章:WebFlux and Reactive Data with Spring Boot

     

     

     

     

     

     

    package com.apress.reactor.example;
    
    import com.apress.reactor.example.domain.ToDo;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import reactor.core.publisher.Mono;
    import reactor.core.publisher.MonoProcessor;
    import reactor.core.scheduler.Schedulers;
    
    import java.time.Duration;
    
    @Configuration
    public class MonoExample {
    
        static private Logger LOG = LoggerFactory.getLogger(MonoExample.class);
    
        @Bean
        public CommandLineRunner runMonoExample(){
            return args -> {
    
    
                MonoProcessor<ToDo> promise = MonoProcessor.create();
    
                Mono<ToDo> result = promise
                        .doOnSuccess(p -> LOG.info("MONO >> ToDo: {}", p.getDescription()))
                        .doOnTerminate( () -> LOG.info("MONO >> Done"))
                        .doOnError(t -> LOG.error(t.getMessage(), t))
                        .subscribeOn(Schedulers.single());
    
                promise.onNext(new ToDo("Buy my ticket for SpringOne Platform 2018"));
                //promise.onError(new IllegalArgumentException("There is an error processing the ToDo..."));
    
                result.block(Duration.ofMillis(1000));
            };
        }
    }

     

    package com.apress.reactor.example;
    
    import com.apress.reactor.example.domain.ToDo;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import reactor.core.publisher.EmitterProcessor;
    import reactor.core.publisher.Mono;
    import reactor.core.scheduler.Schedulers;
    
    import java.util.List;
    
    @Configuration
    public class FluxExample {
    
    
        static private Logger LOG = LoggerFactory.getLogger(FluxExample.class);
    
        @Bean
        public CommandLineRunner runFluxExample(){
            return args -> {
    
                EmitterProcessor<ToDo> stream = EmitterProcessor.create();
    
                // Log values passing through the Flux and capture the first coming signal
                Mono<List<ToDo>> promise = stream
                        .filter( s -> s.isCompleted())
                        .doOnNext(s -> LOG.info("FLUX >>> ToDo: {}", s.getDescription()))
                        .collectList()
                        .subscribeOn(Schedulers.single());
    
                // Publish a value
                stream.onNext(new ToDo("Read a Book",true));
                stream.onNext(new ToDo("Listen Classical Music",true));
                stream.onNext(new ToDo("Workout in the Mornings"));
                stream.onNext(new ToDo("Organize my room", true));
                stream.onNext(new ToDo("Go to the Car Wash", true));
                stream.onNext(new ToDo("SP1 2018 is coming", true));
    
                stream.onComplete();
    
                promise.block();
    
            };
        }
    }

     

     

     

     

  • 相关阅读:
    vue 项目界面绘制_stylus_iconfont_swiper
    react_结合 redux
    BOM 浏览器对象模型_当前窗口的浏览历史 history 对象
    BOM 浏览器对象模型_Storage 接口
    react_app 项目开发 (9)_数据可视化 ECharts
    react_app 项目开发 (8)_角色管理_用户管理----权限管理 ---- shouldComponentUpdate
    BOM 浏览器对象模型_同源限制
    面试题: 多个 await 处理,有一个失败,就算作失败
    react_app 项目开发 (7)_难点集合
    react_app 项目开发_遇到的坑
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12271837.html
Copyright © 2011-2022 走看看