0

I am learning the Reactive Stream API of Java 9.

As it has the Publisher and Subscriber, and the Subscriber subscribes to the Publisher and the Subscriber also implements the following messages:

public class TestSubscriber<T> implements Subscriber<T> {

    @Override
    public void onComplete() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onError(Throwable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNext(T arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSubscribe(Subscription arg0) {
        // TODO Auto-generated method stub

    }

}

I did not found any method in Subscriber which passes/transfers the message to another subscriber. Any suggestions?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
KayV
  • 12,987
  • 11
  • 98
  • 148
  • Interesting, but what shall that be required for? I mean if we are talking about a [Subscription](https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.Subscription.html), it is between a Subscriber and a Publisher in general. – Naman Dec 22 '17 at 06:15
  • Say If a Subscriber wants to change/modify the message, and pass to another Subscriber – KayV Dec 22 '17 at 06:37
  • 1
    Firstly, I guess you can merge these https://stackoverflow.com/questions/47936868/can-a-subscriber-act-as-a-publisher and keep one with the detailed question. – Naman Dec 22 '17 at 06:38

1 Answers1

1

This can be done implementing the Flow.Processor as follows:

import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Subscription;
import java.util.concurrent.SubmissionPublisher;
import java.util.function.Function;

public class MyTransformer<T, R> extends SubmissionPublisher<R> implements Flow.Processor<T, R> {

    private Function<T, R> function;
    private Flow.Subscription subscription;

    public MyTransformer(Function<T, R> function) {
        super();
        this.function = function;
    }

    @Override
    public void onComplete() {
        System.out.println("Transformer Completed");
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
    }

    @Override
    public void onNext(T item) {
        System.out.println("Transformer Got : "+item);
        submit(function.apply(item));
        subscription.request(1);

    }

    @Override
    public void onSubscribe(Subscription subscription) {
        this.subscription = subscription;
        subscription.request(1);
    }


}

And when calling it use as follows:

public class TestTransformer {

    public static void main(String... args) {
        SubmissionPublisher<String> publisher = new SubmissionPublisher<>();
        MyTransformer<String, Integer> transformProcessor = new MyTransformer<>(Integer::parseInt);

        TestSubscriber<Integer> subscriber = new TestSubscriber<>();
        List<String> items = List.of("1", "2", "3");

        List<Integer> expectedResult = List.of(1, 2, 3);

        publisher.subscribe(transformProcessor);
        transformProcessor.subscribe(subscriber);
        items.forEach(publisher::submit);
        publisher.close();

    }
}
KayV
  • 12,987
  • 11
  • 98
  • 148