我有一个PublishSubject
和两个Subscriber
用于处理数据。
问题是,当Subscriber
中的一个发生异常时,Subscriber
似乎不再起作用。
我想忽略它们,并继续处理而不使用try-catch
。我该怎么办?
Subject<Integer, Integer> subject = PublishSubject.create();
Observable<Integer> observable = subject.onBackpressureBuffer().asObservable().observeOn(Schedulers.io());
observable.subscribe(integer -> {
if (integer == 2) {
throw new RuntimeException();
}
System.out.println("thread-1 :" + integer);
}, throwable -> System.out.println("thread-1 :" + throwable.getMessage()));
observable.subscribe(integer -> System.out.println("thread-2 :" + integer), throwable -> System.out.println("thread-2 :" + throwable.getMessage()));
for (int i = 0; i < 10; i++) {
TimeUnit.SECONDS.sleep(1L);
try {
subject.onNext(i);
} catch (Exception e) {
e.printStackTrace();
}
}