Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
944da42
commit ab4aa73
Showing
7 changed files
with
174 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Reactor/ex5/src/main/java/microservices/airports/AirportListProxyRSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package microservices.airports; | ||
|
||
import datamodels.AirportInfo; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.codec.cbor.Jackson2CborDecoder; | ||
import org.springframework.http.codec.cbor.Jackson2CborEncoder; | ||
import org.springframework.messaging.rsocket.RSocketRequester; | ||
import org.springframework.messaging.rsocket.RSocketStrategies; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Scheduler; | ||
import reactor.util.retry.Retry; | ||
|
||
import java.time.Duration; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* This class serves as a proxy to the asynchronous AirportList | ||
* microservice that uses the RSocket framework to provide a list of | ||
* airport codes and associated airport names. | ||
*/ | ||
public class AirportListProxyRSocket { | ||
/** | ||
* The message name that denotes the remote method to obtain the | ||
* list of airport codes/names asynchronously. | ||
*/ | ||
private final String mFindAirportListMessage = | ||
"_getAirportList"; | ||
|
||
/** | ||
* Initialize the RSocketRequestor. | ||
*/ | ||
private final Mono<RSocketRequester> rSocketRequester = Mono | ||
.just(RSocketRequester.builder() | ||
.rsocketConnector(rSocketConnector -> rSocketConnector | ||
.reconnect(Retry.fixedDelay(2, | ||
Duration.ofSeconds(2)))) | ||
.dataMimeType(MediaType.APPLICATION_CBOR) | ||
.rsocketStrategies(RSocketStrategies.builder() | ||
.encoders(encoders -> | ||
encoders.add(new Jackson2CborEncoder())) | ||
.decoders(decoders -> | ||
decoders.add(new Jackson2CborDecoder())) | ||
.build()) | ||
.tcp("localhost", 8090)); | ||
|
||
/** | ||
* Returns a Flux that emits {@code AirportInfo} objects. | ||
* | ||
* @param scheduler The Scheduler context in which to run the operation | ||
* @return A Flux that emits {@code AirportInfo} objects | ||
*/ | ||
public Flux<AirportInfo> findAirportInfo(Scheduler scheduler) { | ||
return Mono | ||
// Return a Flux containing the list of airport | ||
// information. | ||
.fromCallable(() -> rSocketRequester | ||
// Create the data to send to the server. | ||
.map(r -> r | ||
.route(mFindAirportListMessage)) | ||
|
||
// Get the result back from the server as a | ||
// Flux<AirportInfo>. | ||
.flatMapMany(r -> r.retrieveFlux(AirportInfo.class))) | ||
|
||
// Schedule this to run on the given scheduler. | ||
.subscribeOn(scheduler) | ||
|
||
// De-nest the result so it's a Flux<AirportInfo>. | ||
.flatMapMany(Function.identity()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...tor/ex5/src/main/java/microservices/airports/controller/AirportListControllerRSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package microservices.airports.controller; | ||
|
||
import datamodels.AirportInfo; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
import reactor.core.publisher.Flux; | ||
import utils.DataFactory; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* This Spring controller demonstrates how WebFlux can be used to | ||
* handle RSocket requests via reactive programming. These requests | ||
* are mapped to method(s) that convert between various currencies | ||
* asynchronously. | ||
* | ||
* In Spring's approach to building RSocket services, message | ||
* requests are handled by a controller that defines the | ||
* endpoints/routes for each supported operation, i.e., | ||
* {@code @MessageMapping}. These components are | ||
* identified by the @Controller annotation below. | ||
* | ||
* WebFlux uses the {@code @MessageMapping} annotation to map RSocket | ||
* requests onto methods in the {@code ExchangeRateControllerRSocket}. | ||
*/ | ||
@Controller | ||
public class AirportListControllerRSocket { | ||
/** | ||
* The list of AirportInfo objects. | ||
*/ | ||
private final List<AirportInfo> mAirportList; | ||
|
||
/** | ||
* Constructor initializes the field. | ||
*/ | ||
AirportListControllerRSocket() { | ||
mAirportList = DataFactory | ||
// Initialize the list of AirportInfo objects from the | ||
// AirportList.txt file. | ||
.getAirportInfoList("airport-list.txt"); | ||
} | ||
|
||
/** | ||
* This method finds information about all the airports | ||
* asynchronously. | ||
* | ||
* WebFlux maps RSocket requests sent to the _getAirportList | ||
* endpoint to this method. | ||
* | ||
* @return A Flux that emits all {@code AirportInfo} objects | ||
*/ | ||
@MessageMapping("_getAirportList") | ||
private Flux<AirportInfo> getAirportInfo() { | ||
return Flux | ||
// Convert the list of AirportInfo objects into a Flux | ||
// stream. | ||
.fromIterable(mAirportList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters