Asynchronous I/Ofor Servlet 3.0 Jeanfrancois Arcand Senior Staff Engineer Sun Microsystems
Goal                                          JGD• Bring Asynchronous I/O to Servlet without  exposing low level I/O detai...
ServletFuture                                 JGD• A Future representing the result of an  asynchronous I/O operation.• In...
ServletIoFuture                                  JGD boolean cancel(boolean mayInterruptIfRunning) A attachment() A attach...
ServletAsyncHandler                          JGD• A handler for consuming the result of an  asynchronous I/O operation.• T...
ServletAsyncHandler                            JGD void completed(ServletIoFuture<T,A> result)                            ...
ServletRequestChannel                          JGD• An asynchronous channel for read  stream-oriented operations.• Allow s...
ServletRequestChannel                        JGD <T,A> ReadFuture<T,A> read         (java.lang.Class<T> clazz,          A ...
Example 1: Asynchronous Read                                                    JGD// Asynchronous Read// The request is a...
Example 2: Synchronous Read                                           JGD// Synchronous ReadServletRequestChannel channel ...
Example 3: Suspendable Request                                                 JGD// Asynchronous ReadservletRequest.suspe...
ServletResponseChannel                 JGD• An asynchronous channel for write  stream-oriented operations.• Allow synchron...
ServletResponseChannel                                  JGD <A> WriteFuture<Integer> write            (Object object,     ...
Example 4: Asynchronous Write                                                   JGD// Asynchronous Write// The request is ...
Example 5: Asynchronous Write                                              JGD// Asynchronous WriteServletResponseChannel ...
Example 6: Synchronous Write                                     JGD// Asynchronous WriteServletResponseChannel channel = ...
Example 2: Suspendable Request                                                         JGD// Asynchronous ReadservletReque...
Summary                               JGD• Simple API.• No buffering of data• API can be used synchronously and  asynchron...
jeanfrancois.arcand@sun.com
Upcoming SlideShare
Loading in …5
×

Servlet Async I/O Proposal (NIO.2)

2,514 views

Published on

An Async I/O proposal for Servlet 3.x spec.

Published in: Technology, Health & Medicine
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,514
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
16
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Servlet Async I/O Proposal (NIO.2)

  1. 1. Asynchronous I/Ofor Servlet 3.0 Jeanfrancois Arcand Senior Staff Engineer Sun Microsystems
  2. 2. Goal JGD• Bring Asynchronous I/O to Servlet without exposing low level I/O details• New classes: > ServletFuture > ServletAsyncHandler > ServletRequestChannel > ServletResponseChannel > ReadFuture > WriteFuture 2
  3. 3. ServletFuture JGD• A Future representing the result of an asynchronous I/O operation.• In addition to the methods defined by the Future interface, a ServletFuture allows for the attachment of a single arbitrary object.• The object can be used to associate application-specific data or context required when consuming the result of the I/O operation.• This is important for cases where the same ServletAsyncHandler is used to consume the result of several I/O operations. 3
  4. 4. ServletIoFuture JGD boolean cancel(boolean mayInterruptIfRunning) A attachment() A attach(A attachment) 4
  5. 5. ServletAsyncHandler JGD• A handler for consuming the result of an asynchronous I/O operation.• The asynchronous ServletRequestChannel.read or ServletResponseChannel.write defined in this package allow a ServletAsyncHandler to be specified yo consume the result of an asynchronous operation.• When an operation completes the handlers completed method is invoked with the result. 5
  6. 6. ServletAsyncHandler JGD void completed(ServletIoFuture<T,A> result) 6
  7. 7. ServletRequestChannel JGD• An asynchronous channel for read stream-oriented operations.• Allow synchronous and asynchronous read operations• From a Class definition, the read operation will convert the request body into an instance of that Class definition. 7
  8. 8. ServletRequestChannel JGD <T,A> ReadFuture<T,A> read (java.lang.Class<T> clazz, A attachment, ServletAsyncHandler<? super T,A> handler) <T,A> ReadFuture<T,A> read (java.lang.Class<T> clazz, long timeout, TimeUnit unit, A attachment, ServletAsyncHandler<? super T,A> handler) 8
  9. 9. Example 1: Asynchronous Read JGD// Asynchronous Read// The request is automatically suspended when the getChannel() is invoked.// See Example 3 for a scenario when the request is not explicitly suspended.ServletRequestChannel channel = servletRequest.getChannel();ReadFuture ioFuture = channel.read(Map.class,30,TimeUnits.SECONDS, null, new ServletAsyncHandler<ReadFuture<Map,Void>){ public void completed(ReadFuture<Map,Void> result){ Map map = result.getContent(); } }); 9
  10. 10. Example 2: Synchronous Read JGD// Synchronous ReadServletRequestChannel channel = servletRequest.getChannel();ReadFuture ioFuture = channel.read(Map.class);// Or ReadFuture ioFuture = channel.read(Map.class,null,null);ioFuture.get(30,TimeUnits.SECONDS);Map map = ioFuture.getContent() ; 10
  11. 11. Example 3: Suspendable Request JGD// Asynchronous ReadservletRequest.suspend();ServletRequestChannel channel = servletRequest.getChannel();ReadFuture ioFuture = channel.read( Map.class,30,TimeUnits.SECONDS, servletRequest, new ServletAsyncHandler<ReadFuture<Map,ServletRequest>){ public void completed(ReadFuture<Map,ServletRequest> result){ Map map = result.getContent(); result.attachment().resume(); } }); 11
  12. 12. ServletResponseChannel JGD• An asynchronous channel for write stream-oriented operations.• Allow synchronous and asynchronous write operations 12
  13. 13. ServletResponseChannel JGD <A> WriteFuture<Integer> write (Object object, A attachment, ServletAsyncHandler<Integer,A> handler) <A> WriteFuture<Integer> write (Object object, long timeout, TimeUnit unit, A attachment, ServletAsyncHandler<Integer,A> handler) 13
  14. 14. Example 4: Asynchronous Write JGD// Asynchronous Write// The request is automatically suspended when the getChannel() is invoked.// See Example 6 for a scenario when the request is not explicitly suspended.ServletResponseChannel channel = servletResponse.getChannel();WriteFuture ioFuture = channel.write(“Hello”,30,TimeUnits.SECONDS, null, new ServletAsyncHandler<WriteFuture<Integer,Void>){ public void completed(WriteFuture<Integer,Void> result){ System.out.println(“Bytes Written: “ + result.get()); } }); 14
  15. 15. Example 5: Asynchronous Write JGD// Asynchronous WriteServletResponseChannel channel = servletResponse.getChannel();WriteFuture ioFuture = channel.write(“Hello”,30,TimeUnits.SECONDS, null, new ServletAsyncHandler<WriteFuture<Integer,Void>){ public void completed(WriteFuture<Integer,Void> result){ System.out.println(“Bytes Written: “ + result.get()); } }); 15
  16. 16. Example 6: Synchronous Write JGD// Asynchronous WriteServletResponseChannel channel = servletResponse.getChannel();WriteIoFuture ioFuture = channel.write(“Hello”);ioFuture.get(30,TimeUnits.SECONDS); 16
  17. 17. Example 2: Suspendable Request JGD// Asynchronous ReadservletRequest.suspend();ServletResponseChannel channel = servletResponse.getChannel();WriteIoFuture ioFuture = channel.write( “Hello”,30,TimeUnits.SECONDS, servletRequest, new ServletAsynchronousIoHandler<WriteIoFuture<Integer,ServletRequest>){ public void completed(WriteIoFuture<Integer,ServletRequest> result){ result.attachment().resume(); } }); 17
  18. 18. Summary JGD• Simple API.• No buffering of data• API can be used synchronously and asynchronously.• Easy to implement. 18
  19. 19. jeanfrancois.arcand@sun.com

×