0

Having trouble with backpressure. Using publish subject to get Sensor event on emitting, and need to save data to database on subscribe to subject in transaction.

I have been trying to use .window(100) operator so i can bulk insert whenever i get 100 sensor event's in a row but i can only get one item at .subscribe()

Don't want to drop events by using buffer operator. What is the right way to handle this?

@Override
public void onSensorChanged(SensorEvent sevent) {

    Sensor sensor = sevent.sensor;

    switch (sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            sensorEventPublishSubject.onNext(sevent);
            break;
    }
}

sensorEventPublishSubject
            .map(event ->
                    new AccModel(
                            event.values[0],
                            event.values[1],
                            event.values[2],
                            event.accuracy                           
                    )
            )
            .window(100)
            .subscribe(
                    new Action1<Observable<AccModel>>() {
                        @Override
                        public void call(Observable<AccModel> accModelObservable) {
                            //insert in db
                        }
                    }
            );
ddog
  • 670
  • 1
  • 10
  • 25

1 Answers1

1

You have two options, depending on what you want to do with the onError event.

First off, your solution using .window is correct, it's just that it emits an Observable, and you will get one Observable every 100 events, and that Observable will, when you subscribe to it, replay those 100 events. Also, in the case of an error, it will replay the error event in sequence too (AFAIK).

If you don't care about the error event in the sequence, then there's the solution with .buffer(100), in front of which you should put onErrorReturn() or onErrorResumeNext() which you will use to transform the onError event into the onNext. That's because in case of an onError, buffer operator immediately propagates it so you lose the events in the temporary buffer (<100).

Sasa Sekulic
  • 669
  • 4
  • 12