Package org.reactivestreams.example.unicast

Source Code of org.reactivestreams.example.unicast.InfiniteIncrementNumberPublisher

package org.reactivestreams.example.unicast;

import java.util.concurrent.atomic.AtomicLong;

import org.reactivestreams.Subscription;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Publisher;

class InfiniteIncrementNumberPublisher implements Publisher<Integer> {

    @Override
    public void subscribe(final Subscriber<? super Integer> s) {

        final AtomicInteger i = new AtomicInteger();

        Subscription subscription = new Subscription() {

            AtomicLong capacity = new AtomicLong();

            @Override
            public void request(long n) {
                System.out.println("signalAdditionalDemand => " + n);
                if (capacity.getAndAdd(n) == 0) {
                    // start sending again if it wasn't already running
                    send();
                }
            }

            private void send() {
                System.out.println("send => " + capacity.get());
                // this would normally use an eventloop, actor, whatever
                new Thread(new Runnable() {

                    public void run() {
                        do {
                            s.onNext(i.incrementAndGet());
                        } while (capacity.decrementAndGet() > 0);
                    }
                }).start();
            }

            @Override
            public void cancel() {
                capacity.set(-1);
            }

        };

        s.onSubscribe(subscription);

    }
}
TOP

Related Classes of org.reactivestreams.example.unicast.InfiniteIncrementNumberPublisher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.