blob: dd44851b19771deb0b1d2a5e7c0bbbc597b75688 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using Continuations</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="continuations.html" title="Chapter&nbsp;28.&nbsp;Continuations"><link rel="prev" href="continuations.html" title="Chapter&nbsp;28.&nbsp;Continuations"><link rel="next" href="continuations-patterns.html" title="Common Continuation Patterns"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script>
hljs.initHighlightingOnLoad();
</script><link type="text/css" rel="stylesheet" href="css/font-awesome/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.3.28.v20191105</span></td><td style="width: 50%"></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Using Continuations</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="continuations.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;28.&nbsp;Continuations<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="continuations-patterns.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="continuations-using"></a>Using Continuations</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="continuations-using.html#_obtaining_a_continuation">Obtaining a Continuation</a></span></dt><dt><span class="section"><a href="continuations-using.html#_suspending_a_request">Suspending a Request</a></span></dt><dt><span class="section"><a href="continuations-using.html#_resuming_a_request">Resuming a Request</a></span></dt><dt><span class="section"><a href="continuations-using.html#_completing_a_request">Completing a Request</a></span></dt><dt><span class="section"><a href="continuations-using.html#_continuation_listeners">Continuation Listeners</a></span></dt></dl></div><p>Asynchronous servlets were originally introduced with Jetty 6 Continuations, which were a Jetty specific mechanism.
From Jetty 7 onwards, the Continuations API has been extended to be a general purpose API that will work asynchronously on any servlet-3.0 container, as well as on Jetty 6, 7, or 8.
Continuations will also work in blocking mode with any servlet 2.5 container.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_obtaining_a_continuation"></a>Obtaining a Continuation</h3></div></div></div><p>The <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/continuation/ContinuationSupport.html" target="_top">ContinuationSupport</a> factory class can be used to obtain a continuation instance associated with a request:</p><p><code class="literal">Continuation continuation = ContinuationSupport.getContinuation(request);</code></p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_suspending_a_request"></a>Suspending a Request</h3></div></div></div><p>To suspend a request, the suspend method can be called on the continuation:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> void doGet(HttpServletRequest request, HttpServletResponse response)
{
...
// optionally:
// continuation.setTimeout(long);
continuation.suspend();
...
}</code></pre><p>The lifecycle of the request will be extended beyond the return to the container from the <code class="literal">Servlet.service(...)</code> method and <code class="literal">Filter.doFilter(...)</code> calls. When these dispatch methods return, the suspended request will not yet be committed and a response will not yet be sent to the HTTP client.</p><p>Once the request has been suspended, the continuation should be registered with an asynchronous service so that it may be used by an asynchronous callback when the waited-for event happens.</p><p>The request will be suspended until either <code class="literal">continuation.resume()</code> or <code class="literal">continuation.complete()</code> is called. If neither is called then the continuation will timeout.
The timeout should be set before the suspend, by a call to <code class="literal">continuation.setTimeout(long)</code> if no timeout is set, then the default period is used.
If no timeout listeners resume or complete the continuation, then the continuation is resumed with <code class="literal">continuation.isExpired()</code> true.</p><p>Suspension is analogous to the servlet 3.0 <code class="literal">request.startAsync()</code> method. Unlike jetty 6 continuations, an exception is not thrown by suspend and the method should return normally.
This allows the registration of the continuation to occur after suspension and avoids the need for a mutex.
If an exception is desirable (to bypass code that is unaware of continuations and may try to commit the response), then <code class="literal">continuation.undispatch()</code> may be called to exit the current thread from the current dispatch by throwing a <code class="literal">ContinuationThrowable</code>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_resuming_a_request"></a>Resuming a Request</h3></div></div></div><p>Once an asynchronous event has occurred, the continuation can be resumed:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> void myAsyncCallback(Object results)
{
continuation.setAttribute("results",results);
continuation.resume();
}</code></pre><p>When a continuation is resumed, the request is re-dispatched to the servlet container, almost as if the request had been received again.
However during the re-dispatch, the <code class="literal">continuation.isInitial()</code> method returns false and any attributes set by the asynchronous handler are available.</p><p>Continuation resume is analogous to Servlet 3.0 <code class="literal">AsyncContext.dispatch()</code>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_completing_a_request"></a>Completing a Request</h3></div></div></div><p>As an alternative to resuming a request, an asynchronous handler may write the response itself. After writing the response, the handler must indicate the request handling is complete by calling the complete method:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> void myAsyncCallback(Object results)
{
writeResults(continuation.getServletResponse(),results);
continuation.complete();
}</code></pre><p>After complete is called, the container schedules the response to be committed and flushed. Continuation complete is analogous to Servlet 3.0 <code class="literal">AsyncContext.complete()</code>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_continuation_listeners"></a>Continuation Listeners</h3></div></div></div><p>An application may monitor the status of a continuation by using a ContinuationListener:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> void doGet(HttpServletRequest request, HttpServletResponse response)
{
...
Continuation continuation = ContinuationSupport.getContinuation(request);
continuation.addContinuationListener(new ContinuationListener()
{
public void onTimeout(Continuation continuation) { ... }
public void onComplete(Continuation continuation) { ... }
});
continuation.suspend();
...
}</code></pre><p>Continuation listeners are analogous to Servlet 3.0 AsyncListeners.</p></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="continuations.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="continuations.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="continuations-patterns.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;28.&nbsp;Continuations&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Common Continuation Patterns</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/eclipse/jetty.project">Contribute to this documentation at
<span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2019-11-05)</i></span></div></p></body></html>