SlideShare uma empresa Scribd logo
1 de 46
Think Async
Bhakti Mehta
Twitter: @bhakti_mehta
Copyright 2013, Bhakti Mehta all rights reserved
Introduction
• Currently working as a Senior Software Engineer
at Blue Jeans Network. Bluejeans.com
• Worked at Sun Microsystems/Oracle for 13 years
• Committer to numerous open source projects
including GlassFish
Copyright 2013, Bhakti Mehta all rights reserved
My upcoming book
Copyright 2013, Bhakti Mehta all rights reserved
What you will learn
• Start thinking asynchronously
• Cover different technologies with respect to async
support
• Cover concepts from J2SE and J2EE with respect
to asynchronous processing
Copyright 2013, Bhakti Mehta all rights reserved
Why Think Async?
• Increase speed of processing through
parallelization
• Involve multiple CPU cores for processing than
single fast CPU
• Compare with older request response models and
see the benefits
Copyright 2013, Bhakti Mehta all rights reserved
Where async can be
used?
• Long running operations
• Processor intensive tasks
• Background tasks
• To improve application throughput
• To improve application response time
Copyright 2013, Bhakti Mehta all rights reserved
Usecases
• User John Doe wants to download multiple files
from different locations.
Solution
• Execute requests asynchronously and wait for
results from Future object
Copyright 2013, Bhakti Mehta all rights reserved
Usecases
• Use John Doe has a large data to post to a URL
Solution
Use callbacks instead of waiting and blocking for
response
Copyright 2013, Bhakti Mehta all rights reserved
Usecases
• Use John Doe want to upload a large file
Solution
Use AsynchronousFileChannel from JDK 7
Copyright 2013, Bhakti Mehta all rights reserved
Polling
• Polling
 Used by vast majority of AJAX applications
 Poll the server for data
 Client --->request--> Server
 If no data empty response is returned
 Not a true asynchronous mechanism
Copyright 2013, Bhakti Mehta all rights reserved
Polling
Copyright 2013, Bhakti Mehta all rights reserved
Request (client_id)
Response 200 OK: empty
Request (client_id)
….
….
….
….
Response 200 OK: message
body
Polling Drawbacks
 Http overhead
 Reducing the interval will consume more
bandwidth and processing resources for nothing.
Copyright 2013, Bhakti Mehta all rights reserved
Long Polling
 If server does not have data holds request open
 COMET
 Chunked transfer encoding can be used to send
chunks of data as part of response body which is
opened as a stream.
 Chunks can be javascript tags loaded in hidden
iframe and executed in order of arrival
Copyright 2013, Bhakti Mehta all rights reserved
Long Polling
Copyright 2013, Bhakti Mehta all rights reserved
Request (client_id)
Response 200 OK: message
body
Request (client_id)
….
….
….
Response 200 OK: message
body
….
….
Long Polling Drawbacks
• No standard data format or message format when
used in chunked transfer encoding mode
• Each connection initiation has the initiation cost.
• No caching between clients and server, which
impacts the server performance instead of reading
some contents from the cache.
Copyright 2013, Bhakti Mehta all rights reserved
Server-sent Events
 Unidirectional channel between server and client
 Server pushes data to your app when it wants
asynchronously
 Updates can be streamed from server to client as
they happen
Copyright 2013, Bhakti Mehta all rights reserved
Server-sent Events
Copyright 2013, Bhakti Mehta all rights reserved
Request (client_id)
….
….
….
SSE message
….
….
SSE message
WebSockets
 Next generation of asynchronous communication
from client to server
 Full duplex communication in either direction
 After initial handshake on http communication is
over TCP socket using ws(unsecure) or
wss(secure) protocol
Copyright 2013, Bhakti Mehta all rights reserved
WebSocket Handshake
Client and Server upgrade from Http protocol to WebSocket protocol during initial handshake
GET /text HTTP/1.1rn
Upgrade: WebSocketrn
Connection: Upgradern
Host: www.websocket.orgrn …rn
Handshake from server looks like
HTTP/1.1 101 WebSocket Protocol Handshakern
Upgrade: WebSocketrn
Connection: Upgradern …rn
Copyright 2013, Bhakti Mehta all rights reserved
WebSockets
 After the upgrade HTTP is completely out of the
picture at this point.
 Using the lightweight WebSocket wire protocol,
messages can now be sent or received by either
endpoint at any time.
 Creating a Websocket
 ws = new WebSocket("ws://localhost:8080/../WebSocketChat");
 You can set handlers for events onopen or onerror
Copyright 2013, Bhakti Mehta all rights reserved
Other JavaEE technologies
Async support
• Servlet 3.0
• JAX-RS
• EJB
Copyright 2013, Bhakti Mehta all rights reserved
Components ofAsyncServlet
• New attribute asyncSupported on @WebServlet annotation
or in web.xml
• AsyncContext
• Thread Pool
• Runnable instance
Copyright 2013, Bhakti Mehta all rights reserved
AsyncServlet
• When asyncSupported attribute is set to true
response is not committed on method exit
• Calling startAsync() method of AsyncContext
caches the request response pair
• The method returns and original thread is recycled
• AsyncListener and AsyncEvent give developers
control of the asynchronous lifecycle events
Copyright 2013, Bhakti Mehta all rights reserved
AsyncServlet
final AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(600000);
asyncContext.addListener(new AsyncListener()
Implement the following method of the AsyncListener
onComplete(), onError(),OnTimeout(), onstartAsync()
Copyright 2013, Bhakti Mehta all rights reserved
Servlet 3.1:Non blocking IO
• ReadListener
• WriteListener
• These are then registered using
ServletInputStream.setReadListener and
ServletOutputStream.setWriteListener. The
listeners have callback methods that are invoked
when the content is available to be read or can be
written without blocking.
Copyright 2013, Bhakti Mehta all rights reserved
Servlet 3.1: Non blocking IO
public interface ReadListener extends EventListener {
public void onDataAvailable(ServletRequest request);
public void onAllDataRead(ServletRequest request);
public void onError(Throwable t);
}
Copyright 2013, Bhakti Mehta all rights reserved
Servlet 3.0: Non blocking IO
• The onDataAvailable: Invoked when all data for
the current request has been read.
• The onAllDataRead: Method is invoked by the
container the first time when it is possible to read
data.
• The onError: Invoked when an error occurs
processing the request
Copyright 2013, Bhakti Mehta all rights reserved
Servlet 3.0: Non blocking IO
public interface WriteListener extends EventListener {
public void onWritePossible(ServletResponse response);
public void onError(Throwable t);
}
Copyright 2013, Bhakti Mehta all rights reserved
Servlet 3.0: Non blocking IO
• The container invokes the onWritePossible
method when it is possible to write in the Servlet’s
OutputStream.
• The onError is invoked when writing in the
Servlet’s OutputStream encounters an exception.
Copyright 2013, Bhakti Mehta all rights reserved
ServletOutputStream
• In the ServletOutputStream interface
• isReady: This method can be used to determine
if data can be written without blocking.
• setWriteListener: Instructs the
ServletOutputStream to invoke the provided
WriteListener when it is possible to write
Copyright 2013, Bhakti Mehta all rights reserved
ServletInputStream
• In the ServletInputStream interface
• isFinished: Returns true when all the data from
the stream has been read else it returns false.
• isReady: Returns true if data can be read
without blocking else returns false.
• setReadListener: Instructs the
ServletInputStream to invoke the provided
ReadListener when it is possible to read
Copyright 2013, Bhakti Mehta all rights reserved
JAX-RS 2.0 Async
support
• Server Side
• Client Side
Copyright 2013, Bhakti Mehta all rights reserved
JAX-RS 2.0 Server
sideAsync support
• AsyncResponse: An injectable JAX-RS
asynchronous response that provides means for
asynchronous server side response processing.
• @Suspended: The @Suspended instructs the
container that the HTTP request processing should
happen in a secondary thread.
• CompletionCallback: A request processing callback
that receives request processing completion events.
Has onComplete() callback
• ConnectionCallback: Asynchronous request
processing lifecycle callback that receives
connection related asynchronous response lifecycle
events.
Copyright 2013, Bhakti Mehta all rights reserved
JAX-RS 2.0 Server
sideAsync support
• InvocationCallback: Callback that can be
implemented to receive the asynchronous
processing events from the invocation
processing.
• Future: Allows the client to poll for completion of
the asynchronous operation or to block and wait
for it.
Copyright 2013, Bhakti Mehta all rights reserved
@Suspended
@Path(“/books/borrow”)
@Stateless
public class BookResource {
@GET @Produce(“text/plain”)
@Asynchronous
public void borrow(@Suspended AsyncResponse ar) { final String result = prepareResponse();
ar.resume(result) }
}
Copyright 2013, Bhakti Mehta all rights reserved
Client side code
Future<Book> future = client.target("(“books/borrow/borrow")
.request()
.async()
.get(Book.class);
try {
Book book = future.get(30, TimeUnit.SECONDS);
} catch (TimeoutException ex) {
System.err.println("Timeout occurred");
}
Copyright 2013, Bhakti Mehta all rights reserved
InvocationCallback
sample
client.target(restUrl)
.request(MediaType.APPLICATION_JSON)
.async()
.post(Entity.entity("Here is some text", MediaType.TEXT_PLAIN),
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
processResponse(response.readEntity(JSONObject.class));
}
Copyright 2013, Bhakti Mehta all rights reserved
EJB 3.1
• @Asynchronous annotation introduced in EJB 3.1
• Methods annotated with @Asynchronous will
return immediately regardless of how long the
method usually takes.
• Each invocation returns a Future object which is
initially empty and will have its value filled later
when method completes
Copyright 2013, Bhakti Mehta all rights reserved
AsyncResult
• AsyncResult<V> wraps the result of an
asynchronous method call as a Future object
• The value specified in the constructor
AsyncResult(V result) will be retrieved by
Container and sent to client.
Copyright 2013, Bhakti Mehta all rights reserved
Cancelling an async
invocation
• The session bean can check whether the client
requested that the invocation be cancelled by
calling
the javax.ejb.SessionContext.wasCancelled().
Copyright 2013, Bhakti Mehta all rights reserved
@Asynchronous code
snippet@Path(“/books/borrow”)
@Stateless
public class BookResource {
@Context private ExecutionContext ctx;
@GET @Produce(“application/json”)
@Asynchronous
public void borrow() {
Executors.newSingleThreadExecutor().submit( new Runnable() {
public void run() {
Thread.sleep(10000);
ctx.resume(“Hello async world!”);
} });
ctx.suspend();
return; }} Copyright 2013, Bhakti Mehta all rights reserved
Java SE and Think Async
• Future
• A Future object represents the result of
asynchronous completion.
• Methods to check if computation is complete, wait
for result and retrieve results.
• Cancellation can be performed by cancel method
Copyright 2013, Bhakti Mehta all rights reserved
Asynchronous IO
• AsynchronousFileChannel
• Reads and writes contents from a file
asynchronously
Copyright 2013, Bhakti Mehta all rights
reserved
AsynchronousFileChannel
try (AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path)){
//Returns a Future instance which can be used to read the contents of the
file.
Future<Integer> fileResult = asyncChannel.read(buffer, 0);
while(!fileResult.isDone()){
System.out.println("Waiting to complete the file reading ...");
}
Copyright 2013, Bhakti Mehta all rights reserved
Call for action
• Download GlassFish 4.0 from
glassfish.dev.java.net for latest JavaEE7 API
• File issues, contribute patches
• Email users@glassfish.dev.java.net
Copyright 2013, Bhakti Mehta all rights reserved
Questions
• Twitter: @bhakti_mehta
• Blog https://www.java.net//blogs/bhaktimehta
• LinkedIn: http://www.linkedin.com/in/bhaktihmehta
• Slideshttp://www.slideshare.net/bhaktiks/think-async
Copyright 2013, Bhakti Mehta all rights reserved

Mais conteúdo relacionado

Mais procurados

HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 Conference
Roger Kitain
 
Implementing Advanced Caching and Replication Techniques in ...
Implementing Advanced Caching and Replication Techniques in ...Implementing Advanced Caching and Replication Techniques in ...
Implementing Advanced Caching and Replication Techniques in ...
webhostingguy
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
vamsi krishna
 
Introduction to the World Wide Web
Introduction to the World Wide WebIntroduction to the World Wide Web
Introduction to the World Wide Web
Abdalla Mahmoud
 

Mais procurados (19)

Getting started with Websocket and Server-sent Events using Java - Arun Gupta
Getting started with Websocket and Server-sent Events using Java - Arun Gupta Getting started with Websocket and Server-sent Events using Java - Arun Gupta
Getting started with Websocket and Server-sent Events using Java - Arun Gupta
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
Ftp servlet
Ftp servletFtp servlet
Ftp servlet
 
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Getting Started with WebSocket and Server-Sent Events using Java by Arun GuptaGetting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developers
 
Web Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedWeb Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting Started
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 Conference
 
The never-ending REST API design debate -- Devoxx France 2016
The never-ending REST API design debate -- Devoxx France 2016The never-ending REST API design debate -- Devoxx France 2016
The never-ending REST API design debate -- Devoxx France 2016
 
Implementing Advanced Caching and Replication Techniques in ...
Implementing Advanced Caching and Replication Techniques in ...Implementing Advanced Caching and Replication Techniques in ...
Implementing Advanced Caching and Replication Techniques in ...
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServices
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
Introduction to the World Wide Web
Introduction to the World Wide WebIntroduction to the World Wide Web
Introduction to the World Wide Web
 
Great webapis
Great webapisGreat webapis
Great webapis
 
Servlet
ServletServlet
Servlet
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
21 HTTP Protocol #burningkeyboards
21 HTTP Protocol #burningkeyboards21 HTTP Protocol #burningkeyboards
21 HTTP Protocol #burningkeyboards
 
.NET Core, ASP.NET Core Course, Session 19
 .NET Core, ASP.NET Core Course, Session 19 .NET Core, ASP.NET Core Course, Session 19
.NET Core, ASP.NET Core Course, Session 19
 
EAI design patterns/best practices
EAI design patterns/best practicesEAI design patterns/best practices
EAI design patterns/best practices
 

Destaque

Con fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhaktiCon fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhakti
Bhakti Mehta
 

Destaque (16)

Servlet Async I/O Proposal (NIO.2)
Servlet Async I/O Proposal (NIO.2)Servlet Async I/O Proposal (NIO.2)
Servlet Async I/O Proposal (NIO.2)
 
Asynchronous Processing in Java/JEE/Spring
Asynchronous Processing in Java/JEE/SpringAsynchronous Processing in Java/JEE/Spring
Asynchronous Processing in Java/JEE/Spring
 
Expect the unexpected: Anticipate and prepare for failures in microservices b...
Expect the unexpected: Anticipate and prepare for failures in microservices b...Expect the unexpected: Anticipate and prepare for failures in microservices b...
Expect the unexpected: Anticipate and prepare for failures in microservices b...
 
Resilience planning and how the empire strikes back
Resilience planning and how the empire strikes backResilience planning and how the empire strikes back
Resilience planning and how the empire strikes back
 
Con fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhaktiCon fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhakti
 
50 tips50minutes
50 tips50minutes50 tips50minutes
50 tips50minutes
 
Architecting for Failures in micro services: patterns and lessons learned
Architecting for Failures in micro services: patterns and lessons learnedArchitecting for Failures in micro services: patterns and lessons learned
Architecting for Failures in micro services: patterns and lessons learned
 
Expect the unexpected: Prepare for failures in microservices
Expect the unexpected: Prepare for failures in microservicesExpect the unexpected: Prepare for failures in microservices
Expect the unexpected: Prepare for failures in microservices
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and Java
 
Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using WebsocketsBuilding Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
 
Let if flow: Java 8 Streams puzzles and more
Let if flow: Java 8 Streams puzzles and moreLet if flow: Java 8 Streams puzzles and more
Let if flow: Java 8 Streams puzzles and more
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Akka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutesAkka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutes
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3
 
Devoxx2017
Devoxx2017Devoxx2017
Devoxx2017
 

Semelhante a Think async

Interactive web. O rly?
Interactive web. O rly?Interactive web. O rly?
Interactive web. O rly?
timbc
 
web-servers3952 (1)qwjelkjqwlkjkqlwe.ppt
web-servers3952 (1)qwjelkjqwlkjkqlwe.pptweb-servers3952 (1)qwjelkjqwlkjkqlwe.ppt
web-servers3952 (1)qwjelkjqwlkjkqlwe.ppt
20521742
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
Raghunathan52
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
Raghunathan52
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Kavita Sharma
 
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmenMCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
VannaSchrader3
 
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docxMCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
alfredacavx97
 

Semelhante a Think async (20)

Fight empire-html5
Fight empire-html5Fight empire-html5
Fight empire-html5
 
WebSockets - Realtime em Mundo Conectado
WebSockets - Realtime em Mundo ConectadoWebSockets - Realtime em Mundo Conectado
WebSockets - Realtime em Mundo Conectado
 
5-WebServers.ppt
5-WebServers.ppt5-WebServers.ppt
5-WebServers.ppt
 
Interactive web. O rly?
Interactive web. O rly?Interactive web. O rly?
Interactive web. O rly?
 
Deploy secure, scalable, and highly available web apps with Azure Front Door ...
Deploy secure, scalable, and highly available web apps with Azure Front Door ...Deploy secure, scalable, and highly available web apps with Azure Front Door ...
Deploy secure, scalable, and highly available web apps with Azure Front Door ...
 
web-servers3952 (1)qwjelkjqwlkjkqlwe.ppt
web-servers3952 (1)qwjelkjqwlkjkqlwe.pptweb-servers3952 (1)qwjelkjqwlkjkqlwe.ppt
web-servers3952 (1)qwjelkjqwlkjkqlwe.ppt
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
 
Web Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdfWeb Technologies Notes - TutorialsDuniya.pdf
Web Technologies Notes - TutorialsDuniya.pdf
 
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long PollingDifference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
J2ee servlet
J2ee servletJ2ee servlet
J2ee servlet
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Reverse ajax in 2014
Reverse ajax in 2014Reverse ajax in 2014
Reverse ajax in 2014
 
Http_Protocol.pptx
Http_Protocol.pptxHttp_Protocol.pptx
Http_Protocol.pptx
 
Cookie
CookieCookie
Cookie
 
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmenMCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen
 
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docxMCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
MCIS 6163 Assignment 1MCIS 6163 Assignment 1.pdfAssignmen.docx
 
Web servers
Web serversWeb servers
Web servers
 
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at ScaleJUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 

Think async

  • 1. Think Async Bhakti Mehta Twitter: @bhakti_mehta Copyright 2013, Bhakti Mehta all rights reserved
  • 2. Introduction • Currently working as a Senior Software Engineer at Blue Jeans Network. Bluejeans.com • Worked at Sun Microsystems/Oracle for 13 years • Committer to numerous open source projects including GlassFish Copyright 2013, Bhakti Mehta all rights reserved
  • 3. My upcoming book Copyright 2013, Bhakti Mehta all rights reserved
  • 4. What you will learn • Start thinking asynchronously • Cover different technologies with respect to async support • Cover concepts from J2SE and J2EE with respect to asynchronous processing Copyright 2013, Bhakti Mehta all rights reserved
  • 5. Why Think Async? • Increase speed of processing through parallelization • Involve multiple CPU cores for processing than single fast CPU • Compare with older request response models and see the benefits Copyright 2013, Bhakti Mehta all rights reserved
  • 6. Where async can be used? • Long running operations • Processor intensive tasks • Background tasks • To improve application throughput • To improve application response time Copyright 2013, Bhakti Mehta all rights reserved
  • 7. Usecases • User John Doe wants to download multiple files from different locations. Solution • Execute requests asynchronously and wait for results from Future object Copyright 2013, Bhakti Mehta all rights reserved
  • 8. Usecases • Use John Doe has a large data to post to a URL Solution Use callbacks instead of waiting and blocking for response Copyright 2013, Bhakti Mehta all rights reserved
  • 9. Usecases • Use John Doe want to upload a large file Solution Use AsynchronousFileChannel from JDK 7 Copyright 2013, Bhakti Mehta all rights reserved
  • 10. Polling • Polling  Used by vast majority of AJAX applications  Poll the server for data  Client --->request--> Server  If no data empty response is returned  Not a true asynchronous mechanism Copyright 2013, Bhakti Mehta all rights reserved
  • 11. Polling Copyright 2013, Bhakti Mehta all rights reserved Request (client_id) Response 200 OK: empty Request (client_id) …. …. …. …. Response 200 OK: message body
  • 12. Polling Drawbacks  Http overhead  Reducing the interval will consume more bandwidth and processing resources for nothing. Copyright 2013, Bhakti Mehta all rights reserved
  • 13. Long Polling  If server does not have data holds request open  COMET  Chunked transfer encoding can be used to send chunks of data as part of response body which is opened as a stream.  Chunks can be javascript tags loaded in hidden iframe and executed in order of arrival Copyright 2013, Bhakti Mehta all rights reserved
  • 14. Long Polling Copyright 2013, Bhakti Mehta all rights reserved Request (client_id) Response 200 OK: message body Request (client_id) …. …. …. Response 200 OK: message body …. ….
  • 15. Long Polling Drawbacks • No standard data format or message format when used in chunked transfer encoding mode • Each connection initiation has the initiation cost. • No caching between clients and server, which impacts the server performance instead of reading some contents from the cache. Copyright 2013, Bhakti Mehta all rights reserved
  • 16. Server-sent Events  Unidirectional channel between server and client  Server pushes data to your app when it wants asynchronously  Updates can be streamed from server to client as they happen Copyright 2013, Bhakti Mehta all rights reserved
  • 17. Server-sent Events Copyright 2013, Bhakti Mehta all rights reserved Request (client_id) …. …. …. SSE message …. …. SSE message
  • 18. WebSockets  Next generation of asynchronous communication from client to server  Full duplex communication in either direction  After initial handshake on http communication is over TCP socket using ws(unsecure) or wss(secure) protocol Copyright 2013, Bhakti Mehta all rights reserved
  • 19. WebSocket Handshake Client and Server upgrade from Http protocol to WebSocket protocol during initial handshake GET /text HTTP/1.1rn Upgrade: WebSocketrn Connection: Upgradern Host: www.websocket.orgrn …rn Handshake from server looks like HTTP/1.1 101 WebSocket Protocol Handshakern Upgrade: WebSocketrn Connection: Upgradern …rn Copyright 2013, Bhakti Mehta all rights reserved
  • 20. WebSockets  After the upgrade HTTP is completely out of the picture at this point.  Using the lightweight WebSocket wire protocol, messages can now be sent or received by either endpoint at any time.  Creating a Websocket  ws = new WebSocket("ws://localhost:8080/../WebSocketChat");  You can set handlers for events onopen or onerror Copyright 2013, Bhakti Mehta all rights reserved
  • 21. Other JavaEE technologies Async support • Servlet 3.0 • JAX-RS • EJB Copyright 2013, Bhakti Mehta all rights reserved
  • 22. Components ofAsyncServlet • New attribute asyncSupported on @WebServlet annotation or in web.xml • AsyncContext • Thread Pool • Runnable instance Copyright 2013, Bhakti Mehta all rights reserved
  • 23. AsyncServlet • When asyncSupported attribute is set to true response is not committed on method exit • Calling startAsync() method of AsyncContext caches the request response pair • The method returns and original thread is recycled • AsyncListener and AsyncEvent give developers control of the asynchronous lifecycle events Copyright 2013, Bhakti Mehta all rights reserved
  • 24. AsyncServlet final AsyncContext asyncContext = request.startAsync(); asyncContext.setTimeout(600000); asyncContext.addListener(new AsyncListener() Implement the following method of the AsyncListener onComplete(), onError(),OnTimeout(), onstartAsync() Copyright 2013, Bhakti Mehta all rights reserved
  • 25. Servlet 3.1:Non blocking IO • ReadListener • WriteListener • These are then registered using ServletInputStream.setReadListener and ServletOutputStream.setWriteListener. The listeners have callback methods that are invoked when the content is available to be read or can be written without blocking. Copyright 2013, Bhakti Mehta all rights reserved
  • 26. Servlet 3.1: Non blocking IO public interface ReadListener extends EventListener { public void onDataAvailable(ServletRequest request); public void onAllDataRead(ServletRequest request); public void onError(Throwable t); } Copyright 2013, Bhakti Mehta all rights reserved
  • 27. Servlet 3.0: Non blocking IO • The onDataAvailable: Invoked when all data for the current request has been read. • The onAllDataRead: Method is invoked by the container the first time when it is possible to read data. • The onError: Invoked when an error occurs processing the request Copyright 2013, Bhakti Mehta all rights reserved
  • 28. Servlet 3.0: Non blocking IO public interface WriteListener extends EventListener { public void onWritePossible(ServletResponse response); public void onError(Throwable t); } Copyright 2013, Bhakti Mehta all rights reserved
  • 29. Servlet 3.0: Non blocking IO • The container invokes the onWritePossible method when it is possible to write in the Servlet’s OutputStream. • The onError is invoked when writing in the Servlet’s OutputStream encounters an exception. Copyright 2013, Bhakti Mehta all rights reserved
  • 30. ServletOutputStream • In the ServletOutputStream interface • isReady: This method can be used to determine if data can be written without blocking. • setWriteListener: Instructs the ServletOutputStream to invoke the provided WriteListener when it is possible to write Copyright 2013, Bhakti Mehta all rights reserved
  • 31. ServletInputStream • In the ServletInputStream interface • isFinished: Returns true when all the data from the stream has been read else it returns false. • isReady: Returns true if data can be read without blocking else returns false. • setReadListener: Instructs the ServletInputStream to invoke the provided ReadListener when it is possible to read Copyright 2013, Bhakti Mehta all rights reserved
  • 32. JAX-RS 2.0 Async support • Server Side • Client Side Copyright 2013, Bhakti Mehta all rights reserved
  • 33. JAX-RS 2.0 Server sideAsync support • AsyncResponse: An injectable JAX-RS asynchronous response that provides means for asynchronous server side response processing. • @Suspended: The @Suspended instructs the container that the HTTP request processing should happen in a secondary thread. • CompletionCallback: A request processing callback that receives request processing completion events. Has onComplete() callback • ConnectionCallback: Asynchronous request processing lifecycle callback that receives connection related asynchronous response lifecycle events. Copyright 2013, Bhakti Mehta all rights reserved
  • 34. JAX-RS 2.0 Server sideAsync support • InvocationCallback: Callback that can be implemented to receive the asynchronous processing events from the invocation processing. • Future: Allows the client to poll for completion of the asynchronous operation or to block and wait for it. Copyright 2013, Bhakti Mehta all rights reserved
  • 35. @Suspended @Path(“/books/borrow”) @Stateless public class BookResource { @GET @Produce(“text/plain”) @Asynchronous public void borrow(@Suspended AsyncResponse ar) { final String result = prepareResponse(); ar.resume(result) } } Copyright 2013, Bhakti Mehta all rights reserved
  • 36. Client side code Future<Book> future = client.target("(“books/borrow/borrow") .request() .async() .get(Book.class); try { Book book = future.get(30, TimeUnit.SECONDS); } catch (TimeoutException ex) { System.err.println("Timeout occurred"); } Copyright 2013, Bhakti Mehta all rights reserved
  • 37. InvocationCallback sample client.target(restUrl) .request(MediaType.APPLICATION_JSON) .async() .post(Entity.entity("Here is some text", MediaType.TEXT_PLAIN), new InvocationCallback<Response>() { @Override public void completed(Response response) { processResponse(response.readEntity(JSONObject.class)); } Copyright 2013, Bhakti Mehta all rights reserved
  • 38. EJB 3.1 • @Asynchronous annotation introduced in EJB 3.1 • Methods annotated with @Asynchronous will return immediately regardless of how long the method usually takes. • Each invocation returns a Future object which is initially empty and will have its value filled later when method completes Copyright 2013, Bhakti Mehta all rights reserved
  • 39. AsyncResult • AsyncResult<V> wraps the result of an asynchronous method call as a Future object • The value specified in the constructor AsyncResult(V result) will be retrieved by Container and sent to client. Copyright 2013, Bhakti Mehta all rights reserved
  • 40. Cancelling an async invocation • The session bean can check whether the client requested that the invocation be cancelled by calling the javax.ejb.SessionContext.wasCancelled(). Copyright 2013, Bhakti Mehta all rights reserved
  • 41. @Asynchronous code snippet@Path(“/books/borrow”) @Stateless public class BookResource { @Context private ExecutionContext ctx; @GET @Produce(“application/json”) @Asynchronous public void borrow() { Executors.newSingleThreadExecutor().submit( new Runnable() { public void run() { Thread.sleep(10000); ctx.resume(“Hello async world!”); } }); ctx.suspend(); return; }} Copyright 2013, Bhakti Mehta all rights reserved
  • 42. Java SE and Think Async • Future • A Future object represents the result of asynchronous completion. • Methods to check if computation is complete, wait for result and retrieve results. • Cancellation can be performed by cancel method Copyright 2013, Bhakti Mehta all rights reserved
  • 43. Asynchronous IO • AsynchronousFileChannel • Reads and writes contents from a file asynchronously Copyright 2013, Bhakti Mehta all rights reserved
  • 44. AsynchronousFileChannel try (AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path)){ //Returns a Future instance which can be used to read the contents of the file. Future<Integer> fileResult = asyncChannel.read(buffer, 0); while(!fileResult.isDone()){ System.out.println("Waiting to complete the file reading ..."); } Copyright 2013, Bhakti Mehta all rights reserved
  • 45. Call for action • Download GlassFish 4.0 from glassfish.dev.java.net for latest JavaEE7 API • File issues, contribute patches • Email users@glassfish.dev.java.net Copyright 2013, Bhakti Mehta all rights reserved
  • 46. Questions • Twitter: @bhakti_mehta • Blog https://www.java.net//blogs/bhaktimehta • LinkedIn: http://www.linkedin.com/in/bhaktihmehta • Slideshttp://www.slideshare.net/bhaktiks/think-async Copyright 2013, Bhakti Mehta all rights reserved

Notas do Editor

  1. Long running operations—long running commandsProcessor intensive tasks –tasks that require a lot of computationBackgground tasks which can be batched and doneImprove application throughput by proc
  2. When the asyncSupported attribute is set to true, the response object is not committed on method exit. Calling startAsync() returns an AsyncContext object that caches the request/response object pair. The AsyncContext object is then stored in an application-scoped queue. Without any delay, the doGet() method returns, and the original request thread is recycled. In the ServletContextListener object, separate threads initiated during application launch monitor the queue and resume request processing whenever the resources become available. After a request is processed, you have the option of calling ServletResponse.getWriter().print(...), and then complete() to commit the response, or calling forward() to direct the flow to a JSP page to be displayed as the result. Note that JSP pages are servlets with an asyncSupported attribute that defaults to false.In addition, the AsyncEvent and AsynListener classes in Servlet 3.0 give developers elaborate control of asynchronous lifecycle events. You can register an AsynListener through the ServletRequest.addAsyncListener() method. After the startAsync() method is called on the request, an AsyncEvent is sent to the registered AsyncListener as soon as the asynchronous operation has completed or timed out. The AsyncEvent also contains the same request and response objects as in the AsyncContext object.
  3. After a listener is registered withServletOutStream the status of a nonblocking read can be checked by isReady() method
  4. After a listener is registered withServletinputStream the status of a nonblocking read can be checked by isReady() method
  5. Session beans can implement asynchronous methods, business methods where control is returned to the client by the enterprise bean container before the method is invoked on the session bean instance. Clients may then use the Java SE concurrency API to retrieve the result, cancel the invocation, and check for exceptions. Asynchronous methods are typically used for long-running operations, for processor-intensive tasks, for background tasks, to increase application throughput, or to improve application response time if the method invocation result isn’t required immediately.When a session bean client invokes a typical non-asynchronous business method, control is not returned to the client until the method has completed. Clients calling asynchronous methods, however, immediately have control returned to them by the enterprise bean container. This allows the client to perform other tasks while the method invocation completes. If the method returns a result, the result is an implementation of the java.util.concurrent.Future&lt;V&gt; interface, where “V” is the result value type. The Future&lt;V&gt;interface defines methods the client may use to check whether the computation is completed, wait for the invocation to complete, retrieve the final result, and cancel the invocation.Every time a method annotated @Asynchronous is invoked by anyone it will immediately return regardless of how long the method actually takes. Each invocation returns a Future object that essentially starts out empty and will later have its value filled in by the container when the related method call actually completes. Returning a Future object is not required and @Asynchronous methods can of course return void.
  6. . Note that this object is not passed to the client. It is merely a convenience for providing the result value to the container. Therefore, none of its instance methods should be called by the application.
  7. .
  8. A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled.