use of org.reactivestreams.Subscriber in project ratpack by ratpack.
the class ByteBufComposingPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {
private Subscription subscription;
private CompositeByteBuf composite;
private volatile State state;
@Override
protected void onRequest(long n) {
if (subscription == null) {
upstream.subscribe(new Subscriber<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
subscription = s;
state = State.Fetching;
s.request(1);
}
@Override
public void onNext(ByteBuf t) {
if (state == State.Closed) {
t.release();
return;
}
if (composite == null) {
composite = alloc.compositeBuffer(maxNum);
}
composite.addComponent(true, t);
if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
state = State.Writing;
emitNext(composite);
composite = null;
maybeFetch();
} else {
subscription.request(1);
}
}
@Override
public void onError(Throwable t) {
state = State.Closed;
ReferenceCountUtil.release(composite);
emitError(t);
}
@Override
public void onComplete() {
state = State.Closed;
if (composite != null) {
emitNext(composite);
}
emitComplete();
}
});
} else {
maybeFetch();
}
}
private void maybeFetch() {
if (getDemand() > 0 && state != State.Fetching) {
state = State.Fetching;
subscription.request(1);
}
}
@Override
protected void onCancel() {
state = State.Closed;
ReferenceCountUtil.release(composite);
if (subscription != null) {
subscription.cancel();
}
}
});
}
use of org.reactivestreams.Subscriber in project reactor-core by reactor.
the class EmitterProcessorTest method test.
@Test
@Ignore
public void test() {
Scheduler asyncGroup = Schedulers.single();
FluxProcessor<String, String> emitter = EmitterProcessor.create();
CountDownLatch requestReceived = new CountDownLatch(1);
AtomicLong demand = new AtomicLong(0);
Publisher<String> publisher = s -> s.onSubscribe(new Subscription() {
@Override
public void request(long n) {
System.out.println("request: " + n + " " + s);
demand.addAndGet(n);
requestReceived.countDown();
}
@Override
public void cancel() {
System.out.println("cancel");
}
});
Flux.from(publisher).subscribeOn(asyncGroup).subscribe(emitter);
AssertSubscriber<String> subscriber = AssertSubscriber.create();
emitter.subscribe(subscriber);
int i = 0;
for (; ; ) {
if (getAndSub(demand, 1) != 0) {
emitter.onNext("" + (i++));
} else {
System.out.println("NO REQUESTED: " + emitter);
LockSupport.parkNanos(100_000_000);
}
}
}
use of org.reactivestreams.Subscriber in project reactor-core by reactor.
the class EmitterProcessorTest method testPerformance.
@Test
@Ignore
public void testPerformance() {
FluxProcessor<String, String> emitter = EmitterProcessor.create();
CountDownLatch requestReceived = new CountDownLatch(1);
AtomicLong maxDelay = new AtomicLong(0);
AtomicLong demand = new AtomicLong(0);
Publisher<String> publisher = new Publisher<String>() {
long lastTimeRequestReceivedNs = -1;
@Override
public void subscribe(Subscriber<? super String> s) {
s.onSubscribe(new Subscription() {
@Override
public void request(long n) {
requestReceived.countDown();
long now = System.nanoTime();
if (lastTimeRequestReceivedNs > 0) {
maxDelay.set(now - lastTimeRequestReceivedNs);
}
lastTimeRequestReceivedNs = now;
demand.addAndGet(n);
}
@Override
public void cancel() {
System.out.println("cancel");
}
});
}
};
publisher.subscribe(emitter);
AssertSubscriber<String> subscriber = AssertSubscriber.create();
emitter.subscribe(subscriber);
String buffer = "Hello";
int i = 0;
for (; ; ) {
if (getAndSub(demand, 1) > 0) {
emitter.onNext(buffer);
}
if (i++ % 1000000 == 0) {
System.out.println("maxDelay: " + TimeUnit.MICROSECONDS.toMillis(maxDelay.get()) + " µs");
}
}
}
use of org.reactivestreams.Subscriber in project reactor-core by reactor.
the class BaseOperatorTest method fluxFuseableAsyncState.
@SuppressWarnings("unchecked")
final Flux<O> fluxFuseableAsyncState(OperatorScenario<I, PI, O, PO> scenario, boolean conditional) {
AtomicReference<Scannable> ref = new AtomicReference<>();
Flux<I> source = this.fluxFuseableAsync(scenario).doOnSubscribe(s -> Scannable.from(s).actuals().skip(1).findFirst().ifPresent(t -> {
ref.set(t);
if (scenario.prefetch() != -1) {
assertThat(t.scan(Scannable.Attr.PREFETCH)).isEqualTo(scenario.prefetch());
}
}));
if (source.getPrefetch() != -1 && scenario.prefetch() != -1) {
assertThat(Math.min(source.getPrefetch(), Integer.MAX_VALUE)).isEqualTo(scenario.prefetch());
}
PO f;
f = applyStateScenario(scenario, conditional, source);
return Flux.from(f).doOnSubscribe(parent -> {
Scannable t = Scannable.from(parent);
assertThat(t.scan(Scannable.Attr.ERROR)).isNull();
assertThat(t.scanOrDefault(Scannable.Attr.TERMINATED, false)).isFalse();
// noop path
if (parent instanceof Subscriber) {
((Subscriber<I>) parent).onSubscribe(Operators.emptySubscription());
((Subscriber<I>) parent).onSubscribe(Operators.cancelledSubscription());
}
touchTreeState(parent);
}).doOnComplete(() -> {
if (ref.get() != null) {
Scannable t = ref.get();
if (scenario.shouldAssertPostTerminateState()) {
assertThat(t.scanOrDefault(Scannable.Attr.TERMINATED, true)).isTrue();
}
touchTreeState(ref.get());
}
}).doOnNext(d -> touchTreeState(ref.get()));
}
use of org.reactivestreams.Subscriber in project NewPipe by TeamNewPipe.
the class SubscriptionsImportService method getSubscriber.
private Subscriber<List<SubscriptionEntity>> getSubscriber() {
return new Subscriber<List<SubscriptionEntity>>() {
@Override
public void onSubscribe(Subscription s) {
subscription = s;
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(List<SubscriptionEntity> successfulInserted) {
if (DEBUG)
Log.d(TAG, "startImport() " + successfulInserted.size() + " items successfully inserted into the database");
}
@Override
public void onError(Throwable error) {
handleError(error);
}
@Override
public void onComplete() {
LocalBroadcastManager.getInstance(SubscriptionsImportService.this).sendBroadcast(new Intent(IMPORT_COMPLETE_ACTION));
showToast(R.string.import_complete_toast);
stopService();
}
};
}
Aggregations