Java Magazine, Jan/Feb 2018
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 29 reactive programming The reactive implementation might Reactive programming is more than enhancing look more complicated at first glance the implementation from synchronous to asynchronous but after closer examination you will it also eases development with concepts such as see that its fairly straightforward nesting stages Within the ForecastReactiveResource implementation I first create a client invocation on the location services with the help of the JAX RS Reactive Client API As I mentioned previously this is an addition to Java EE 8 and it helps to create a reactive invoker simply by use of the rx method Now I compose another stage based on location to collect the list of forecasts They will be stored in one big completion stage named forecastCS as a list of forecasts I will ultimately create the response of the service call by using only forecastCS Lets continue by collecting the forecasts as a list of completion stages as defined in the forecastList variable To create the completion stages for each forecast I stream on the locations and then create the tempCS variable by again using the JAX RS Reactive Client API which will invoke the temperature service with city name I use the resolveTemplate method here to build a client and that enables me to pass the name of the city to the builder as a parameter As a last step of streaming on locations I do a call to CompletableFuture completedFuture by providing a newly created instance of Forecast as the parameter I combine this future with the tempCS stage so that I have the temperature value for the iterated locations The CompletableFuture allOf method in Listing 11 transforms the list of completion stages to forecastCS Execution of this step returns the big completable future instance when all provided completable futures are completed The response from the service is an instance of the ServiceResponse class so I create a completed future for that as well and then I combine the forecastCS completion stage with the list of forecasts and calculate the response time of the service Of course this reactive programming makes only the server side execute asynchronously the client side will be blocked until the server sends the response back to the requester In order to overcome this problem Server Sent Events SSEs can also be used to partially send
You must have JavaScript enabled to view digital editions.