1

I'm experimenting both Vert.x and Reactive Extensions together (RXJava) and finding it difficult to compose async events.

Problem statement

  1. Establish a DB connection with auto commit turned off.
  2. Bulk insert data into a table1
  3. Update status in table2
  4. Commit

Since most operations in Vert.x are asynchronous, I'm trying to compose/chain them using RxJava. This is what I've come up with so far.

public Observable<Boolean> insertBulkDataAndUpdateStatus (final List<JsonArray> inputs,final MyObject obj){

    System.out.println("Total Rows to be inserted : "+inputs.size());
    return getModifiedConnObservable()
            .flatMap(conn ->{

                System.out.println("Obtained connection .. ");

                inputs.forEach(json->{
                    System.out.println("Inserting a row");

                    conn.updateWithParamsObservable(sql, json).subscribe();
                });
                //Checkpoint 1
                System.out.println("updating completion status");
                obj.complete();
                updateStatus(conn,obj).subscribe(result->{
                    System.out.println("Committing");
                    commit(conn).subscribe();
                });

                return Observable.just(true);
            });
}

Observable<SQLConnection> getModifiedConnObservable(){
    return _jdbc.getConnectionObservable().flatMap(conn->{
        return  Observable.just(conn).doOnNext(con->con.setAutoCommitObservable(false).subscribe()).doOnUnsubscribe(conn::close);
    });
}

While this kind of works, I'm sure this isn't the ideal approach.

  • Point #2(bulk insert for each record) and #3 final status update aren't chained together. And I'm not sure if any record failure during bulk insert would stop #3 to happen.

Coming from an imperative background, I'm finding it difficult to grasp Rx concepts and asynch events together. Any help is much appreciated.

user378101
  • 649
  • 3
  • 12
  • 19

0 Answers0