You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

 

NIO Extension

Introduction

Servlet 3.1 API introduces a support for Non-Blocking IO, see this tutorial for more information.

The idea is that when the service code reads or writes the stream it does not block at all and only does a read or write action when a servlet container is ready to handle it effectively.

Early JAX-RS 2.1 API had a server-side prototype to help the JAX-RS service code utilize this Servlet 3.1 NIO features in a JAX-RS friendly way. Unfortunately that prototype was dropped from the final 2.1 API with the future major JAX-RS version expected to provide a much more complete and sophisticated NIO API.

CXF 3.2.0 has retained the implementation of the original JAX-RS 2.1 NIO API prototype and made it possible for the users to experiment with it.

NIO Reader

 

NIO Writer

@GET
@Produces(MediaType.TEXT_PLAIN)
    public Response getBookStream() throws IOException {
        final ByteArrayInputStream in = new ByteArrayInputStream(
            IOUtils.readBytesFromStream(getClass().getResourceAsStream("/files/books.txt")));

        final byte[] buffer = new byte[4096];

        return Response.ok().entity(

                new NioWriteEntity(
        
                   out -> {

                    final int n = in.read(buffer);
                    if (n >= 0) {
                        out.write(buffer, 0, n);
                        return true;
                    }
                    closeInputStream(in);
                    return false;
                }
                // by default the runtime will throw the exception itself
                // if the error handler is not provided
                //,
                //throwable -> {
                //    throw throwable;
                //}
            ))
            .build();
    }

  • No labels