-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
LambdaSubscriber.java
194 lines (175 loc) · 5.95 KB
/
LambdaSubscriber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 2016-2021 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.core.publisher;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Consumer;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.Disposable;
import reactor.core.Exceptions;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
/**
* An unbounded Java Lambda adapter to {@link Subscriber}
*
* @param <T> the value type
*/
final class LambdaSubscriber<T>
implements InnerConsumer<T>, Disposable {
final Consumer<? super T> consumer;
final Consumer<? super Throwable> errorConsumer;
final Runnable completeConsumer;
final Consumer<? super Subscription> subscriptionConsumer;
final Context initialContext;
volatile Subscription subscription;
static final AtomicReferenceFieldUpdater<LambdaSubscriber, Subscription> S =
AtomicReferenceFieldUpdater.newUpdater(LambdaSubscriber.class,
Subscription.class,
"subscription");
/**
* Create a {@link Subscriber} reacting onNext, onError and onComplete. If no
* {@code subscriptionConsumer} is provided, the subscriber will automatically request
* Long.MAX_VALUE in onSubscribe, as well as an initial {@link Context} that will be
* visible by operators upstream in the chain.
*
* @param consumer A {@link Consumer} with argument onNext data
* @param errorConsumer A {@link Consumer} called onError
* @param completeConsumer A {@link Runnable} called onComplete with the actual
* context if any
* @param subscriptionConsumer A {@link Consumer} called with the {@link Subscription}
* to perform initial request, or null to request max
* @param initialContext A {@link Context} for this subscriber, or null to use the default
* of an {@link Context#empty() empty Context}.
*/
LambdaSubscriber(
@Nullable Consumer<? super T> consumer,
@Nullable Consumer<? super Throwable> errorConsumer,
@Nullable Runnable completeConsumer,
@Nullable Consumer<? super Subscription> subscriptionConsumer,
@Nullable Context initialContext) {
this.consumer = consumer;
this.errorConsumer = errorConsumer;
this.completeConsumer = completeConsumer;
this.subscriptionConsumer = subscriptionConsumer;
this.initialContext = initialContext == null ? Context.empty() : initialContext;
}
/**
* Create a {@link Subscriber} reacting onNext, onError and onComplete. If no
* {@code subscriptionConsumer} is provided, the subscriber will automatically request
* Long.MAX_VALUE in onSubscribe, as well as an initial {@link Context} that will be
* visible by operators upstream in the chain.
*
* @param consumer A {@link Consumer} with argument onNext data
* @param errorConsumer A {@link Consumer} called onError
* @param completeConsumer A {@link Runnable} called onComplete with the actual
* context if any
* @param subscriptionConsumer A {@link Consumer} called with the {@link Subscription}
* to perform initial request, or null to request max
*/ //left mainly for the benefit of tests
LambdaSubscriber(
@Nullable Consumer<? super T> consumer,
@Nullable Consumer<? super Throwable> errorConsumer,
@Nullable Runnable completeConsumer,
@Nullable Consumer<? super Subscription> subscriptionConsumer) {
this(consumer, errorConsumer, completeConsumer, subscriptionConsumer, null);
}
@Override
public Context currentContext() {
return this.initialContext;
}
@Override
public final void onSubscribe(Subscription s) {
if (Operators.validate(subscription, s)) {
this.subscription = s;
if (subscriptionConsumer != null) {
try {
subscriptionConsumer.accept(s);
}
catch (Throwable t) {
Exceptions.throwIfFatal(t);
s.cancel();
onError(t);
}
}
else {
s.request(Long.MAX_VALUE);
}
}
}
@Override
public final void onComplete() {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s == Operators.cancelledSubscription()) {
return;
}
if (completeConsumer != null) {
try {
completeConsumer.run();
}
catch (Throwable t) {
Exceptions.throwIfFatal(t);
onError(t);
}
}
}
@Override
public final void onError(Throwable t) {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s == Operators.cancelledSubscription()) {
Operators.onErrorDropped(t, this.initialContext);
return;
}
if (errorConsumer != null) {
errorConsumer.accept(t);
}
else {
Operators.onErrorDropped(Exceptions.errorCallbackNotImplemented(t), this.initialContext);
}
}
@Override
public final void onNext(T x) {
try {
if (consumer != null) {
consumer.accept(x);
}
}
catch (Throwable t) {
Exceptions.throwIfFatal(t);
this.subscription.cancel();
onError(t);
}
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) return subscription;
if (key == Attr.PREFETCH) return Integer.MAX_VALUE;
if (key == Attr.TERMINATED || key == Attr.CANCELLED) return isDisposed();
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
return null;
}
@Override
public boolean isDisposed() {
return subscription == Operators.cancelledSubscription();
}
@Override
public void dispose() {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s != null && s != Operators.cancelledSubscription()) {
s.cancel();
}
}
}