Skip to content

Commit

Permalink
RxJava 3 example
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed May 27, 2021
1 parent 712ffe2 commit 0bfd4fb
Show file tree
Hide file tree
Showing 45 changed files with 2,139 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ Vert.x for RxJava provides most of its APIs as RxJava so you can use those if yo

RxJava is a great choice when you want to perform complex operations on multiple asynchronous streams of data.

The link:rxjava-2-examples/README.adoc[Vert.x RxJava 2] examples contains a wide range of examples using Vert.x for RxJava
The link:rxjava-2-examples/README.adoc[Vert.x RxJava 2] examples contains a wide range of examples using Vert.x for RxJava 2
The link:rxjava-3-examples/README.adoc[Vert.x RxJava 3] examples contains a wide range of examples using Vert.x for RxJava 3

=== gRPC examples

Expand Down
208 changes: 208 additions & 0 deletions rxjava-3-examples/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
= Vert.x RxJava3 extension examples

**IMPORTANT**: These examples use the RX Java 3 API. If you are interested by RX Java 2, check the `rxjava-2-examples`
module.

Here you will find examples demonstrating Vert.x RxJava3 extension in action.

Vert.x RxJava 3 extension provides Rxified version of the Vert.x APIs. Please consult the Vert.x RxJava manual
for detailed documentation on Vert.x core.

== RxJava Web client examples

These examples shows the Rxified HTTP api.

=== Simple

A simple web client.

The client creates an `Single<HttpRequest<String>>` and then subscribe multiple times to the single to send the request.

link:src/main/java/io/vertx/example/reactivex/web/client/simple/Client.java[RxJava 3 simple web client]

=== Zip

A variation of the _simple_ example with two client requests mapped to an `Single<JsonObject>`
and then _zipped_ in a single json object.

The main interest is to get the final result when the two responses are delivered.

link:src/main/java/io/vertx/example/reactivex/web/client/zip/Client.java[RxJava 3 zip web client]

=== Unmarshalling

The web client json response is unmarshalled to a Java object using the web client unmarshalling features.

link:src/main/java/io/vertx/example/reactivex/web/client/unmarshalling/Client.java[RxJava 3 unmarshalling web client]

== RxJava Web examples

=== Real-time

This example demonstrates how an RxJava `Flowable` source can be sent _real-time_ to the browser
via a `SockJSSocket`.

link:src/main/java/io/vertx/example/reactivex/web/realtime/Server.java[RxJava 3 real-time web]

SocksJS gives a WebSocket-like API in client side JavaScript even if the browser or network doesn't support WebSockets.

This is ideal for so-called _real-time_ web applications where you want quick, responsive communication between server
and client and you're probably rendering the user interface on the client side.

Run the server either in your IDE or on the command line, then open your browser and hit
link:http://localhost:8080

This serves the link:src/main/java/io/vertx/example/reactivex/web/realtime/webroot/index.html[index page] which contains
some JavaScript which opens an event bus connection to the server.

When the connection is open, a SockJS connection is opened on the `/news-feed` uri. When data
arrives in the handler the script just uses some simple JQuery to write the message to the page.

On the server side, in the link:src/main/java/io/vertx/example/reactivex/web/realtime/Server.java[server] when a SockJS
connection arrives, we subscribe to an `Flowable<String>` (that is created from the EventBus, but it would be
another source of data) and send to the browser the observed items.

When you get the index page in your browser you should see it update every second as it receives a message.

== RxJava 3 Http examples

These examples shows the Rxified HTTP api.

=== Simple

A simple http server and client.

The server uses an `Flowable<HttpServerRequest>` to serve request:

link:src/main/java/io/vertx/example/reactivex/http/client/simple/Server.java[RxJava 3 simple HTTP server]

The client uses an `Flowable<HttpClientRequest` and applies `flatMap` to get a `Flowable<Buffer>`

link:src/main/java/io/vertx/example/reactivex/http/client/simple/Client.java[RxJava 3 simple HTTP client]

=== Reduce

Same as _simple_ example however the client applies several operations on this flowable to end
with the http client response:

* `flatMap` transforms the `Flowable<HttpClientResponse>` -> `Flowable<Buffer>`
* `reduce` merge all response buffers in a single buffer
* `map` transform the buffer to a string
* `subscribe` delivers the response content

link:src/main/java/io/vertx/example/reactivex/http/client/reduce/Client.java[RxJava 3 reduce HTTP client]

=== Zip

A variation of the _simple_ example with two client requests mapped to an `Flowable<JsonObject>`
and then _zipped_ in a single json object.

The main interest is to get the final result when the two responses are delivered.

link:src/main/java/io/vertx/example/reactivex/http/client/zip/Client.java[RxJava 3 zip HTTP client]

=== Unmarshalling

The http client json response is unmarshalled to a Java object: the `RxHelper.unmarshaller` static method
creates an Rx operator applied to the response via the `lift`.

link:src/main/java/io/vertx/example/reactivex/http/client/unmarshalling/Client.java[RxJava 3 unmarshalling HTTP client]

=== Backpressure

The http server which uses Vert.x-Web router and defines a "drop" strategy when the server is overloaded (backpressure).

link:src/main/java/io/vertx/example/reactivex/web/backpressure/Server.java[RxJava 3 backpressure HTTP server]

== RxJava event bus examples

The event bus provides a natural fit with the Rx api.

=== Publish / Subscribe

A reinterpreation of the core publish / subscribe example with the subscriber using the Rx api.

link:src/main/java/io/vertx/example/reactivex/eventbus/pubsub/Receiver.java[RxJava 3 event bus pubsub receiver]
link:src/main/java/io/vertx/example/reactivex/eventbus/pubsub/Sender.java[RxJava 3 event bus pubsub sender]

=== Ping / Pong

An example of sending, receiving and replying to messages using the Rx api.

link:src/main/java/io/vertx/example/reactivex/eventbus/pingpong/PingPong.java[RxJava 3 simple ping-pong ]

=== Zip replies

The example Sender sends two messages over the event bus and wait for replies, the
_zip_ operation is applied to deliver a single reply when the two replies are received.

link:src/main/java/io/vertx/example/reactivex/eventbus/zipreplies/Receiver.java[RxJava 3 zipreplies eventbus receiver]
link:src/main/java/io/vertx/example/reactivex/eventbus/zipreplies/Sender.java[RxJava 3 zipreplies eventbus sender]

== RxJava 3 Database examples

=== SQL client example

An example showing the SQL client Rxified api, after the client connected to the database, it chains
operations via the `flatMap` operation and then subscribes to the result.

link:src/main/java/io/vertx/example/reactivex/database/sqlclient/Client.java[RxJava 3 SQL client]

=== SQL client Transaction Handling

An example showing an Rxified SQL client api to handle simplified transaction that commits if all succeeded or rollback with
exception propagation to the caller in case of anyone failed.

link:src/main/java/io/vertx/example/reactivex/database/sqlclient/Transaction.java[RxJava SQL transaction]

=== Mongo example

An example showing the Mongo Service Rxified api, after the client connected to Mongo, it chains
`createCollection` and `insert` via _flatMap_ and then subscribes to the result to do a query
in the _onComplete_.

link:src/main/java/io/vertx/example/reactivex/database/mongo/Client.java[RxJava 3 Mongo client]

== Scheduler examples

Vertx for RxJava provides schedulers for performing delayed, periodic actions.

=== Periodic events

RxJava timer can use Vertx scheduler for scheduling actions on the event loop, this example shows a 1 second
periodic flowable scheduled on Vertx event loop.

link:src/main/java/io/vertx/example/reactivex/scheduler/timer/Periodic.java[Periodic scheduled action]

=== Blocking action example

When an Flowable operation is blocking, a blocking Vertx scheduler can be used to perform the action, this
examples shows how blocking operation can be scheduled on Vert.x

link:src/main/java/io/vertx/example/reactivex/scheduler/blocking/Scheduled.java[Blocking scheduled action]
link:src/main/java/io/vertx/example/reactivex/scheduler/blocking/Scheduled.java[Blocking scheduled action]

== Scheduler examples

These examples demonstrate usage of Vert.x net servers and clients with RxJava 3

=== Greeter

This example combines `RecordParser` and RxJava 3 for a TCP client/server exchange.
When the client sends a name to the server, it replies with a greeting.
Names and greetings are line-separated.

link:src/main/java/io/vertx/example/reactivex/net/greeter/Client.java[Greeting client]
link:src/main/java/io/vertx/example/reactivex/net/greeter/Server.java[Greeting Server]

== Services examples

Rxified Vert.x Services examples

=== Service Proxy example

This example shows you how to make your service proxy Rxified with RxJava 3.

link:src/main/java/io/vertx/example/reactivex/services/serviceproxy/SomeDatabaseService.java[Service Proxy interface]
link:src/main/java/io/vertx/example/reactivex/services/serviceproxy/SomeDatabaseServiceVerticle.java[Service Provider Verticle]
link:src/main/java/io/vertx/example/reactivex/services/serviceproxy/ServiceConsumerVerticle.java[Service Consumer Verticle]
129 changes: 129 additions & 0 deletions rxjava-3-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.vertx</groupId>
<artifactId>rxjava-3-examples</artifactId>
<version>4.1.0.CR2</version>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-gen</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java3-gen</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java3</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-jdbc-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mongo-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-service-factory</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-service-proxy</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>

<build>
<pluginManagement>
<plugins>
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessors>
<annotationProcessor>io.vertx.codegen.CodeGenProcessor</annotationProcessor>
</annotationProcessors>
<generatedSourcesDirectory>
${project.basedir}/src/main/generated
</generatedSourcesDirectory>
<compilerArgs>
<arg>-AoutputDirectory=${project.basedir}/src/main</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>staging</id>
<repositories>
<repository>
<id>staging</id>
<url>https://oss.sonatype.org/content/repositories/iovertx-3905/</url>
</repository>
</repositories>
</profile>
</profiles>
</project>
Loading

0 comments on commit 0bfd4fb

Please sign in to comment.