![Page 1: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/1.jpg)
Globalcode – Open4education
Trilha – Java
Rafael Torres Coelho Soares (Tuelho)[email protected]
@rafaeltuelho
Aumentando a eficiência do Web Container usando chamadas Assíncronas
![Page 2: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/2.jpg)
Globalcode – Open4education
Agenda
Web 2.0
Web Assíncrona
Servlet 3.0 (JSR 315)
Demo
![Page 3: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/3.jpg)
Globalcode – Open4education
Tuelho
Trabalho com Java desde 2004Desenvolvimento (back-end)
Infraestrutura Java
JBoss.orgrhq-project (contributor)
Red Hat (2010-2012)Serviços e consultoria
Oracle Fusion MiddlewareWeblogic Application Server (consultoria)
![Page 4: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/4.jpg)
Globalcode – Open4education
Web interativa
![Page 5: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/5.jpg)
Globalcode – Open4education
Web 2.0 (mais interativa e dinâmica)
![Page 6: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/6.jpg)
Globalcode – Open4education
Novas tecnologias
![Page 7: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/7.jpg)
Globalcode – Open4education
Novos modelos de comunicação
Server events
Client asynchronous requests
pooling
push
![Page 8: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/8.jpg)
Globalcode – Open4education
Server busy!
![Page 9: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/9.jpg)
Globalcode – Open4education
Comunicação HTTP
thread per connectionBaseado no http 1.1 (persistent connection)
thread per request modelnon-blocking I/O (NIO) – Java 1.4
![Page 10: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/10.jpg)
Globalcode – Open4education
WebServer
Http Thread Pool
T#1
T#2
T#6
T#3
T#5T#
4
thread-per-connection
idle
Http Connection
T#5
response
request active
Keep alive timeout… ouConnection timeout
c1c1 c1
Connection Queue(backlog)
C#C1#
![Page 11: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/11.jpg)
Globalcode – Open4education
c1c1 c1
thread-per-request
WebServer
Http Thread Pool
T#1
T#2
T#6
T#3
T#5T#
4 idle
T#5Response #1
Request #1 active
request #2
T#1Response #2
Response time…
Connection Queue(backlog)
C#C1#
![Page 12: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/12.jpg)
Globalcode – Open4education
Web Assíncrona: problemas e motivações
Long Running Requests
Real time updates
![Page 13: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/13.jpg)
Globalcode – Open4education
Servlet 3.0: melhorias
JSR 315 (2009)Asynchronous support
Ease of configuration
Annotations
Pluggability/Modularization: web fragments
Enhancements to existing APIs
![Page 14: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/14.jpg)
Globalcode – Open4education
Async Context
Javax.servlet.AsyncContext.start()
“Causes the container to dispatch a thread, possibly from a managed thread pool, to run the specified Runnable.”
e daí? O que faço com isso?
![Page 15: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/15.jpg)
Globalcode – Open4education
Ciclo de uma requsição assíncrona
① Cliente envia uma requisição
② Servidor invoca um Servlet (nova thread alocada)
③ O Servlet invoca request.startAsync(), salva AsyncContext, e
retorna
④ A thread alocada é liberada, mas o response permanece aberto
⑤ Outra thread (pool separado) utiliza o AsyncContext para concluir a
resposta (asyncContext.complete() ou
asyncContext.dispatch())
⑥ Cliente recebe a resposta do servidor.
![Page 16: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/16.jpg)
Globalcode – Open4education
Processamento assíncrono
WebServer
Http Thread Pool
T#5T#2
T#6
T#3
T#4 idle
T#5
Response
Request active Servlet Context
Managed Thread Pool
request.startAsync()
asyncContext.complete()
1
3
2
5
6
4
![Page 17: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/17.jpg)
Globalcode – Open4education
Sample code
@WebServlet(name = "MyAsyncServlet", urlPatterns = {"/MyAsyncServlet"}, asyncSupported = true)public class MyAsyncServlet extends HttpServlet {
public void doGet(...) { final AsyncContext asyncContext = request.startAsync();
asyncContext.start(new Runnable() { @ Override public void run() { // do some work here which involves waiting ... asyncContext.complete(); } });}
![Page 18: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/18.jpg)
Globalcode – Open4education
JavaSE 5 Executor Framework
![Page 19: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/19.jpg)
Globalcode – Open4education
JavaSE 5 Executor Framework: conceitos
Task
Runnable
Callable
Thread
Synchronous processing
Asynchronous processing
Thread pool
ExecutorService
ThreadPoolExecutor
SchedulePoolExecutor
![Page 20: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/20.jpg)
Globalcode – Open4education
ThreadPoolExecutor
Thread pool
Work queue
Bounded
Unbounded
Handler (saturation policy)
Defaults: RejectedExecutionException runtime exception
Thread Factory
![Page 21: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/21.jpg)
Globalcode – Open4education
Executor: main steps
① Criar uma instância de Executor
② Criar uma taskRunnable ou Callable
③ Submeter a task para execução
④ Executar a task
⑤ Finalizar o Executor
![Page 22: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/22.jpg)
Globalcode – Open4education
ServletContextListener@WebListenerpublic class ServletContextListener implements javax.servlet.ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { Executor executor = new ThreadPoolExecutor(10, 100, 60000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100)); sce.getServletContext(). setAttribute("threadPoolExecutor", executor); }
@Override public void contextDestroyed(ServletContextEvent sce) { ThreadPoolExecutor executor = (ThreadPoolExecutor)sce.getServletContext() .getAttribute("threadPoolExecutor"); executor.shutdownNow(); }}
![Page 23: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/23.jpg)
Globalcode – Open4education
MyAsyncServlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try { AsyncContext asyncCtx = request.startAsync(); ThreadPoolExecutor executor = (ThreadPoolExecutor) request.getServletContext(). getAttribute("threadPoolExecutor"); asyncCtx.setTimeout(-1L); asyncCtx.addListener(new ServletAsyncListener());
//delegate long running process to an "async" thread executor.execute(new AsyncTask(asyncCtx)); } finally { }}
![Page 24: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/24.jpg)
Globalcode – Open4education
AsyncTask (Runnable instance)public class AsyncTask implements Runnable { private AsyncContext asyncContext;
public AsyncTask(AsyncContext asyncContext) { this.asyncContext = asyncContext; }
@Override public void run() { // acesso remoto, query, processamento pesado // escreve na resposta… asyncContext.complete(); // ou asyncContext.dispatch(); }}
![Page 25: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/25.jpg)
Globalcode – Open4education
Demo: sync vs. async
Requisição Servlet Rest Service
Rest
![Page 26: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/26.jpg)
Globalcode – Open4education
… mas eu não uso servlet
JEE 6EJB @Asynchronous
CDI Events
JSF Component FrameworkPrimefaces/RichFaces
ServerPush com Atmosphere
Ajax Pooling
JEE 7Java Websocket Spec (JSR 356)
![Page 27: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/27.jpg)
Globalcode – Open4education
Web Assíncrona: implementações
CometTraditional pooling
Long pooling
Http Streaming
WebSocket protocol
Algumas implementações alternativasAtmosphere Framework (github.com/Atmosphere/)
Tomcat: CometProcessor class
Jetty 6: Continuations
Grizzly (glassfish): CometEngine class.
![Page 28: Aumentando a eficiência do Web Container usando chamadas Assíncronas](https://reader034.vdocuments.net/reader034/viewer/2022051611/54b7ac154a79594b258b4698/html5/thumbnails/28.jpg)
Globalcode – Open4education
DÚVIDAS?