Search code examples
javaspringrx-javareactive-programmingspring-ioc

Spring + RxJava + Schedule cron job: why beans called only once on connectable observable


I have following code that demonstrates issue:

@Component
public class App {
    @Autowired S1 s1;
    @Autowired S2 s2;
    int jobs = 0;
    @Scheduled(cron = "0 * * * * ?")
    void foo() {
        System.out.println("schedule cron job: " + jobs++);
        Observable<String> observable = Observable.just("bar");
        ConnectableObservable<String> publishedObservable = observable.publish();
        publishedObservable.subscribe(s1);
        publishedObservable.subscribe(s2);
        publishedObservable.connect();
    }
}

Subscriber1:

@Component
public class S1 extends Subscriber<String> {
    private AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
    }

    @Override
    public void onNext(String s) {
        System.out.println("S1:::: Times called: " + counter.getAndIncrement() + ", input: " + s);

    }
}

Subscriber2:

@Component
public class S2 extends Subscriber<String> {
    private AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
    }

    @Override
    public void onNext(String s) {
        System.out.println("S2:::: Times called: " + counter.getAndIncrement() + ", input: " + s);
    }
}

The output will be:

schedule cron job: 0
S1:::: Times called: 0, input: bar
S2:::: Times called: 0, input: bar
schedule cron job: 1
schedule cron job: 2
schedule cron job: 3
schedule cron job: 4
......

Why is S1 and S2 not called each time the foo method called ? How to achieve that ?

Is this because rx some subscriptions logic or because of these beans are singletons?


Solution

  • Why is S1 and S2 not called each time the foo method called ?

    RxJava Subscribers are stateful and once they consumed a sequence they are no longer usable and report themselves as unsubscribed. Subscribing with them again has no effect. You have to re-create them each time you need to subscribe to the source.