-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Scheduled.java
50 lines (39 loc) · 1.3 KB
/
Scheduled.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io.vertx.example.reactivex.scheduler.blocking;
import io.reactivex.Flowable;
import io.reactivex.Scheduler;
import io.vertx.core.Launcher;
import io.vertx.reactivex.core.AbstractVerticle;
/*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class Scheduled extends AbstractVerticle {
public static void main(String[] args) {
Launcher.executeCommand("run", Scheduled.class.getName());
}
private String blockingLoad(String id) {
// Simulate a blocking action
try {
Thread.sleep(100);
return "someData for " + id + " on thread " + Thread.currentThread().getName();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}
@Override
public void start() throws Exception {
Flowable<String> o = Flowable.just("someID1", "someID2", "someID3", "someID4");
// This scheduler can execute blocking actions
Scheduler scheduler = io.vertx.reactivex.core.RxHelper.blockingScheduler(vertx);
// All operations done on the observer now can be blocking
o = o.observeOn(scheduler);
// Load from a blocking api
o = o.map(this::blockingLoad
);
o.subscribe(item -> {
System.out.println("Got item " + item);
}, Throwable::printStackTrace, () -> {
System.out.println("Done");
});
}
}