Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Loading in …3
×
1 of 40

Jetty Continuation - 이상민

5

Share

Download to read offline

2010 한국 자바 개발자 페스티벌 Track 3 Session 2 발표자료

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Jetty Continuation - 이상민

  1. 1. Jetty Continuation 작성자: 이상민 문서범위 (일반) ⓒ 2010 NHN CORPORATION
  2. 2. 목차 1. Current WAS 2. Jetty Continuation 3. Jetty Continuation sample & demo 4. Servlet 3.0
  3. 3. 1. Current WAS (Based on Servlet spec. 2.5)
  4. 4. 1.1 Thread Pool • Whenever we request, WAS always doesn’t make new thread.  It uses Thread Pool. • It’s not easy to set suitable Thread Pool size. • Thread per connection • The traditional IO model of java associates a thread with every TCP/IP connection. 4 / Jetty Continuation
  5. 5. 1.2 Synchronous • Almost all web requests are handled synchronously. • Asynchronous request handling on WAS is not easy. 5 / Jetty Continuation
  6. 6. 1.3 AJAX polling problem • AJAX servers cannot deliver asynchronous events to the client, the AJAX client must poll for events on the server. • To avoid a busy polling loop, AJAX servers will often hold onto a poll request until either there is an event or a timeout occurs. • The server again needs to have one or more threads for every client. http://docs.codehaus.org/display/JETTY/Continuations 6 / Jetty Continuation
  7. 7. 2. Jetty Continuation 7 / Jetty Continuation
  8. 8. 2.1.1 What is Jetty • Jetty - http://eclipse.org/jetty/ • Jetty provides an HTTP server, HTTP client, and javax.servlet container. • Full-featured and standards-based • Open source and commercially usable • Flexible and extensible • Small footprint • Embeddable • Asynchronous • Enterprise scalable • Dual licensed under Apache and Eclipse 8 / Jetty Continuation
  9. 9. 2.1.2 Asynchronous Servlet • Asynchronous Servlets • Not Asynchronous IO • Asynchronous Servlets are different with Asynchronous IO or the use of NIO • Asynchronous Waiting • waiting for a resource to be available before processing the request • waiting for an application event in an AJAX Comet application • waiting for a response from a remote service 9 / Jetty Continuation
  10. 10. 2.1.3 Continuation • Continuation in Naver dictionary 10 / Jetty Continuation
  11. 11. 2.1.3 Continuation • Continuation in Jetty • A continuation is a mechanism by which a HTTP Request can be suspended and restarted after a timeout or an asynchronous event has occurred. 11 / Jetty Continuation
  12. 12. 2.1.2 Continuation • Continuation • Process of continuation Registered with an async. service Working in other thread Return thread Take thread 12 / Jetty Continuation
  13. 13. 2.2.1 Thread Pool with Continuation • You also should use Thread Pool when you use Continuation • But you can manage WAS with a fewer Thread. 13 / Jetty Continuation
  14. 14. 2.2.2 Use Asynchronous call with Continuation • You must use “Asynchronous call” to use Continuation • One thread can run much request at a time. 14 / Jetty Continuation
  15. 15. 2.2.3 Continuation examples • Chat Servlet • http://download.eclipse.org/jetty/stable- 7/xref/com/acme/ChatServlet.html • Jetty’s filter & servlet • QoSFilter (Quality of Service Filter) • DosFilter (Denial of Service Filter) • GzipFilter (Compress response objects) • ProxyServlet 15 / Jetty Continuation
  16. 16. 2.3 Continuation sample • Continuation sample public class ContinuationServlet extends HttpServlet{ public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { String flag=(String)req.getAttribute("CONT_FLAG"); if(flag==null) { Continuation continuation = ContinuationSupport.getContinuation(req); continuation.addContinuationListener(new AListener()); continuation.setTimeout(5000); continuation.suspend(); req.setAttribute("CONT_FLAG", "RUNNING"); //asynchronous call } else { } Call resume() or complete(), return; after work is done. } 16 / Jetty Continuation
  17. 17. 2.4 Continuation interface • Continuation interface • Package : org.eclipse.jetty.continuation • Methods : • setTimeout(long) • addContinuationListener(ContinuationListener) • suspend(), suspend(ServletResponse) • resume() • complete() … 17 / Jetty Continuation
  18. 18. 2.4 Continuation interface • Continuation interface • Get instance of Continuation Continuation continuation= ContinuationSupport.getContinuation(request); 18 / Jetty Continuation
  19. 19. 2.5.1 Continuation.setTimeout (long) • setTimeout(long timeoutMS) method • Set the continuation timeout. • You can set timeout in millisecond(1/1000 second) unit. • If timeoutMS is <=0, the continuation will never expire. (Don’t do that - -) Sample code //set timeout 5 seconds continuation.setTimeout(5000); 19 / Jetty Continuation
  20. 20. 2.5.2.1 Continuation. addContinuationListener(ContinuationListener ) • addContinuationListener(ContinuationListener) method • Add a ContinuationListener • You can handle continuation result by ContinuationListener Sample code //Make listener class SampleListener listener=new SampleListener(); //Add listener to continuation object continuation.addContinuationListener(listener); 20 / Jetty Continuation
  21. 21. 2.5.2.2 How can I make ContinuationListener? • Just implements ContinuationListener interface with two methods Sample code import org.eclipse.jetty.continuation.Continuation; import org.eclipse.jetty.continuation.ContinuationListener; public class SampleListener implements ContinuationListener{ public SampleListener() { } When Continuation is completed public void onComplete(Continuation continuation) { } public void onTimeout(Continuation continuation) { } } When Continuation is timeout 21 / Jetty Continuation
  22. 22. 2.5.3 Continuation.suspend() • suspend() & suspend(ServletResponse) method • Suspend the processing of the request and associated ServletResponse. • After this method has been called, the lifecycle of the request will be extended beyond the return to the container from • Servlet.service(ServletRequest, ServletResponse) • Filter.doFilter(ServletRequest, ServletResponse, FilterChain) calls. 22 / Jetty Continuation
  23. 23. 2.5.3 Continuation.suspend() • suspend() & suspend(ServletResponse) method • The thread is freed for other tasks and the request is held until • a call to resume(). • a call to complete(). • the timeout expires. Sample code continuation.suspend(); or continuation.suspend(response); 23 / Jetty Continuation
  24. 24. 2.5.4 Continuation.resume() • resume() method • Resume a suspended request. • The request is redispatched to the normal filter chain and servlet processing with isInitial() false. • Typically resume() is used after a call to suspend() with no arguments. 24 / Jetty Continuation
  25. 25. 2.5.5 Continuation.complete() • complete() method • Complete a suspended request. • The associated response object committed and flushed. The request is not redispatched. • Typically complete() is used after a call to suspend(ServletResponse) with a possibly wrapped response. 25 / Jetty Continuation
  26. 26. 2.5.5 resume() vs complete() • compare resume() vs complete() methods resume() complete() redispatch O X suspend type suspend() suspend(ServletResponse) wrap response X O 26 / Jetty Continuation
  27. 27. 2.6 Asynchronous on Java • Use java.lang.Thread • Easy to use • But making new thread every time is not good in performance • Use java.util.concurrent.ExecutorService • Not easy to use at first time. • But it reuse threads and manages thread pool. 27 / Jetty Continuation
  28. 28. 2.6 Asynchronous on Java • Thread sample package com.cont.sample.async; public class ThreadSample extends Thread { public static void main(String args[]) { ThreadSample sample=new ThreadSample(); System.out.println("Before start sample."); Before start sample. sample.start(); After start sample. System.out.println("After start sample."); Run method called } public void run() { System.out.println("Run method called"); } } 28 / Jetty Continuation
  29. 29. 2.6 Asynchronous on Java • Executor sample package com.cont.sample.async.executor; public class SampleReceiver { public void receiveResult(Object obj) { System.out.println("Received result is "+obj); } } package com.cont.sample.async.executor; import java.util.concurrent.Callable; public class SampleCallable implements Callable<Object>{ private SampleReceiver receiver; public SampleCallable(SampleReceiver receiver) { this.receiver=receiver; } public Object call() throws Exception { System.out.println("SampleCallable is called"); receiver.receiveResult("OK"); return null; } } 29 / Jetty Continuation
  30. 30. 2.6 Asynchronous on Java • Executor sample – execute package com.cont.sample.async.executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ExecutorMain { public static void main(String[] args) { ExecutorService service= Executors.newCachedThreadPool(); Before submit After submit SampleReceiver receiver= SampleCallable is called new SampleReceiver(); Received result is OK SampleCallable callable= new SampleCallable(receiver); System.out.println("Before submit"); service.submit(callable); System.out.println("After submit"); } } 30 / Jetty Continuation
  31. 31. 3. Jetty Continuation sample & demo 31 / Jetty Continuation
  32. 32. 3.1 Common Servlet • Common Servlet Sample • Continuation timeout sample • Continuation resume() sample • Continuation complete() sample 32 / Jetty Continuation
  33. 33. 4. Servlet 3.0 33 / Jetty Continuation
  34. 34. 4.1 Servlet 3.0 • Final release of Servlet 3.0 was announced at 10 Dec, 2009. • JSR(Java Specification Request) – 315 http://jcp.org/en/jsr/detail?id=315 • Glassfish 3 is based on Servlet 3.0 34 / Jetty Continuation
  35. 35. 4.2 Servlet 3.0 new features • Asynchronous Servlets and comet support. • Web framework pluggability. • Use Annotations to define web.xml setting easier • @WebServlet, @WebFilter, @WebInitParam, @WebListner, @MultipartConfig • Modularization of web.xml • Use web-fragment.xml file http://www.jsptube.com/servlet-tutorials/servlet3/new-features.html 35 / Jetty Continuation
  36. 36. 4.3 AsyncContext • AsyncContext interface • Package : javax.servlet • Class representing the execution context for an asynchronous operation that was initiated on a ServletRequest. • Methods : • setTimeout(long) • addListener(AsyncListener) • complete() … 36 / Jetty Continuation
  37. 37. 4.4 ServletRequest • ServletRequest interface • Package : javax.servlet • Added Methods : • startAsync() • startAsync(ServletRequest , ServletResponse ) … 37 / Jetty Continuation
  38. 38. 5. Conclusion 38 / Jetty Continuation
  39. 39. Jetty Continuation One Multi 39 / Jetty Continuation
  40. 40. Thank you.

×