Jetty Continuation
작성자: 이상민
문서범위 (일반)
ⓒ 2010 NHN CORPORATION
목차
1. Current WAS
2. Jetty Continuation
3. Jetty Continuation sample & demo
4. Servlet 3.0
1. Current WAS (Based on Servlet spec. 2.5)
4 / Jetty Continuation
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.
5 / Jetty Continuation
1.2 Synchronous
• Almost all web requests are handled synchronously.
• Asynchronous request handling on WAS is not easy.
6 / Jetty Continuation
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 holdonto 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
7 / Jetty Continuation
2. Jetty Continuation
8 / Jetty Continuation
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
9 / Jetty Continuation
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
10 / Jetty Continuation
2.1.3 Continuation
• Continuation in Naver dictionary
11 / Jetty Continuation
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.
12 / Jetty Continuation
2.1.2 Continuation
• Continuation• Process of continuation
Return thread
Take thread
Registered with an async. service
Working in other thread
13 / Jetty Continuation
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.
14 / Jetty Continuation
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.
15 / Jetty Continuation
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
16 / Jetty Continuation
2.3 Continuation sample
• Continuation samplepublic 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 {}return;
}
Call resume() or complete(), after work is done.
17 / Jetty Continuation
2.4 Continuation interface
• Continuation interface
• Package : org.eclipse.jetty.continuation
• Methods :
• setTimeout(long)
• addContinuationListener(ContinuationListener)
• suspend(), suspend(ServletResponse)
• resume()
• complete()
…
18 / Jetty Continuation
2.4 Continuation interface
• Continuation interface
• Get instance of Continuation
Continuation continuation=ContinuationSupport.getContinuation(request);
19 / Jetty Continuation
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);
20 / Jetty Continuation
2.5.2.1 Continuation. addContinuationListener(ContinuationListener )
• addContinuationListener(ContinuationListener)
method
• Add a ContinuationListener
• You can handle continuation result by ContinuationListener
Sample code
//Make listener classSampleListener listener=new SampleListener();//Add listener to continuation objectcontinuation.addContinuationListener(listener);
21 / Jetty Continuation
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() {}
public void onComplete(Continuation continuation) {
}
public void onTimeout(Continuation continuation) {
}
}
When Continuation is completed
When Continuation is timeout
22 / Jetty Continuation
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.
23 / Jetty Continuation
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);
24 / Jetty Continuation
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.
25 / Jetty Continuation
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.
26 / Jetty Continuation
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
27 / Jetty Continuation
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.
28 / Jetty Continuation
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.");sample.start();System.out.println("After start sample.");
}public void run() {
System.out.println("Run method called");}
}
Before start sample.After start sample.Run method called
29 / Jetty Continuation
2.6 Asynchronous on Java
• Executor sample
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;
}}
package com.cont.sample.async.executor;public class SampleReceiver {
public void receiveResult(Object obj) {System.out.println("Received result is "+obj);
}}
30 / Jetty Continuation
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();
SampleReceiver receiver=new SampleReceiver();
SampleCallable callable=new SampleCallable(receiver);
System.out.println("Before submit");service.submit(callable);System.out.println("After submit");
}}
Before submitAfter submitSampleCallable is calledReceived result is OK
31 / Jetty Continuation
3. Jetty Continuation sample & demo
32 / Jetty Continuation
3.1 Common Servlet
• Common Servlet Sample• Continuation timeout sample• Continuation resume() sample• Continuation complete() sample
33 / Jetty Continuation
4. Servlet 3.0
34 / Jetty Continuation
4.1 Servlet 3.0
• Final release of Servlet 3.0 was announced at 10 Dec, 2009.
• JSR(Java Specification Request) – 315http://jcp.org/en/jsr/detail?id=315
• Glassfish 3 is based on Servlet 3.0
35 / Jetty Continuation
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
36 / Jetty Continuation
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()
…
37 / Jetty Continuation
4.4 ServletRequest
• ServletRequest interface
• Package : javax.servlet
• Added Methods :
• startAsync()
• startAsync(ServletRequest , ServletResponse )
…
38 / Jetty Continuation
5. Conclusion
39 / Jetty Continuation
Jetty Continuation
One
Multi
Thank you.