Skip to content

Commit 8e3671b

Browse files
authored
readme.md with links to 1.x and a quick guide
1 parent 307a6a6 commit 8e3671b

File tree

1 file changed

+133
-1
lines changed

1 file changed

+133
-1
lines changed

README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ RxJava is a Java VM implementation of [Reactive Extensions](http://reactivex.io)
88

99
It extends the [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.
1010

11+
#### Version 1.x
12+
13+
Looking for version 1.x? Jump [to the 1.x branch](https://github.com/ReactiveX/RxJava/tree/1.x).
14+
15+
Timeline plans for the 1.x line:
16+
17+
- **June 1, 2017** - feature freeze (no new operators), only bugfixes
18+
- **March 31, 2018** - end of life, no further development
19+
1120
#### Version 2.x
1221

1322
- single dependency: [Reactive-Streams](https://github.com/reactive-streams/reactive-streams-jvm)
@@ -23,6 +32,129 @@ Version 2.x and 1.x will live side-by-side for several years. They will have dif
2332

2433
See the differences between version 1.x and 2.x in the wiki article [What's different in 2.0](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0). Learn more about RxJava in general on the <a href="https://github.com/ReactiveX/RxJava/wiki">Wiki Home</a>.
2534

35+
## Getting started
36+
37+
The first step is to include RxJava 2 into your project, for example, as a Gradle compile dependency:
38+
39+
```groovy
40+
compile "io.reactivex.rxjava2:rxjava:2.x.y"
41+
```
42+
43+
The second is to write the **Hello World** program:
44+
45+
```java
46+
package rxjava.examples;
47+
48+
import io.reactivex.*;
49+
50+
public class HelloWorld {
51+
public static void main(String[] args) {
52+
Flowable.just("Hello world").subscribe(System.out::println);
53+
}
54+
}
55+
```
56+
57+
If your platform doesn't support Java 8 lambdas (yet), you have to create an inner class of `Consumer` manually:
58+
59+
```java
60+
Flowable.just("Hello world")
61+
.subscribe(new Consumer<String>() {
62+
@Override public void accept(String s) {
63+
System.out.println(s);
64+
}
65+
);
66+
```
67+
68+
RxJava 2 features several base classes you can discover operators on:
69+
70+
- `io.reactivex.Flowable` : 0..N flows, supporting Reactive-Streams and backpressure
71+
- `io.reactivex.Observable`: 0..N flows, no backpressure
72+
- `io.reactivex.Single`: a flow of exactly 1 item or an error
73+
- `io.reactivex.Completable`: a flow without items but only a completion or error signal
74+
- `io.reactivex.Maybe`: a flow with no items, exactly one item or an error
75+
76+
One of the common use cases for RxJava is to run some computation, network request on a background thread and show the results (or error) on the UI thread:
77+
78+
```java
79+
Flowable.fromCallable(() -> {
80+
Thread.sleep(1000); // imitate expensive computation
81+
return "Done";
82+
})
83+
.subscribeOn(Schedulers.io())
84+
.observeOn(Schedulers.single())
85+
.subscribe(System.out::println, Throwable::printStackTrace);
86+
87+
Thread.sleep(2000); // <--- wait for the flow to finish
88+
```
89+
90+
This style of chaining methods is called a **fluent API** which resembles the **builder pattern**. However, RxJava's reactive types are immutable; each of the method calls returns a new `Flowable` with added behavior. To illustrate, the example can be rewritten as follows:
91+
92+
```java
93+
Flowable<String> source = Flowable.fromCallable(() -> {
94+
Thread.sleep(1000); // imitate expensive computation
95+
return "Done";
96+
});
97+
98+
Flowabe<String> runBackground = source.subscribeOn(Schedulers.io());
99+
100+
Flowable<String> showForeground = runBackground.observeOn(Schedulers.single());
101+
102+
showForeground.subscribe(System.out::println, Throwable::printStackTrace);
103+
104+
Thread.sleep(2000);
105+
```
106+
107+
Typically, you can move computations or blocking IO to some other thread via `subscribeOn`. Once the data is ready, you can make sure they get processed on the foreground or GUI thread via `observeOn`.
108+
109+
RxJava operators don't work `Thread`s or `ExecutorService`s directly but with so called `Scheduler`s that abstract away sources of concurrency behind an uniform API. RxJava 2 features several standard schedulers accessible via `Schedulers` utility class. These are available on all JVM platforms but some specific platforms, such as Android, have their own typical `Scheduler`s defined: `AndroidSchedulers.mainThread()`, `SwingScheduler.instance()` or `JavaFXSchedulers.gui()`.
110+
111+
The `Thread.sleep(2000);` at the end is no accident. In RxJava the default `Scheduler`s run on daemon threads, which means once the Java main thread exits, they all get stopped and background computations may never happen. Sleeping for some time in this example situations let's you see the output of the flow on the console with time to spare.
112+
113+
Flows in RxJava are sequential in nature split into processing stages that may run **concurrently** with each other:
114+
115+
```java
116+
Flowable.range(1, 10)
117+
.observeOn(Schedulers.computation())
118+
.map(v -> v * v)
119+
.blockingSubscribe(System.out::println);
120+
```
121+
122+
This example flow squares the numbers from 1 to 10 on the **computation** `Scheduler` and consumes the results on the "main" thread (more precisely, the caller thread of `blockingSubscribe`). However, the lambda `v -> v * v` doesn't run in parallel for this flow; it receives the values 1 to 10 on the same computation thread one after the other.
123+
124+
Processing the numbers 1 to 10 in parallel is a bit more involved:
125+
126+
```java
127+
Flowable.range(1, 10)
128+
.flatMap(v ->
129+
Flowable.just(v)
130+
.subscribeOn(Schedulers.computation())
131+
.map(v -> v * v)
132+
)
133+
.blockingSubscribe(System.out::println);
134+
```
135+
136+
Practically, paralellism in RxJava means running independent flows and merging their results back into a single flow. The operator `flatMap` does this by first mapping each number from 1 to 10 into its own individual `Flowable`, runs them and merges the computed squares.
137+
138+
`flatMap` is a powerful operator and helps in a lot of situations. For example, given a service that returns a `Flowable`, we'd like to call another service with values emitted by the first service:
139+
140+
```java
141+
Flowable<Inventory> inventorySource = warehouse.getInventoryAsync();
142+
143+
inventorySource.flatMap(inventoryItem ->
144+
erp.getDemandAsync(inventoryItem.getId())
145+
.map(demand
146+
-> System.out.println("Item " + inventoryItem.getName() + " has demand " + demand));
147+
)
148+
.subscribe();
149+
```
150+
151+
Note, however, that `flatMap` doesn't guarantee any order and the end result from the inner flows may end up interleaved. There are alternative operators:
152+
153+
- `concatMap` that maps and runs one inner flow at a time and
154+
- `concatMapEager` which runs all inner flows "at once" but the output flow will be in the order those inner flows were created.
155+
156+
For further details, consult the [wiki](https://github.com/ReactiveX/RxJava/wiki).
157+
26158
## Communication
27159

28160
- Google Group: [RxJava](http://groups.google.com/d/forum/rxjava)
@@ -130,4 +262,4 @@ See the License for the specific language governing permissions and
130262
limitations under the License.
131263

132264
[beta source link]: https://github.com/ReactiveX/RxJava/blob/master/src/main/java/rx/annotations/Beta.java
133-
[experimental source link]: https://github.com/ReactiveX/RxJava/blob/master/src/main/java/rx/annotations/Experimental.java
265+
[experimental source link]: https://github.com/ReactiveX/RxJava/blob/master/src/main/java/rx/annotations/Experimental.java

0 commit comments

Comments
 (0)