40
Jetty Continuation 작성자: 이상민 문서범위 (일반) 2010 NHN CORPORATION

Jetty Continuation - 이상민

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Jetty Continuation - 이상민

Jetty Continuation

작성자: 이상민

문서범위 (일반)

ⓒ 2010 NHN CORPORATION

Page 2: Jetty Continuation - 이상민

목차

1. Current WAS

2. Jetty Continuation

3. Jetty Continuation sample & demo

4. Servlet 3.0

Page 3: Jetty Continuation - 이상민

1. Current WAS (Based on Servlet spec. 2.5)

Page 4: Jetty Continuation - 이상민

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.

Page 5: Jetty Continuation - 이상민

5 / Jetty Continuation

1.2 Synchronous

• Almost all web requests are handled synchronously.

• Asynchronous request handling on WAS is not easy.

Page 6: Jetty Continuation - 이상민

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

Page 7: Jetty Continuation - 이상민

7 / Jetty Continuation

2. Jetty Continuation

Page 8: 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

Page 9: Jetty Continuation - 이상민

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

Page 10: Jetty Continuation - 이상민

10 / Jetty Continuation

2.1.3 Continuation

• Continuation in Naver dictionary

Page 11: Jetty Continuation - 이상민

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.

Page 12: Jetty Continuation - 이상민

12 / Jetty Continuation

2.1.2 Continuation

• Continuation• Process of continuation

Return thread

Take thread

Registered with an async. service

Working in other thread

Page 13: Jetty Continuation - 이상민

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.

Page 14: Jetty Continuation - 이상민

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.

Page 15: Jetty Continuation - 이상민

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

Page 16: Jetty Continuation - 이상민

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.

Page 17: Jetty Continuation - 이상민

17 / Jetty Continuation

2.4 Continuation interface

• Continuation interface

• Package : org.eclipse.jetty.continuation

• Methods :

• setTimeout(long)

• addContinuationListener(ContinuationListener)

• suspend(), suspend(ServletResponse)

• resume()

• complete()

Page 18: Jetty Continuation - 이상민

18 / Jetty Continuation

2.4 Continuation interface

• Continuation interface

• Get instance of Continuation

Continuation continuation=ContinuationSupport.getContinuation(request);

Page 19: Jetty Continuation - 이상민

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);

Page 20: Jetty Continuation - 이상민

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);

Page 21: Jetty Continuation - 이상민

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

Page 22: Jetty Continuation - 이상민

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.

Page 23: Jetty Continuation - 이상민

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);

Page 24: Jetty Continuation - 이상민

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.

Page 25: Jetty Continuation - 이상민

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.

Page 26: Jetty Continuation - 이상민

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

Page 27: Jetty Continuation - 이상민

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.

Page 28: Jetty Continuation - 이상민

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

Page 29: Jetty Continuation - 이상민

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);

}}

Page 30: Jetty Continuation - 이상민

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

Page 31: Jetty Continuation - 이상민

31 / Jetty Continuation

3. Jetty Continuation sample & demo

Page 32: Jetty Continuation - 이상민

32 / Jetty Continuation

3.1 Common Servlet

• Common Servlet Sample• Continuation timeout sample• Continuation resume() sample• Continuation complete() sample

Page 33: Jetty Continuation - 이상민

33 / Jetty Continuation

4. Servlet 3.0

Page 34: Jetty Continuation - 이상민

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

Page 35: Jetty Continuation - 이상민

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

Page 36: Jetty Continuation - 이상민

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()

Page 37: Jetty Continuation - 이상민

37 / Jetty Continuation

4.4 ServletRequest

• ServletRequest interface

• Package : javax.servlet

• Added Methods :

• startAsync()

• startAsync(ServletRequest , ServletResponse )

Page 38: Jetty Continuation - 이상민

38 / Jetty Continuation

5. Conclusion

Page 39: Jetty Continuation - 이상민

39 / Jetty Continuation

Jetty Continuation

One

Multi

Page 40: Jetty Continuation - 이상민

Thank you.