Java Magazine, Sept/Oct 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 44 enterprise java rx executor get JsonObject class thenApply o o getInt total toCompletableFuture join average System out println average average getAsDouble Calling the rx method configures the builder to use an RxInvoker and the CompletionStage is used as the default invocation type Therefore all subsequent actions such as get return the responses wrapped in this corresponding reactive type As in the previous example I chain several actions together and CompletionStage toCompletableFuture enables me to call join as well This is one of the many useful improvements in the next release of JAX RS Non Blocking I O Another extension to both the client and the server side will be support for non blocking I O NIO By using NIO the caller is guaranteed to be able to call certain methods for either sending data without being blocked or registering a reader that will eventually be called when data is available Server side NIO The JAX RS server side programming model will be enhanced so that developers can register NioReaderHandlers for responses or NioWriterHandlers for requests respectively These callback handlers are called when data needs to be read or written without blocking The following examples show how to read and write data in a non blocking way in a JAX RS resource @ GET @ Produces MediaType APPLICATION_ OCTET_ STREAM public Response download final InputStream in final byte buffer new byte 1000 return Response ok entity out try final int length in read buffer if length 0 out write buffer 0 length return true in close return false catch IOException e throw new WebApplicationException e build The Response ok entity method takes an NioWriterHandler as an argument and thus the lambda implementing the NioWriterHandler write NioOutputStream method is called each time out is ready to accept data The handler method is expected to return a boolean value indicating whether there is more data to write A similar handler approach is realized for reading uploaded data from the clients request @ POST @ Consumes MediaType APPLICATION_ OCTET_ STREAM public void upload@ Context Request request final ByteArrayOutputStream out final byte buffer new byte 1000 request entity in
You must have JavaScript enabled to view digital editions.