rx-java

How to handle multiple data sources with rxjava?

蹲街弑〆低调 提交于 2021-02-08 03:46:25
问题 This is the case: I have the domain layer to provide data fetching interface for business logics, and I have 2 data sources: local database and remote network. It works like this: Request all users: DataRepository.getInstance().getUsers(); In DataRepository, there are 2 sources: LocalDataSource.getUsers() which fetches all users from local database, if there is no data then ignore this request. RemoteDataSource.getUsers() which requests latest user list from our server(Even if there is data

Schedulers.io() on parallel database search

こ雲淡風輕ζ 提交于 2021-02-07 19:52:02
问题 I wonder whether I should use Schedulers.io() or Schedulers.newThread() when I access to database tables in parallel. For example, if I use Schedulers.io() to select the records from thousands of tables in parallel, a lot of threads newly created were in the thread pool after the task. Observabe.just("table1", "table2", "table3"...) .flatMap(t -> { // creating the observable that emits the record return Observable create(s -> { Record rec = selectFrom(t); s.onNext(rec); s.onCompleted(); })

Schedulers.io() on parallel database search

北城以北 提交于 2021-02-07 19:50:24
问题 I wonder whether I should use Schedulers.io() or Schedulers.newThread() when I access to database tables in parallel. For example, if I use Schedulers.io() to select the records from thousands of tables in parallel, a lot of threads newly created were in the thread pool after the task. Observabe.just("table1", "table2", "table3"...) .flatMap(t -> { // creating the observable that emits the record return Observable create(s -> { Record rec = selectFrom(t); s.onNext(rec); s.onCompleted(); })

Modify Source Observable on retry - RxJava

十年热恋 提交于 2021-02-07 14:32:10
问题 How do i update a source observable on retry? List<String> ids = new ArrayList<>(); // A,B,C Observable.from(ids) .retryWhen(errors -> { return errors .zipWith(Observable.range(0, 1), (n, i) -> i) .flatMap(retryCount -> Observable.timer((long) Math.pow(2, retryCount), TimeUnit.MINUTES)); }) .subscribe(....); now rather than passing //A,B,C as ids if i want to pass some other values. How do i do it? or is this even the right approach? 回答1: Use defer . This would allow ids to be re-computed:

Modify Source Observable on retry - RxJava

百般思念 提交于 2021-02-07 14:31:29
问题 How do i update a source observable on retry? List<String> ids = new ArrayList<>(); // A,B,C Observable.from(ids) .retryWhen(errors -> { return errors .zipWith(Observable.range(0, 1), (n, i) -> i) .flatMap(retryCount -> Observable.timer((long) Math.pow(2, retryCount), TimeUnit.MINUTES)); }) .subscribe(....); now rather than passing //A,B,C as ids if i want to pass some other values. How do i do it? or is this even the right approach? 回答1: Use defer . This would allow ids to be re-computed:

Dropwizard @UnitOfWork with asynchronous database call

六月ゝ 毕业季﹏ 提交于 2021-02-07 08:58:42
问题 I imagine that this is a common problem, but after some searching I wasn't able to find anything relevant. The problem I'm having is that I'm getting a No Hibernate Session bound to thread exception when annotating my resource method with @UnitOfWork and inside my resource method, making an asynchronous DAO call. The idea behind this design is to make the database call on a separate I/O thread so that it frees up the Jersey resource thread. Unfortunately, as the exception says, this

Dropwizard @UnitOfWork with asynchronous database call

妖精的绣舞 提交于 2021-02-07 08:58:30
问题 I imagine that this is a common problem, but after some searching I wasn't able to find anything relevant. The problem I'm having is that I'm getting a No Hibernate Session bound to thread exception when annotating my resource method with @UnitOfWork and inside my resource method, making an asynchronous DAO call. The idea behind this design is to make the database call on a separate I/O thread so that it frees up the Jersey resource thread. Unfortunately, as the exception says, this

Dropwizard @UnitOfWork with asynchronous database call

混江龙づ霸主 提交于 2021-02-07 08:56:06
问题 I imagine that this is a common problem, but after some searching I wasn't able to find anything relevant. The problem I'm having is that I'm getting a No Hibernate Session bound to thread exception when annotating my resource method with @UnitOfWork and inside my resource method, making an asynchronous DAO call. The idea behind this design is to make the database call on a separate I/O thread so that it frees up the Jersey resource thread. Unfortunately, as the exception says, this

Use flatMap or zip in RxJava2?

本秂侑毒 提交于 2021-02-07 08:40:25
问题 I have a class called Student and it has two fields grade and school . Both of two fields need to be fetched from remote server. When two result returned, I new a Student object. With help of RxJava , I have done it in two ways, one in flatMap and another in zip operator. Observable<String> gradeObservable = Observable.create((ObservableOnSubscribe<String>) emitter -> { Thread.sleep(1000); emitter.onNext("senior"); }).subscribeOn(Schedulers.io()); Observable<String> schoolObservable =

Use flatMap or zip in RxJava2?

五迷三道 提交于 2021-02-07 08:39:46
问题 I have a class called Student and it has two fields grade and school . Both of two fields need to be fetched from remote server. When two result returned, I new a Student object. With help of RxJava , I have done it in two ways, one in flatMap and another in zip operator. Observable<String> gradeObservable = Observable.create((ObservableOnSubscribe<String>) emitter -> { Thread.sleep(1000); emitter.onNext("senior"); }).subscribeOn(Schedulers.io()); Observable<String> schoolObservable =