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. 1.2 Synchronous
• Almost all web requests are handled
synchronously.
• Asynchronous request handling on WAS is
not easy.
5 / Jetty Continuation
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
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. 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
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. 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. 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. 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. 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. 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
18. 2.4 Continuation interface
• Continuation interface
• Get instance of Continuation
Continuation continuation=
ContinuationSupport.getContinuation(request);
18 / Jetty Continuation
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
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. 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. 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