Reactor系列subscribe订阅

Posted javaweibiancheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Reactor系列subscribe订阅相关的知识,希望对你有一定的参考价值。

#java# #reactor# #subcribe# #订阅#

视频讲解 :https://www.bilibili.com/video/av79117693/

技术图片

FluxMonoTestCase.java
package com.example.reactor;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.publisher.Flux;

@Slf4j
public class FluxMonoTestCase extends BaseTestCase {
    @Test
    public void subscribe(){
        Flux<String> stringFlux = Flux.just("Hello","World");
        //stringFlux.subscribe(System.out::println);
        //订阅方式一
        stringFlux.subscribe(val ->{
            log.info("val:{}",val);
        },error ->{
            log.error("error:{}",error);
        },() ->{
            log.info("Finished");
        },subscription -> {
            subscription.request(1);
        });
        //订阅方式二
        stringFlux.subscribe(new Subscriber<String>() {
            @Override
            public void onSubscribe(Subscription subscription) {
                subscription.request(Long.MAX_VALUE);
            }
            @Override
            public void onNext(String s) {
                log.info("onNext:{}",s);
            }
            @Override
            public void onError(Throwable throwable) {
            }
            @Override
            public void onComplete() {
                log.info("onComplete");
            }
        });
    }
}
BaseTestCase.java
package com.example.reactor;

import java.util.Arrays;
import java.util.List;

public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

关注公众号,坚持每天3分钟视频学习

技术图片

以上是关于Reactor系列subscribe订阅的主要内容,如果未能解决你的问题,请参考以下文章

设计模式 行为型模式 -- 观察者模式(发布-订阅(Publish/Subscribe)模式)

MQTT从入门到提高系列 | 06MQTT3.1.1之SUBSCRIBE订阅工作流

RabbitMQ系列教程之三:发布/订阅(Publish/Subscribe)

RabbitMQJava系列4-Publish/Subscribe

Reactor的Publisher与Subscriber

reactor3 flux 的使用