Skip to content

Commit 4c7f165

Browse files
danidemipivovarit
authored andcommitted
Bael 776 vert.x and rxjava (eugenp#2529)
* Burlap & Hessian server added * Burlap & Hessian client work * Fixed main * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Fixed main * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Burlap & Hessian client work * Fixed main * Fixed main * Fixed formatting * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Fixed POM * Vertx / RxJava example * BAEL-776 - Vertx and RxJava * BAEL-776 - Vertx and RxJava
1 parent 7df2635 commit 4c7f165

9 files changed

Lines changed: 319 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
4141
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
4242

4343
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
44+
/vertx-and-rxjava/.vertx/

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
<module>spring-boot-property-exp</module>
241241
<module>mockserver</module>
242242
<module>undertow</module>
243+
<module>vertx-and-rxjava</module>
243244
</modules>
244245

245246
<dependencies>

‎vertx-and-rxjava/pom.xml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>vertx-and-rxjava</artifactId>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<configuration>
22+
<source>1.8</source>
23+
<target>1.8</target>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
29+
<dependencies>
30+
31+
<dependency>
32+
<groupId>io.vertx</groupId>
33+
<artifactId>vertx-rx-java2</artifactId>
34+
<version>3.5.0.Beta1</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.vertx</groupId>
38+
<artifactId>vertx-core</artifactId>
39+
<version>3.5.0.Beta1</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.vertx</groupId>
43+
<artifactId>vertx-unit</artifactId>
44+
<version>3.5.0.Beta1</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>4.12</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.slf4j</groupId>
55+
<artifactId>slf4j-api</artifactId>
56+
<version>1.7.25</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>ch.qos.logback</groupId>
60+
<artifactId>logback-classic</artifactId>
61+
<version>1.2.3</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.12</version>
67+
</dependency>
68+
69+
</dependencies>
70+
71+
</project>

‎vertx-and-rxjava/pom.xml.orig‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>vertx-and-rxjava</artifactId>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<configuration>
22+
<source>1.8</source>
23+
<target>1.8</target>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
29+
<dependencies>
30+
31+
<dependency>
32+
<groupId>io.vertx</groupId>
33+
<artifactId>vertx-rx-java2</artifactId>
34+
<version>3.5.0.Beta1</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.vertx</groupId>
38+
<artifactId>vertx-core</artifactId>
39+
<version>3.5.0.Beta1</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.vertx</groupId>
43+
<artifactId>vertx-unit</artifactId>
44+
<version>3.5.0.Beta1</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>4.12</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.slf4j</groupId>
55+
<artifactId>slf4j-api</artifactId>
56+
<version>1.7.25</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>ch.qos.logback</groupId>
60+
<artifactId>logback-classic</artifactId>
61+
<version>1.2.3</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.12</version>
67+
</dependency>
68+
69+
</dependencies>
70+
71+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung;
2+
3+
import java.text.MessageFormat;
4+
5+
class CityAndDayLength {
6+
7+
private final String city;
8+
private final double dayLengthInHours;
9+
10+
public CityAndDayLength(String city, long dayLengthInSeconds) {
11+
this.city = city;
12+
this.dayLengthInHours = dayLengthInSeconds / (60.0 * 60.0);
13+
}
14+
15+
@Override public String toString() {
16+
return MessageFormat.format("In {0} there are {1,number,#0.0} hours of light.", city, dayLengthInHours);
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung;
2+
3+
import io.reactivex.Flowable;
4+
import io.vertx.core.http.RequestOptions;
5+
import io.vertx.reactivex.core.http.HttpClient;
6+
import io.vertx.reactivex.core.http.HttpClientRequest;
7+
import io.vertx.reactivex.core.http.HttpClientResponse;
8+
9+
import static java.lang.String.format;
10+
11+
public class MetaWeatherClient {
12+
13+
private static RequestOptions metawether = new RequestOptions()
14+
.setHost("www.metaweather.com")
15+
.setPort(443)
16+
.setSsl(true);
17+
18+
/**
19+
* @return A flowable backed by vertx that automatically sends an HTTP request at soon as the first subscription is received.
20+
*/
21+
private static Flowable<HttpClientResponse> autoPerformingReq(HttpClient httpClient, String uri) {
22+
HttpClientRequest req = httpClient.get(new RequestOptions(metawether).setURI(uri));
23+
return req.toFlowable()
24+
.doOnSubscribe(subscription -> req.end());
25+
}
26+
27+
static Flowable<HttpClientResponse> searchByCityName(HttpClient httpClient, String cityName) {
28+
HttpClientRequest req = httpClient.get(
29+
new RequestOptions()
30+
.setHost("www.metaweather.com")
31+
.setPort(443)
32+
.setSsl(true)
33+
.setURI(format("/api/location/search/?query=%s", cityName)));
34+
return req
35+
.toFlowable()
36+
.doOnSubscribe(subscription -> req.end());
37+
}
38+
39+
static Flowable<HttpClientResponse> getDataByPlaceId(HttpClient httpClient, long placeId) {
40+
return autoPerformingReq(
41+
httpClient,
42+
format("/api/location/%s/", placeId));
43+
}
44+
45+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.baeldung;
2+
3+
import io.reactivex.Flowable;
4+
import io.vertx.reactivex.core.Vertx;
5+
import io.vertx.reactivex.core.buffer.Buffer;
6+
import io.vertx.reactivex.core.file.FileSystem;
7+
import io.vertx.reactivex.core.http.HttpClient;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import java.time.ZonedDateTime;
15+
16+
import static com.baeldung.MetaWeatherClient.getDataByPlaceId;
17+
import static com.baeldung.MetaWeatherClient.searchByCityName;
18+
19+
public class VertxWithRxJavaTest {
20+
21+
private Vertx vertx;
22+
private HttpClient httpClient;
23+
private FileSystem fileSystem;
24+
static Logger log = LoggerFactory.getLogger(VertxWithRxJavaTest.class);
25+
26+
@Before public void setUp() {
27+
vertx = io.vertx.reactivex.core.Vertx.vertx();
28+
httpClient = vertx.createHttpClient();
29+
fileSystem = vertx.fileSystem();
30+
}
31+
32+
@After public void tearDown() {
33+
vertx.close();
34+
}
35+
36+
@Test public void lightLengthTest() throws InterruptedException {
37+
38+
// read the file that contains one city name per line
39+
fileSystem
40+
.rxReadFile("cities.txt").toFlowable()
41+
.doOnNext(buffer -> log.info("File buffer ---\n{}\n---", buffer))
42+
43+
// split file content in lines to obtain one city per line
44+
.flatMap(buffer -> Flowable.fromArray(buffer.toString().split("\\r?\\n")))
45+
.doOnNext(city -> log.info("City from file: '{}'", city))
46+
47+
// discard cities that are commented out with a leading '#'
48+
.filter(city -> !city.startsWith("#"))
49+
.doOnNext(city -> log.info("City that survived filtering: '{}'", city))
50+
51+
// for each city sends a request to obtain its 'woeid'
52+
// and collect the buffer from the answer
53+
.flatMap(city -> searchByCityName(httpClient, city) )
54+
.flatMap(response -> response.toFlowable())
55+
.doOnNext(buffer -> log.info("JSON of city detail: '{}'", buffer))
56+
57+
// get the woeid of each city
58+
.map(cityBuffer -> cityBuffer
59+
.toJsonArray()
60+
.getJsonObject(0)
61+
.getLong("woeid"))
62+
63+
// use the id to ask for data
64+
.flatMap(cityId -> getDataByPlaceId(httpClient, cityId))
65+
.flatMap(response -> response
66+
.toObservable()
67+
.reduce(
68+
Buffer.buffer(),
69+
(total, newBuf) -> total.appendBuffer( newBuf )).toFlowable() )
70+
71+
// get the JSON object out of the response
72+
.doOnNext(buffer -> log.info("JSON of place detail: '{}'", buffer))
73+
.map(buffer -> buffer.toJsonObject())
74+
75+
// map the JSON in a POJO
76+
.map(json -> {
77+
ZonedDateTime sunRise = ZonedDateTime.parse(json.getString("sun_rise"));
78+
ZonedDateTime sunSet = ZonedDateTime.parse(json.getString("sun_set"));
79+
String cityName = json.getString("title");
80+
return new CityAndDayLength(
81+
cityName,sunSet.toEpochSecond() - sunRise.toEpochSecond());
82+
})
83+
84+
// consume the events
85+
.subscribe(
86+
cityAndDayLength -> System.out.println(cityAndDayLength),
87+
ex -> ex.printStackTrace());
88+
89+
// enough to give time to complete the execution
90+
Thread.sleep(20000);
91+
92+
}
93+
94+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Milan
2+
Chicago
3+
Cairo
4+
Santiago
5+
Moscow
6+
Auckland
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[%thread{32}] %-5level %msg - %logger%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="warn">
9+
<appender-ref ref="STDOUT" />
10+
</root>
11+
12+
</configuration>

0 commit comments

Comments
 (0)