I saw this question here.
It's about achieving delay for each emitted item. This is how to achieve it based on the accepted answer:
Observable.zip(Observable.range(1, 5)
.groupBy(n -> n % 5)
.flatMap(g -> g.toList()),
Observable.interval(50, TimeUnit.MILLISECONDS),
(obs, timer) -> obs)
.doOnNext(item -> {
System.out.println(System.currentTimeMillis() - timeNow);
System.out.println(item);
System.out.println(" ");
}).toList().toBlocking().first();
In the question, the asker specifically asked for a fixed set of observables (Observable.range(1,5)), unfortunately this is not what I want to achieve.
I also saw this comment.
That comment is what I want to achieve. So my source observable emits items at a slower (and sometimes faster) rate than the interval. Also the observable's emits are never ending.
===
So basically I want hot observables to have minimum delay.
For example, if I want 400ms minimum delay and I have this kind of observable emittance:
X1-100ms delay-X2-200ms delay-X3-600ms delay-X4-20000ms delay-X5-...
I want it to yield:
X1-400ms delay-X2-400ms delay-X3-600ms delay-X4-20000ms delay-X5-...
Anybody have any idea to achieve that?
Your requirement is so strange...
I can resolve it but not elegant. Here is my code:
class Three<A, B, C> {
A a;
B b;
C c;
// Getter, Setter, Constructor
}
public static void main(String[] args) throws Exception {
BehaviorSubject<Integer> s = BehaviorSubject.create();
// Three = (The value, upstream comes mills, downstream emits mills)
s.map(i -> new Three<>(i, System.currentTimeMillis(), System.currentTimeMillis()))
.scan((a, b) -> {
b.setC(a.getC() + Math.max(400L, b.getB() - a.getB()));
return b;
})
.concatMap(i -> Observable.just(i.getA()).delay(Math.max(0, i.getC() - System.currentTimeMillis()),
TimeUnit.MILLISECONDS))
.subscribe(i -> System.out.println(i + "\t" + System.currentTimeMillis()));
s.onNext(0);
Thread.sleep(100);
s.onNext(1);
Thread.sleep(200);
s.onNext(2);
Thread.sleep(600);
s.onNext(3);
Thread.sleep(2000);
s.onNext(4);
Thread.sleep(200);
s.onNext(5);
Thread.sleep(800);
s.onNext(6);
Thread.sleep(1000);
}
and output
0 1510128693984
1 1510128694366 // 400ms
2 1510128694766 // 400ms
3 1510128695366 // 600ms
4 1510128697366 // 2000ms
5 1510128697766 // 400ms
6 1510128698567 // 800ms
Related
I need to execute a task which includes four steps.
Each step relies on the result of previous one. Executing all steps in one thread needs long time.
I want to use four threads and each one performs one step and use a buffer between neighboring two steps to store the result of the previous step.
I am developing on Android platform using Java.
Can anybody give me an example?
Thanks a lot.
YL
I was curious, how the (non magically parallel) code would look like using reactive streams in java9. Turned out, the Java9 infrastructure is fragmentary and to be used with caution. So I chose to base the example on JavaRx, providing "Flows". There's an android extension available for that.
I put it in perspective with Streams, parallelStreams and sequential flows.
public class FlowStream {
#Test
public void flowStream() {
int items = 10;
List<Integer> source = IntStream.range(0, items - 1).boxed().collect(Collectors.toList());
print("\nstream");
source.stream().map(this::exp).map(this::exp).forEach(i -> print("streamed %d", i));
print("\nparallelStream");
source.parallelStream().map(this::exp).map(this::exp).forEach(i -> print("streamed %d parallel", i));
print("\nflow");
Flowable.range(0, items)
.map(this::exp)
.map(this::exp)
.forEach(i -> print("flowed %d", i));
print("\nparallel flow");
Flowable.range(0, items)
.flatMap(v ->
Flowable.just(v)
.subscribeOn(Schedulers.computation())
.map(this::exp)
)
.flatMap(v ->
Flowable.just(v)
.subscribeOn(Schedulers.computation())
.map(this::exp)
).forEach(i -> print("flowed parallel %d", i));
await(5000);
}
private Integer exp(Integer i) {
print("making %d more expensive", i);
await(Math.round(10f / (Math.abs(i) + 1)) * 50);
return i;
}
private void await(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void print(String pattern, Object... values) {
System.out.println(String.format(pattern, values));
}
}
<!-- https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava -->
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.13</version>
</dependency>
I'm trying to create a server call using RxJava2 library that will try to poll server for answer and if receives exception 3 times in a row to return that exception
I've set up a basic call that fetches the response from the server
final Observable<ResponseValue> responseValueObservable = Observable
.fromCallable((Callable) (c) -> return getDispatcher().performSubmit(submitValue);
}
});
return responseValueObservable
.retry(3)
.subscribeOn(Schedulers.io()
.onError((t) -> { log.error(t); Observable.timer(2, SECONDS);}
.retryUntil(() -> { return retryIsEnabled }
so getDispatcher().performSubmit(submitValue) returns either SubmitException or ResponseValue object.
I need the code to retry 3 times, pausing after each exception for 2 seconds and return either ResponseValue or the last SubmitException
So after reading Dan Lew's Blog from previous answer I was able to put together this piece of code which does exactly what I wanted to. retryWhen() on re-subscribes automatically after waiting 2 seconds. With the first successful reply from server it stops.
Observable.fromCallable((d::performSubmit))
.subscribeOn(Schedulers.io())
.doOnSubscribe(subscription -> System.out.println("Subscribing"))
.retryWhen(errors -> {
AtomicInteger counter = new AtomicInteger();
return errors
.takeWhile(e -> counter.incrementAndGet() < 3)
.flatMap(e -> {
System.out.println("delay retry by 2 second(s)");
return Observable.timer(2, TimeUnit.SECONDS);
});
}).blockingSubscribe(res -> result = Optional.of(res), throwable -> t = Optional.of((Exception) throwable));
Use the retryWhen() operator to customize the response to errors. From the excellent overview at Dan Lew's Blog:
responseValueObservable
.retryWhen( errorObservable -> errorObservable
.zipWith(Observable.range(1, 3), (n, i) -> i)
.flatMap(retryCount -> Observable.timer(2, TimeUnit.SECONDS)))
...
I've been stuck with this for a day. Inspired in Dan Lew great post, I tried to make a simple testcase for repeatWhen() and retryWhen():
public class ObsTest {
private static final Logger LOG = LoggerFactory.getLogger(ObsTest.class);
#Test
public void test1() throws InterruptedException {
Observable<Integer> obs = rx.Observable.<Integer> create(observer -> {
LOG.info("onSubscribe");
Integer data = RandomUtils.nextInt(0, 1000);
if (data % 2 != 0) {
observer.onError(new RuntimeException("Odd number " + data));
} else {
observer.onNext(data);
}
observer.onCompleted();
}, BackpressureMode.BUFFER);
obs.repeatWhen(completed -> completed.delay(1, TimeUnit.MILLISECONDS))
.retryWhen(error -> error.delay(1, TimeUnit.MILLISECONDS))
.subscribe(i -> LOG.info("value={}", i), e -> LOG.info("Exception = {}", e.getMessage()));
}
My idea is this should run forever, emitting even numbers as "correct" results, and odd numbers as "errors".
Instead, this runs for one or two loops and then stops. And that is when the delay is 1 millisecond, for longer periods of time (ie. 1 second), it runs a single time, emitting just a single odd or even number.
I'm sure I'm doing something wrong, but I can't find what it is.
When you call delay which uses Schedulers.computation() by default you are introducing asynchrony. Once activity starts occurring in a background thread your test will finish and presumably your process is exited. You need to use a blockingSubscribe or put a longish Thread.sleep at the end.
As Dave Moten mentioned, delay uses Schedulers.computation() by default, but you can pass scheduler of your choice instead - for tests purposes you may use TestScheduler and "take control over time". Code below shows how can it be used - as you can see this subscription won't terminal for another 30 days, what is basically forever ;)
public class ObsTest {
#Test
public void test1() {
Observable<Integer> obs = rx.Observable.create(observer -> {
Integer data = RandomUtils.nextInt(0, 1000);
if (data % 2 != 0) {
observer.onError(new RuntimeException("Odd number " + data));
} else {
observer.onNext(data);
}
observer.onCompleted();
}, Emitter.BackpressureMode.BUFFER);
TestScheduler scheduler = Schedulers.<Integer>test();
AssertableSubscriber subscriber = obs.repeatWhen(completed -> completed.delay(1, TimeUnit.MILLISECONDS, scheduler))
.retryWhen(error -> error.delay(1, TimeUnit.MILLISECONDS, scheduler))
.subscribeOn(scheduler)
.test();
subscriber.assertNoValues();
scheduler.advanceTimeBy(30, TimeUnit.SECONDS);
subscriber.assertNoTerminalEvent();
scheduler.advanceTimeBy(30, TimeUnit.DAYS);
subscriber.assertNoTerminalEvent();
}
}
This question already has answers here:
RxJava delay for each item of list emitted
(17 answers)
Closed 6 years ago.
I have an observable I've created from a list of objects. For each object in the list I make a network request but I'd like to put a delay between each item in the list as to space out the requests a bit. Here's a snippet of my code.
return Observable.from(documentGroupModels).flatMap(new Func1<DocumentGroupModel, Observable<Boolean>>() {
#Override
public Observable<Boolean> call(DocumentGroupModel documentGroupModel) {
return refreshDocumentWithUri(documentGroupModel.getUri(), documentGroupModel.sectionGroupId,
includeExceptions, false);
}
});
Using delay or buffer doesn't quite work for this scenario as far as I can tell.
You can use a combination of Zip and interval operator if your delay is static, so you can emit an item of your zip every time configure on your interval.
Check the example
#Test
public void delaySteps() {
long start = System.currentTimeMillis();
Subscription subscription =
Observable.zip(Observable.from(Arrays.asList(1, 2, 3)), Observable.interval(200, TimeUnit.MILLISECONDS),
(i, t) -> i)
.subscribe(n -> System.out.println("time:" + (System.currentTimeMillis() - start)));
new TestSubscriber((Observer) subscription).awaitTerminalEvent(3000, TimeUnit.MILLISECONDS);
}
Also you can create an Observable with your list and use concatMap, then you can use delay for every item emitted. Maybe this solution is more elegant and no so Hacky
#Test
public void delayObservableList() {
Observable.from(Arrays.asList(1, 2, 3, 4, 5))
.concatMap(s -> Observable.just(s).delay(100, TimeUnit.MILLISECONDS))
.subscribe(n -> System.out.println(n + " emitted"),
e -> {
},
() -> System.out.println("All emitted"));
new TestSubscriber().awaitTerminalEvent(1000, TimeUnit.MILLISECONDS);
}
You can see another examples of delay here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/utils/ObservableDelay.java
Use the `delay' operator e.g.
return Observable.from(documentGroupModels).flatMap(new Func1<DocumentGroupModel, Observable<Boolean>>() {
#Override
public Observable<Boolean> call(DocumentGroupModel documentGroupModel) {
return refreshDocumentWithUri(documentGroupModel.getUri(), documentGroupModel.sectionGroupId,
includeExceptions, false).delay(2000, TimeUnit.MILLISECONDS);
}
});
I have the following code:
public void onEnter() {
Observable<GObject> obs = context.update(activeG);
obs.subscribe((gObj) -> {
//TODO: add delay of 3 sec.
activeG.publishNewG();
activeG.nextState(GType.RUNNING);
});
}
My question is, How can I put a delay of 3 seconds so
activeG.publishNewG()
is called, then delay of 3 seconds, and then a call to
activeGame.nextState(GameStateType.RUNNING);
'publishNewG' returns void.
Thank you!
If I understand correctly, you want to put a 3 second delay between publishNewG and nextState. You can use doOnNext to inject activity at certain points in the sequence, for example, before and after a 3 second delay:
Observable.just(1)
.doOnNext(e -> System.out.println("Action before"))
.delay(3, TimeUnit.SECONDS)
.doOnNext(e -> System.out.println("Action after"))
.toBlocking().first();
You would typically accomplish this using a scheduler of some sort. Java has ScheduledExecutorService that does this for you.
public class MyClass {
private final ScheduledExecutorService scheduler;
public MyClass() {
scheduler = Executors.newSingleThreadScheduledExecutor();
}
public void onEnter() {
Observable<GObject> obs = context.update(activeG);
obs.subscribe((gObj) -> {
activeG.publishNewG();
// Run in 3 seconds
scheduler.schedule(() -> {
activeG.nextState(GType.RUNNING);
}, 3, TimeUnit.SECONDS);
});
}
}
Edit: From what I can understand of the RxJava docs this is how you would do it with delay:
public void onEnter() {
Observable<GObject> obs = context.update(activeG);
// Observable that delays all events by 3 seconds
Observable<GObject> delayed = obs.delay(3, TimeUnit.SECONDS);
// This will fire immediately when an event happens
obs.subscribe((gObj) -> {
activeG.publishNewG();
});
// This will fire 3 seconds after an event happens
delayed.subscribe((gObj) -> {
activeG.nextState(GType.RUNNING);
});
}
You can easily add delay by using the timer operator.
For example:
// simulated stream of events spread apart by 400ms
Observable<Long> yourObservable = Observable.interval(400, TimeUnit.MILLISECONDS).take(3);
yourObservable.flatMap(data -> {
// add delay of 3 seconds
return Observable.timer(3, TimeUnit.SECONDS).map(i -> data);
}).map(dataAfterDelay -> {
// do whatever you want after 3 seconds
System.out.println("got data " + dataAfterDelay + " after 3 seconds");
return dataAfterDelay + " processed after delay";
}).toBlocking().forEach(System.out::println); // toBlocking here just as example to block main thread
AFAIK you can use Thread.sleep(3000)
If not, something like this should do:
long start = new Date().getTime();
while(new Date().getTime() - start < 3000L){}