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();
1
  • 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. Commented Aug 29, 2018 at 10:15

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.