SpringBoot2.0不容错过的新特性 WebFlux响应式编程【第4章】reactive stream 响应式流

4-1 初识Reactive Stream

概念

JDK9引入的一套标准,是一套基于发布订阅者模式数据处理的规范。FLOW API。

背压

backpress发布者和订阅者之间的互动,调节数据流量的作用,

4-2 Reactive Stream主要接口

java.util.concurrent.Flow

IDEA 查看类概述[Alt +7]
在这里插入图片描述

java.util.concurrent.Flow.Publisher

发布者;

  • subscribe:发布者和调用者产生订阅关系。
java.util.concurrent.Flow.Subscriber

订阅者

  • onSubscribe:和发布者签约订阅关系的时候。
java.util.concurrent.Flow.Subscription

发布者和订阅者之间通过Publisher.subscribe建立调用关系,这个关系就是Subscription,它是实现背压的关键。

  • request方法告诉发布者还需要订阅者还需要多少资源。
java.util.concurrent.Flow.Processor

继承了发布者和订阅者接口。既可以做发布者,又可以做消费者。更多的时候是承担中间角色,发布者发布消息到接收者,如果中间要进行一些处理、过滤。 相当于过滤器,中转站

4-3 完整实例

1.创建发布者;使用JDK自带的SubmissionPublisher,它实现了Publisher接口
SubmissionPublisher<String> publisher = new SubmissionPublisher();
2.创建订阅者
Subscriber<String> subscriber = new Subscriber<>() {

          int num = 1;
          private Subscription subscription;

          @Override
          public void onSubscribe(Subscription subscription) {

              System.out.println("Subscribe!");
              //保持订阅者关系,需要用它来给发布者响应
              this.subscription = subscription;
              //请求一条数据
              this.subscription.request(1);

          }

          @Override
          public void onNext(String item) {

              System.out.println("The request times is : " + num);
              num++;
              //接收到一条消息,处理
              System.out.println("The message received is :" + item);

              //处理完调用request再请求一条数据
              this.subscription.request(1);

              //或者已经达到目标,告诉发布者不需要再发送数据了
              //this.subscription.cancel();
          }

          @Override
          public void onError(Throwable throwable) {
              //异常处理(当程序出现异常的时候),一般是在onNext方法处理出错的时候这里会捕获

              //告诉发布者,后面不收收数据了
              this.subscription.cancel();

          }

          @Override
          public void onComplete() {
              //publisher.close()时会执行该方法
              //当消息处理完成时
              System.out.println("Complete!");

          }
      };
3.发布者和订阅者建立关系
 publisher.subscribe(subscriber);
4.生产数据并发布
publisher.submit("The sunset is beautiful today.");

publisher.submit("Today, I went to the seaside to pick up rubbish.");

publisher.submit("A lot of people took part in the activity.");
5.结束后关闭发布者;正式环境应该放finally或者使用try-resource确保关闭
publisher.close();

//主线程延迟停止,否则数据没有消费就退出
//TimeUnit.SECONDS.sleep(2);
Thread.currentThread().join(1000);
输出结果
Subscribe!
The request times is : 1
The message received is :The sunset is beautiful today.
The request times is : 2
The message received is :Today, I went to the seaside to pick up rubbish.
The request times is : 3
The message received is :A lot of people took part in the activity.
Complete!

使用processor,需要继承SubmissionPublisher,实现Process接口

输入数据源String,处理掉不符合规则的数据,然后发布出去

自定义Processor
class MyProcessor extends SubmissionPublisher<String> implements Processor<String,String> {

    private Subscription subscription;

    @Override
    public void onSubscribe(Subscription subscription) {

        System.out.println("Processor subscribe");

        this.subscription =subscription;
        this.subscription.request(1);

    }

    @Override
    public void onNext(String item) {

        System.out.println("Processor onNext");

        //Conditional filtering
        if("A".equals(item) || "C".equals(item) || "E".equals(item)){

            this.submit("Data to be published " + item);
        }

        this.subscription.request(1);

    }

    @Override
    public void onError(Throwable throwable) {

        System.out.println("Processor onError.");

    }

    @Override
    public void onComplete() {

        System.out.println("Processor onComplete.");

    }

}
1.创建发布者;使用JDK自带的SubmissionPublisher,它实现了Publisher接口
SubmissionPublisher<String> publisher = new SubmissionPublisher<>();
2.定义处理器,对数据进行过滤
MyProcessor myProcessor = new MyProcessor();
3.发布者和订阅者建立关系
publisher.subscribe(myProcessor);
4.定义最终订阅者,消费String类型数据
Subscriber<String> subscriber = new Subscriber<>() {

    int num = 1;
    private Subscription subscription;

    @Override
    public void onSubscribe(Subscription subscription) {
        System.out.println(" Subscribe!");
        //保持订阅者关系,需要用它来给发布者响应
        this.subscription = subscription;
        //请求一条数据
        this.subscription.request(1);
    }

    @Override
    public void onNext(String item) {
        System.out.println("The request times is : " + num);
        num++;
        //接收到一条消息,处理
        System.out.println("The message received is :" + item);

        //处理完调用request再请求一条数据
        this.subscription.request(1);

        //或者已经达到目标,告诉发布者不需要再发送数据了
        //this.subscription.cancel();

    }

    @Override
    public void onError(Throwable throwable) {

        System.out.println("Error");
        System.out.println(throwable.getMessage());

        //异常处理(当程序出现异常的时候),一般是在onNext方法处理出错的时候这里会捕获

        //告诉发布者,后面不收收数据了
        this.subscription.cancel();

    }

    @Override
    public void onComplete() {
        //publisher.close()时会执行该方法
        //当消息处理完成时
        System.out.println("Complete!");

    }
};
5.处理器和最终订阅者建立订阅关系
myProcessor.subscribe(subscriber);
6.生产数据并发布
publisher.submit("A");
publisher.submit("B");
publisher.submit("C");
publisher.submit("D");
publisher.submit("E");
publisher.submit("F");
7.结束后关闭发布者;正式环境应该放finally或者使用try-resource确保关闭
publisher.close();

//主线程延迟停止,否则数据没有消费就退出
//TimeUnit.SECONDS.sleep(2);
Thread.currentThread().join(10000);
输出结果
Processor subscribe
 Subscribe!
Processor onNext
Processor onNext
Processor onNext
Processor onNext
Processor onNext
Processor onNext
Processor onComplete.
The request times is : 1
The message received is :Data to be published A
The request times is : 2
The message received is :Data to be published C
The request times is : 3
The message received is :Data to be published E

4-4 运行机制

在Subscriber.onSubscribe方法处打断点执行

在这里插入图片描述

说明:由于未知原因,在执行Subscriber.onSubscribe()方法时subscription调式部分的array提示All elements are null.为了达到预期效果在this.subscription = subscription;前增加以下代码:

try {
    TimeUnit.SECONDS.sleep(1);

} catch (InterruptedException e) {
    e.printStackTrace();
}
array:订阅者缓冲,它会把发布者的数据先缓冲起来。
响应式流里面最关键的就是反馈,可以通过反馈来调节发布者,创建产生数据的速度。如何让发布者慢下来的?
publisher.submit()是阻塞方法。发布者发布数据的时候,订阅者的缓冲池满了,那submit()就会被阻塞,这个时候就会被停下来,就不会再生产数据了。
发布者发布259条数据
4.生产数据并发布
for (int i = 0; i < 259; i++) {

    System.out.println("publisher data is " + i);
    publisher.submit("The sunset is beautiful today." + i);

}
订阅者缓冲池最大的容量为256

在这里插入图片描述

输出结果
publisher data is 0
publisher data is 1
publisher data is 2
publisher data is 3
publisher data is 4
publisher data is 5
publisher data is 6
publisher data is 7
publisher data is 8
publisher data is 9
publisher data is 10
publisher data is 11
publisher data is 12
publisher data is 13
publisher data is 14
publisher data is 15
publisher data is 16
publisher data is 17
publisher data is 18
publisher data is 19
publisher data is 20
publisher data is 21
publisher data is 22
publisher data is 23
publisher data is 24
publisher data is 25
publisher data is 26
publisher data is 27
publisher data is 28
publisher data is 29
publisher data is 30
publisher data is 31
publisher data is 32
publisher data is 33
publisher data is 34
publisher data is 35
publisher data is 36
publisher data is 37
publisher data is 38
publisher data is 39
publisher data is 40
publisher data is 41
publisher data is 42
publisher data is 43
publisher data is 44
publisher data is 45
publisher data is 46
publisher data is 47
publisher data is 48
publisher data is 49
publisher data is 50
publisher data is 51
publisher data is 52
publisher data is 53
publisher data is 54
publisher data is 55
publisher data is 56
publisher data is 57
publisher data is 58
publisher data is 59
publisher data is 60
publisher data is 61
publisher data is 62
publisher data is 63
publisher data is 64
publisher data is 65
publisher data is 66
publisher data is 67
publisher data is 68
publisher data is 69
publisher data is 70
publisher data is 71
publisher data is 72
publisher data is 73
publisher data is 74
publisher data is 75
publisher data is 76
publisher data is 77
publisher data is 78
publisher data is 79
publisher data is 80
publisher data is 81
publisher data is 82
publisher data is 83
publisher data is 84
publisher data is 85
publisher data is 86
publisher data is 87
publisher data is 88
publisher data is 89
publisher data is 90
publisher data is 91
publisher data is 92
publisher data is 93
publisher data is 94
publisher data is 95
publisher data is 96
publisher data is 97
publisher data is 98
publisher data is 99
publisher data is 100
publisher data is 101
publisher data is 102
publisher data is 103
publisher data is 104
publisher data is 105
publisher data is 106
publisher data is 107
publisher data is 108
publisher data is 109
publisher data is 110
publisher data is 111
publisher data is 112
publisher data is 113
publisher data is 114
publisher data is 115
publisher data is 116
publisher data is 117
publisher data is 118
publisher data is 119
publisher data is 120
publisher data is 121
publisher data is 122
publisher data is 123
publisher data is 124
publisher data is 125
publisher data is 126
publisher data is 127
publisher data is 128
publisher data is 129
publisher data is 130
publisher data is 131
publisher data is 132
publisher data is 133
publisher data is 134
publisher data is 135
publisher data is 136
publisher data is 137
publisher data is 138
publisher data is 139
publisher data is 140
publisher data is 141
publisher data is 142
publisher data is 143
publisher data is 144
publisher data is 145
publisher data is 146
publisher data is 147
publisher data is 148
publisher data is 149
publisher data is 150
publisher data is 151
publisher data is 152
publisher data is 153
publisher data is 154
publisher data is 155
publisher data is 156
publisher data is 157
publisher data is 158
publisher data is 159
publisher data is 160
publisher data is 161
publisher data is 162
publisher data is 163
publisher data is 164
publisher data is 165
publisher data is 166
publisher data is 167
publisher data is 168
publisher data is 169
publisher data is 170
publisher data is 171
publisher data is 172
publisher data is 173
publisher data is 174
publisher data is 175
publisher data is 176
publisher data is 177
publisher data is 178
publisher data is 179
publisher data is 180
publisher data is 181
publisher data is 182
publisher data is 183
publisher data is 184
publisher data is 185
publisher data is 186
publisher data is 187
publisher data is 188
publisher data is 189
publisher data is 190
publisher data is 191
publisher data is 192
publisher data is 193
publisher data is 194
publisher data is 195
publisher data is 196
publisher data is 197
publisher data is 198
publisher data is 199
publisher data is 200
publisher data is 201
publisher data is 202
publisher data is 203
publisher data is 204
publisher data is 205
publisher data is 206
publisher data is 207
publisher data is 208
publisher data is 209
publisher data is 210
publisher data is 211
publisher data is 212
publisher data is 213
publisher data is 214
publisher data is 215
publisher data is 216
publisher data is 217
publisher data is 218
publisher data is 219
publisher data is 220
publisher data is 221
publisher data is 222
publisher data is 223
publisher data is 224
publisher data is 225
publisher data is 226
publisher data is 227
publisher data is 228
publisher data is 229
publisher data is 230
publisher data is 231
publisher data is 232
publisher data is 233
publisher data is 234
publisher data is 235
publisher data is 236
publisher data is 237
publisher data is 238
publisher data is 239
publisher data is 240
publisher data is 241
publisher data is 242
publisher data is 243
publisher data is 244
publisher data is 245
publisher data is 246
publisher data is 247
publisher data is 248
publisher data is 249
publisher data is 250
publisher data is 251
publisher data is 252
publisher data is 253
publisher data is 254
publisher data is 255
publisher data is 256
Subscribe!
publisher data is 257
The message received is :The sunset is beautiful today.0
The message received is :The sunset is beautiful today.1
publisher data is 258
The message received is :The sunset is beautiful today.2
The message received is :The sunset is beautiful today.3
The message received is :The sunset is beautiful today.4
The message received is :The sunset is beautiful today.5
The message received is :The sunset is beautiful today.6
The message received is :The sunset is beautiful today.7
The message received is :The sunset is beautiful today.8
The message received is :The sunset is beautiful today.9
The message received is :The sunset is beautiful today.10
The message received is :The sunset is beautiful today.11
The message received is :The sunset is beautiful today.12
The message received is :The sunset is beautiful today.13
The message received is :The sunset is beautiful today.14
The message received is :The sunset is beautiful today.15
The message received is :The sunset is beautiful today.16
The message received is :The sunset is beautiful today.17
The message received is :The sunset is beautiful today.18
The message received is :The sunset is beautiful today.19
The message received is :The sunset is beautiful today.20
The message received is :The sunset is beautiful today.21
The message received is :The sunset is beautiful today.22
The message received is :The sunset is beautiful today.23
The message received is :The sunset is beautiful today.24
The message received is :The sunset is beautiful today.25
The message received is :The sunset is beautiful today.26
The message received is :The sunset is beautiful today.27
The message received is :The sunset is beautiful today.28
The message received is :The sunset is beautiful today.29
The message received is :The sunset is beautiful today.30
The message received is :The sunset is beautiful today.31
The message received is :The sunset is beautiful today.32
The message received is :The sunset is beautiful today.33
The message received is :The sunset is beautiful today.34
The message received is :The sunset is beautiful today.35
The message received is :The sunset is beautiful today.36
The message received is :The sunset is beautiful today.37
The message received is :The sunset is beautiful today.38
The message received is :The sunset is beautiful today.39
The message received is :The sunset is beautiful today.40
The message received is :The sunset is beautiful today.41
The message received is :The sunset is beautiful today.42
The message received is :The sunset is beautiful today.43
The message received is :The sunset is beautiful today.44
The message received is :The sunset is beautiful today.45
The message received is :The sunset is beautiful today.46
The message received is :The sunset is beautiful today.47
The message received is :The sunset is beautiful today.48
The message received is :The sunset is beautiful today.49
The message received is :The sunset is beautiful today.50
The message received is :The sunset is beautiful today.51
The message received is :The sunset is beautiful today.52
The message received is :The sunset is beautiful today.53
The message received is :The sunset is beautiful today.54
The message received is :The sunset is beautiful today.55
The message received is :The sunset is beautiful today.56
The message received is :The sunset is beautiful today.57
The message received is :The sunset is beautiful today.58
The message received is :The sunset is beautiful today.59
The message received is :The sunset is beautiful today.60
The message received is :The sunset is beautiful today.61
The message received is :The sunset is beautiful today.62
The message received is :The sunset is beautiful today.63
The message received is :The sunset is beautiful today.64
The message received is :The sunset is beautiful today.65
The message received is :The sunset is beautiful today.66
The message received is :The sunset is beautiful today.67
The message received is :The sunset is beautiful today.68
The message received is :The sunset is beautiful today.69
The message received is :The sunset is beautiful today.70
The message received is :The sunset is beautiful today.71
The message received is :The sunset is beautiful today.72
The message received is :The sunset is beautiful today.73
The message received is :The sunset is beautiful today.74
The message received is :The sunset is beautiful today.75
The message received is :The sunset is beautiful today.76
The message received is :The sunset is beautiful today.77
The message received is :The sunset is beautiful today.78
The message received is :The sunset is beautiful today.79
The message received is :The sunset is beautiful today.80
The message received is :The sunset is beautiful today.81
The message received is :The sunset is beautiful today.82
The message received is :The sunset is beautiful today.83
The message received is :The sunset is beautiful today.84
The message received is :The sunset is beautiful today.85
The message received is :The sunset is beautiful today.86
The message received is :The sunset is beautiful today.87
The message received is :The sunset is beautiful today.88
The message received is :The sunset is beautiful today.89
The message received is :The sunset is beautiful today.90
The message received is :The sunset is beautiful today.91
The message received is :The sunset is beautiful today.92
The message received is :The sunset is beautiful today.93
The message received is :The sunset is beautiful today.94
The message received is :The sunset is beautiful today.95
The message received is :The sunset is beautiful today.96
The message received is :The sunset is beautiful today.97
The message received is :The sunset is beautiful today.98
The message received is :The sunset is beautiful today.99
The message received is :The sunset is beautiful today.100
The message received is :The sunset is beautiful today.101
The message received is :The sunset is beautiful today.102
The message received is :The sunset is beautiful today.103
The message received is :The sunset is beautiful today.104
The message received is :The sunset is beautiful today.105
The message received is :The sunset is beautiful today.106
The message received is :The sunset is beautiful today.107
The message received is :The sunset is beautiful today.108
The message received is :The sunset is beautiful today.109
The message received is :The sunset is beautiful today.110
The message received is :The sunset is beautiful today.111
The message received is :The sunset is beautiful today.112
The message received is :The sunset is beautiful today.113
The message received is :The sunset is beautiful today.114
The message received is :The sunset is beautiful today.115
The message received is :The sunset is beautiful today.116
The message received is :The sunset is beautiful today.117
The message received is :The sunset is beautiful today.118
The message received is :The sunset is beautiful today.119
The message received is :The sunset is beautiful today.120
The message received is :The sunset is beautiful today.121
The message received is :The sunset is beautiful today.122
The message received is :The sunset is beautiful today.123
The message received is :The sunset is beautiful today.124
The message received is :The sunset is beautiful today.125
The message received is :The sunset is beautiful today.126
The message received is :The sunset is beautiful today.127
The message received is :The sunset is beautiful today.128
The message received is :The sunset is beautiful today.129
The message received is :The sunset is beautiful today.130
The message received is :The sunset is beautiful today.131
The message received is :The sunset is beautiful today.132
The message received is :The sunset is beautiful today.133
The message received is :The sunset is beautiful today.134
The message received is :The sunset is beautiful today.135
The message received is :The sunset is beautiful today.136
The message received is :The sunset is beautiful today.137
The message received is :The sunset is beautiful today.138
The message received is :The sunset is beautiful today.139
The message received is :The sunset is beautiful today.140
The message received is :The sunset is beautiful today.141
The message received is :The sunset is beautiful today.142
The message received is :The sunset is beautiful today.143
The message received is :The sunset is beautiful today.144
The message received is :The sunset is beautiful today.145
The message received is :The sunset is beautiful today.146
The message received is :The sunset is beautiful today.147
The message received is :The sunset is beautiful today.148
The message received is :The sunset is beautiful today.149
The message received is :The sunset is beautiful today.150
The message received is :The sunset is beautiful today.151
The message received is :The sunset is beautiful today.152
The message received is :The sunset is beautiful today.153
The message received is :The sunset is beautiful today.154
The message received is :The sunset is beautiful today.155
The message received is :The sunset is beautiful today.156
The message received is :The sunset is beautiful today.157
The message received is :The sunset is beautiful today.158
The message received is :The sunset is beautiful today.159
The message received is :The sunset is beautiful today.160
The message received is :The sunset is beautiful today.161
The message received is :The sunset is beautiful today.162
The message received is :The sunset is beautiful today.163
The message received is :The sunset is beautiful today.164
The message received is :The sunset is beautiful today.165
The message received is :The sunset is beautiful today.166
The message received is :The sunset is beautiful today.167
The message received is :The sunset is beautiful today.168
The message received is :The sunset is beautiful today.169
The message received is :The sunset is beautiful today.170
The message received is :The sunset is beautiful today.171
The message received is :The sunset is beautiful today.172
The message received is :The sunset is beautiful today.173
The message received is :The sunset is beautiful today.174
The message received is :The sunset is beautiful today.175
The message received is :The sunset is beautiful today.176
The message received is :The sunset is beautiful today.177
The message received is :The sunset is beautiful today.178
The message received is :The sunset is beautiful today.179
The message received is :The sunset is beautiful today.180
The message received is :The sunset is beautiful today.181
The message received is :The sunset is beautiful today.182
The message received is :The sunset is beautiful today.183
The message received is :The sunset is beautiful today.184
The message received is :The sunset is beautiful today.185
The message received is :The sunset is beautiful today.186
The message received is :The sunset is beautiful today.187
The message received is :The sunset is beautiful today.188
The message received is :The sunset is beautiful today.189
The message received is :The sunset is beautiful today.190
The message received is :The sunset is beautiful today.191
The message received is :The sunset is beautiful today.192
The message received is :The sunset is beautiful today.193
The message received is :The sunset is beautiful today.194
The message received is :The sunset is beautiful today.195
The message received is :The sunset is beautiful today.196
The message received is :The sunset is beautiful today.197
The message received is :The sunset is beautiful today.198
The message received is :The sunset is beautiful today.199
The message received is :The sunset is beautiful today.200
The message received is :The sunset is beautiful today.201
The message received is :The sunset is beautiful today.202
The message received is :The sunset is beautiful today.203
The message received is :The sunset is beautiful today.204
The message received is :The sunset is beautiful today.205
The message received is :The sunset is beautiful today.206
The message received is :The sunset is beautiful today.207
The message received is :The sunset is beautiful today.208
The message received is :The sunset is beautiful today.209
The message received is :The sunset is beautiful today.210
The message received is :The sunset is beautiful today.211
The message received is :The sunset is beautiful today.212
The message received is :The sunset is beautiful today.213
The message received is :The sunset is beautiful today.214
The message received is :The sunset is beautiful today.215
The message received is :The sunset is beautiful today.216
The message received is :The sunset is beautiful today.217
The message received is :The sunset is beautiful today.218
The message received is :The sunset is beautiful today.219
The message received is :The sunset is beautiful today.220
The message received is :The sunset is beautiful today.221
The message received is :The sunset is beautiful today.222
The message received is :The sunset is beautiful today.223
The message received is :The sunset is beautiful today.224
The message received is :The sunset is beautiful today.225
The message received is :The sunset is beautiful today.226
The message received is :The sunset is beautiful today.227
The message received is :The sunset is beautiful today.228
The message received is :The sunset is beautiful today.229
The message received is :The sunset is beautiful today.230
The message received is :The sunset is beautiful today.231
The message received is :The sunset is beautiful today.232
The message received is :The sunset is beautiful today.233
The message received is :The sunset is beautiful today.234
The message received is :The sunset is beautiful today.235
The message received is :The sunset is beautiful today.236
The message received is :The sunset is beautiful today.237
The message received is :The sunset is beautiful today.238
The message received is :The sunset is beautiful today.239
The message received is :The sunset is beautiful today.240
The message received is :The sunset is beautiful today.241
The message received is :The sunset is beautiful today.242
The message received is :The sunset is beautiful today.243
The message received is :The sunset is beautiful today.244
The message received is :The sunset is beautiful today.245
The message received is :The sunset is beautiful today.246
The message received is :The sunset is beautiful today.247
The message received is :The sunset is beautiful today.248
The message received is :The sunset is beautiful today.249
The message received is :The sunset is beautiful today.250
The message received is :The sunset is beautiful today.251
The message received is :The sunset is beautiful today.252
The message received is :The sunset is beautiful today.253
The message received is :The sunset is beautiful today.254
The message received is :The sunset is beautiful today.255
The message received is :The sunset is beautiful today.256
The message received is :The sunset is beautiful today.257
The message received is :The sunset is beautiful today.258
Complete!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值