Skip to content

Commit 78e47f1

Browse files
authored
RxJava Vertx refactor (eugenp#2531)
* Refactor * Refactor
1 parent d1a1322 commit 78e47f1

5 files changed

Lines changed: 115 additions & 117 deletions

File tree

‎vertx-and-rxjava/pom.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
</parent>
1111

1212
<modelVersion>4.0.0</modelVersion>
13-
<groupId>com.baeldung</groupId>
1413
<artifactId>vertx-and-rxjava</artifactId>
1514

1615
<build>

‎vertx-and-rxjava/src/test/java/com/baeldung/VertxWithRxJavaTest.java‎

Lines changed: 0 additions & 94 deletions
This file was deleted.

vertx-and-rxjava/src/test/java/com/baeldung/CityAndDayLength.java renamed to vertx-and-rxjava/src/test/java/com/baeldung/weather/CityAndDayLength.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.weather;
22

33
import java.text.MessageFormat;
44

@@ -7,12 +7,13 @@ class CityAndDayLength {
77
private final String city;
88
private final double dayLengthInHours;
99

10-
public CityAndDayLength(String city, long dayLengthInSeconds) {
10+
CityAndDayLength(String city, long dayLengthInSeconds) {
1111
this.city = city;
1212
this.dayLengthInHours = dayLengthInSeconds / (60.0 * 60.0);
1313
}
1414

15-
@Override public String toString() {
15+
@Override
16+
public String toString() {
1617
return MessageFormat.format("In {0} there are {1,number,#0.0} hours of light.", city, dayLengthInHours);
1718
}
1819
}

vertx-and-rxjava/src/test/java/com/baeldung/MetaWeatherClient.java renamed to vertx-and-rxjava/src/test/java/com/baeldung/weather/MetaWeatherClient.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.weather;
22

33
import io.reactivex.Flowable;
44
import io.vertx.core.http.RequestOptions;
@@ -8,38 +8,38 @@
88

99
import static java.lang.String.format;
1010

11-
public class MetaWeatherClient {
11+
class MetaWeatherClient {
1212

1313
private static RequestOptions metawether = new RequestOptions()
14-
.setHost("www.metaweather.com")
15-
.setPort(443)
16-
.setSsl(true);
14+
.setHost("www.metaweather.com")
15+
.setPort(443)
16+
.setSsl(true);
1717

1818
/**
1919
* @return A flowable backed by vertx that automatically sends an HTTP request at soon as the first subscription is received.
2020
*/
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-
}
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+
}
2626

27-
static Flowable<HttpClientResponse> searchByCityName(HttpClient httpClient, String cityName) {
28-
HttpClientRequest req = httpClient.get(
29-
new RequestOptions()
27+
static Flowable<HttpClientResponse> searchByCityName(HttpClient httpClient, String cityName) {
28+
HttpClientRequest req = httpClient.get(
29+
new RequestOptions()
3030
.setHost("www.metaweather.com")
3131
.setPort(443)
3232
.setSsl(true)
3333
.setURI(format("/api/location/search/?query=%s", cityName)));
34-
return req
35-
.toFlowable()
36-
.doOnSubscribe(subscription -> req.end());
37-
}
34+
return req
35+
.toFlowable()
36+
.doOnSubscribe(subscription -> req.end());
37+
}
3838

3939
static Flowable<HttpClientResponse> getDataByPlaceId(HttpClient httpClient, long placeId) {
4040
return autoPerformingReq(
41-
httpClient,
42-
format("/api/location/%s/", placeId));
41+
httpClient,
42+
format("/api/location/%s/", placeId));
4343
}
4444

4545
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.weather;
2+
3+
import io.reactivex.Flowable;
4+
import io.reactivex.functions.Function;
5+
import io.vertx.core.json.JsonObject;
6+
import io.vertx.reactivex.core.Vertx;
7+
import io.vertx.reactivex.core.buffer.Buffer;
8+
import io.vertx.reactivex.core.file.FileSystem;
9+
import io.vertx.reactivex.core.http.HttpClient;
10+
import io.vertx.reactivex.core.http.HttpClientResponse;
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.reactivestreams.Publisher;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
import java.time.ZonedDateTime;
19+
20+
import static com.baeldung.weather.MetaWeatherClient.getDataByPlaceId;
21+
import static com.baeldung.weather.MetaWeatherClient.searchByCityName;
22+
23+
public class VertxWithRxJavaTest {
24+
25+
private Vertx vertx;
26+
private HttpClient httpClient;
27+
private FileSystem fileSystem;
28+
private static Logger log = LoggerFactory.getLogger(VertxWithRxJavaTest.class);
29+
30+
@Before
31+
public void setUp() {
32+
vertx = io.vertx.reactivex.core.Vertx.vertx();
33+
httpClient = vertx.createHttpClient();
34+
fileSystem = vertx.fileSystem();
35+
}
36+
37+
@After
38+
public void tearDown() {
39+
vertx.close();
40+
}
41+
42+
@Test
43+
public void shouldDisplayLightLenghts() throws InterruptedException {
44+
45+
// read the file that contains one city name per line
46+
fileSystem
47+
.rxReadFile("cities.txt").toFlowable()
48+
.doOnNext(buffer -> log.info("File buffer ---\n{}\n---", buffer))
49+
.flatMap(buffer -> Flowable.fromArray(buffer.toString().split("\\r?\\n")))
50+
.doOnNext(city -> log.info("City from file: '{}'", city))
51+
.filter(city -> !city.startsWith("#"))
52+
.doOnNext(city -> log.info("City that survived filtering: '{}'", city))
53+
.flatMap(city -> searchByCityName(httpClient, city))
54+
.flatMap(HttpClientResponse::toFlowable)
55+
.doOnNext(buffer -> log.info("JSON of city detail: '{}'", buffer))
56+
.map(extractingWoeid())
57+
.flatMap(cityId -> getDataByPlaceId(httpClient, cityId))
58+
.flatMap(toBufferFlowable())
59+
.doOnNext(buffer -> log.info("JSON of place detail: '{}'", buffer))
60+
.map(Buffer::toJsonObject)
61+
.map(toCityAndDayLength())
62+
.subscribe(System.out::println, Throwable::printStackTrace);
63+
64+
Thread.sleep(20000); // enough to give time to complete the execution
65+
}
66+
67+
private static Function<HttpClientResponse, Publisher<? extends Buffer>> toBufferFlowable() {
68+
return response -> response
69+
.toObservable()
70+
.reduce(
71+
Buffer.buffer(),
72+
Buffer::appendBuffer).toFlowable();
73+
}
74+
75+
private static Function<Buffer, Long> extractingWoeid() {
76+
return cityBuffer -> cityBuffer
77+
.toJsonArray()
78+
.getJsonObject(0)
79+
.getLong("woeid");
80+
}
81+
82+
private static Function<JsonObject, CityAndDayLength> toCityAndDayLength() {
83+
return json -> {
84+
ZonedDateTime sunRise = ZonedDateTime.parse(json.getString("sun_rise"));
85+
ZonedDateTime sunSet = ZonedDateTime.parse(json.getString("sun_set"));
86+
String cityName = json.getString("title");
87+
return new CityAndDayLength(
88+
cityName, sunSet.toEpochSecond() - sunRise.toEpochSecond());
89+
};
90+
}
91+
92+
}

0 commit comments

Comments
 (0)