Java Magazine, Sept/Oct 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 43 enterprise java event is being sent you need to manually take care of threading Therefore you start a new thread that waits for the events and thus the execution in the start method can continue Consumers of the StatefulSseClient class can call start and stop to resume and pause the stream and all events that happen during the pause are replayed after resuming Reactive Programming On the client side another interesting new feature will be the integration of reactive computation types such as CompletionStage into the JAX RS client As an example Ill call several web services and combine the results with functionality that is available in JAX RS 20 already Suppose I have a slow HTTP endpoint returning a randomly created response after a while and suppose I want to consume this service several times and calculate a result from all responses Polling the services sequentially and then constructing the result would lead to long wait times while the threads are blocked To avoid that I can use constructs such as CompletableFutures together with Java 8 lambdas and streams Heres a JAX RS 20 client side example final Executor executor Executors newCachedThreadPool endpoint takes 2 seconds to respond with data like random 1 final OptionalDouble average IntStream range 0 10 parallel map i CompletableFuture supplyAsync target request MediaType APPLICATION_ JSON_ TYPE get JsonObject class executor thenApply o o getInt random join average System out println average average getAsDouble Calling the endpoint via the target results in a 2 second wait time To avoid this I wrap the action in a Completable Future and extract the total value from the JSON data once the result is available The executor is provided as a parameter so you can manage the threads for the 10 concurrent polls while most of the calling threads are waiting The execution blocks when join is called but because Im running the IntStream in parallel all the results are available at roughly the same time after about 2 seconds rather than after 20 seconds as they would be when polling one after the other The stream of results is then aggregated into the average value This is what is possible today using JAX RS 20 and CompletableFuture to wrap long running jobs JAX RS 21 will integrate reactive invokers for the client side Constructing invocations is enhanced with rx methods that wrap responses into a reactive invocation type and optionally use a specific ExecutorService Heres an example final ExecutorService executor Executors newCachedThreadPool final OptionalDouble average IntStream range 0 10 parallel map i target request MediaType APPLICATION_ JSON_ TYPE
You must have JavaScript enabled to view digital editions.