Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rx-Java: Creating a configurable Observable

I'm new to RxJava, and I am wondering how I can create a configurable Observable? Let's imagine I could write a DB-to-DB transfer like this:

srcDb.getObservable(Bean.class)
     .sql(selectSql)
     .params(selectParams)
     .subscribe(
          trgDb.getSubscriber(Bean.class)
               .sql(insertSql)
     );

I can already do that with the Subscriber, but how can I get some small configuration in the same fashion to the Observable itself?

like image 664
LittleLight Avatar asked Apr 28 '26 16:04

LittleLight


1 Answers

There's 2 ways you can do that:

Option #1: have your own objects do the configuration, and then have an execute(), query() or toObservable() that switches domains:

 srcDb
 .find(Bean.class)
 .sql(selectSql)
 .params(selectParams)
 .execute()
 .subscribe(
      trgDb.getSubscriber(Bean.class)
           .sql(insertSql)
 );

Option #2: use .compose() to re-use common operations:

srcDb
.getObservable(Bean.class)
.compose(addSQLParameters())
.subscribe(
      trgDb.getSubscriber(Bean.class)
           .sql(insertSql)
 );

 <T> Transformer<T,T> addSQLParameters() {
   return obs -> obs.sql(selectSql).params(selectParams);
 }

I would suggest you use option #1, as it allows much better management of your part of the code.

like image 190
Tassos Bassoukos Avatar answered Apr 30 '26 04:04

Tassos Bassoukos