1

Lets say I have 3 servers (with same API):

| S1 | S2 | S3 |

| postSomething(data) | postSomething(data) | postSomething(data) |

What I need is this (in sequence):

  1. S1-postSomething(100) , S2-postSomething(100) , S3-postSomething(100)
  2. Sleep
  3. do simple calculatios
  4. S1-postSomething(90) , S2-postSomething(90) , S3-postSomething(90)
  5. Sleep
  6. do simple calculation from domain
  7. S1-postSomething(80) , S2-postSomething(80) , S3-postSomething(80)
  8. Sleep
  9. do simple calculations
  10. Update domain after done with all 3 calculations made

Few notes:

  • Steps 1,4,7 doesn't have to be in sequence. but has to finish all 3 requests before going to sleep (I used single thread thread pool for the whole job, so the 3 post is in sequence).
  • I don't care about postSomething(data) response data

pseudo code:

int value = 100;
List<Observable> tuning = new List()
for (int tuningStep = 0; tuningStep < 10; tuningStep++) {
    for (LampUnit lampUnit : lampUnits.getAllLampUnits()) {
        // Don't care about response as long gettings success
        Observable<Integer> post = service.postSomething(lampUnit.getId(), value);
        result.add(post)
    }
    result.add(() -> {
        Thread.sleep(5000L)
        return 1;
    });
    result.add(() -> {
        return doCalculations();
    });
    value -= 10;
}

result.subscribeOn(Schedulers.from(Executors.newFixedThreadPool(1))
    .observeOn(Schedulers.from(this.executor))
    .subscribe();
sharonooo
  • 684
  • 3
  • 8
  • 25
  • Unqualified comment: I didn't grasp the full extent of your goal, but it sounds like the static methods `Observable.zip`, `Observable.concat` or `Observable.merge` might be helpful here. – Link64 Aug 29 '18 at 10:15

1 Answers1

1

Probably more pseudo than yours, but it may hint at your desired solution:

int val = 100;
Observable res = Observable.empty();
while(val >= 0) {
    res = Observable.concat(
            res,
            service.postSomething([...], val),
            Observable.timer(5, TimeUnit.SECONDS)
    );
    val -= 10;
}
res.subscribe(); //completion handler will be invoked when done
Link64
  • 718
  • 4
  • 20