Search code examples
jettycontinuationsjakarta-migrationjetty-12

What is the equivalent of the code written by jetty.continuation in Jakarta.server.AsyncContext?


I want to upgrade my project from jetty 9 to 12. Since the jetty.continuation is deprecated, I want to replace the following code snippet using AsyncContext class.

        Continuation cont = ContinuationSupport.getContinuation(request);
         .
         .
         .
        cont.setTimeout(suspendMs);
        cont.suspend();

what is the code equivalent?

which method does implement "suspend" in AsyncContext class?


Solution

  • The best source of advice for moving from Jetty Continuations (deprecated in Jetty 8 btw, removed in Jetty 10), is to look at the Servlet3Continuations.java source in the jetty-9.4.x branch.

    Methods like suspend() and resume() are not a 1::1 method call on AsyncContext, but in fact multiple method calls.

    Continuation to AsyncContext

    • Continuation.setTimeout(long) ::
      AsyncContext.setTimeout(long);
      
    • Continuation.suspend() ::
      HttpServletRequest.startAsync();
      HttpServletRequest.setTimeout(...);
      HttpServletRequest.addListener(AsyncListener); // handles onTimeout and onComplete
      
    • Continuation.suspend(ServletResponse response) ::
      HttpServletRequest.startAsync(request, response);
      HttpServletRequest.setTimeout(...);
      HttpServletRequest.addListener(AsyncListener); // handles onTimeout and onComplete
      
    • Continuation.resume() ::
      AsyncContext.dispatch();
      
    • Continuation.complete() ::
      AsyncContext.complete();
      
    • Continuation.isSuspended() ::
      HttpServletRequest.isAsyncStarted();
      
    • Continuation.isResumed() ::
      // no servlet API for this
      
    • Continuation.isExpired() ::
      // no servlet API for this
      
    • Continuation.isInitial() ::
      if (HttpServletRequest.getDispatcherType() != DispatcherType.ASYNC) {
        // possibly the initial request
        // note, this could be a REQUEST / INCLUDE / FORWARD / ERROR dispatch
      }
      
    • Continuation.isResponseWrapped() ::
      // no servlet API for this
      
    • Continuation.getServletResponse() ::
      AsyncContext.getRequest();
      AsyncContext.getResponse();
      
    • Continuation.addContinuationListener(ContinuationListener) ::
      AsyncContext.addListener(AsyncListener);
      
    • Continuation.setAttribute(String, Object) ::
      HttpServletRequest.setAttribute(String, Object);
      
    • Continuation.getAttribute(String) ::
      HttpServletRequest.getAttribute(String);
      
    • Continuation.removeAttribute(String) ::
      HttpServletRequest.removeAttribute(String);
      
    • Continuation.undispatch() ::
      // no servlet API for this