Class AsynchronousSectionedRenderer<RESPONSE extends Response>

java.lang.Object
com.yahoo.component.AbstractComponent
com.yahoo.processing.rendering.Renderer<RESPONSE>
com.yahoo.processing.rendering.AsynchronousRenderer<RESPONSE>
com.yahoo.processing.rendering.AsynchronousSectionedRenderer<RESPONSE>
All Implemented Interfaces:
com.yahoo.component.Component, com.yahoo.component.Deconstructable, Cloneable, Comparable<com.yahoo.component.Component>
Direct Known Subclasses:
ProcessingRenderer

public abstract class AsynchronousSectionedRenderer<RESPONSE extends Response> extends AsynchronousRenderer<RESPONSE>
Helper class to implement processing API Response renderers. This renderer will walk the data tree and call the appropriate render methods as it progresses. Nodes with the same parent branch will be rendered in the order in which the data is ready for consumption.

This API assumes all data should be rendered. Choosing which data should be rendered is the responsibility of the processing chains.

Author:
Steinar Knutsen, Einar M R Rosenvinge, bratseth
  • Field Summary

    Fields inherited from class com.yahoo.component.AbstractComponent

    isDeconstructable
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create an renderer instance not yet associated with any request processing or network for easy subclassing.
    Create a renderer using the specified executor instead of the default one which should be used for production.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    Invoked at the beginning of each data list, including the implicit, outermost one in the response.
    abstract void
    Invoked once at the beginning of rendering a response.
    protected boolean
    Returns whether the client this is rendering to has closed the connection
    abstract void
    data(Data data)
    Invoked for each leaf node in the data tree
    void
     
    abstract void
    endList(DataList<?> list)
    Invoked at the end of each data list, including the implicit, outermost one in the response.
    abstract void
    Invoked once at the end of rendering a response.
    The outermost execution which was run to create the response to render.
    int
    How deep into the tree of nested data lists the callback currently is.
    The response render callbacks are generated from.
    void
    Do per instance initialization.
    protected void
    This hook is called once when the renderer detects that the client has closed the connection
    renderResponse(OutputStream stream, RESPONSE response, Execution execution, Request request)
    Render this response using the renderer's own threads and return a future indicating whether the rendering was successful.
    renderResponseBeforeHandover(OutputStream stream, RESPONSE response, Execution execution, Request request)
    Initiate rendering before handover to rendering threads.
    final void
    setNetworkWiring(com.yahoo.jdisc.handler.ContentChannel channel, com.yahoo.jdisc.handler.CompletionHandler completionHandler)
    For internal use: Expose JDisc wiring to ensure asynchronous cleanup.

    Methods inherited from class com.yahoo.processing.rendering.Renderer

    clone, getEncoding, getMimeType

    Methods inherited from class com.yahoo.component.AbstractComponent

    compareTo, getClassName, getId, getIdString, hasInitializedId, initId, isDeconstructable, setIsDeconstructable, toString

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • AsynchronousSectionedRenderer

      public AsynchronousSectionedRenderer()
      Create an renderer instance not yet associated with any request processing or network for easy subclassing. It is the handler's responsibility to wire in the resources needed by a renderer before use.
    • AsynchronousSectionedRenderer

      public AsynchronousSectionedRenderer(Executor executor)
      Create a renderer using the specified executor instead of the default one which should be used for production. Using a custom executor is useful for tests to avoid creating new threads for each renderer registry.
      Parameters:
      executor - the executor to use or null to use the default executor suitable for production
  • Method Details

    • beginResponse

      public abstract void beginResponse(OutputStream stream) throws IOException
      Invoked once at the beginning of rendering a response. This assigns the stream to be used throughput the rendering. Subsequent calls must use the same stream.
      Parameters:
      stream - the stream to render to in this and all subsequent calls.
      Throws:
      IOException - passed on from the stream
    • beginList

      public abstract void beginList(DataList<?> list) throws IOException
      Invoked at the beginning of each data list, including the implicit, outermost one in the response.
      Parameters:
      list - the data list which now will be rendered
      Throws:
      IOException - passed on from the stream
    • data

      public abstract void data(Data data) throws IOException
      Invoked for each leaf node in the data tree
      Parameters:
      data - the leaf node to render
      Throws:
      IOException - passed on from the stream
    • endList

      public abstract void endList(DataList<?> list) throws IOException
      Invoked at the end of each data list, including the implicit, outermost one in the response.
      Parameters:
      list - the data list which now has no more data items to render
      Throws:
      IOException - passed on from the stream
    • endResponse

      public abstract void endResponse() throws IOException
      Invoked once at the end of rendering a response.
      Throws:
      IOException - passed on from the stream
    • renderResponse

      public final CompletableFuture<Boolean> renderResponse(OutputStream stream, RESPONSE response, Execution execution, Request request)

      Render this response using the renderer's own threads and return a future indicating whether the rendering was successful. The data list tree will be traversed asynchronously, and the pertinent methods will be called as data becomes available.

      If rendering fails, the exception causing this will be wrapped in an ExecutionException and thrown from blocked calls to Future.get()

      Specified by:
      renderResponse in class Renderer<RESPONSE extends Response>
      Parameters:
      stream - a stream API bridge to JDisc
      response - the response to render
      execution - the execution which created this response
      request - the request matching the response
      Returns:
      a future indicating whether rendering was successful
    • deconstruct

      public void deconstruct()
      Specified by:
      deconstruct in interface com.yahoo.component.Deconstructable
      Overrides:
      deconstruct in class com.yahoo.component.AbstractComponent
    • renderResponseBeforeHandover

      public final CompletableFuture<Boolean> renderResponseBeforeHandover(OutputStream stream, RESPONSE response, Execution execution, Request request)
      Initiate rendering before handover to rendering threads. This is rendering which happens before the Response is returned from the main chain, caused by freezing of DataLists. At this point the worker thread still owns the Response, so all this rendering must happen on the caller thread invoking freeze (that is, on the thread calling this).
    • getExecution

      public Execution getExecution()
      The outermost execution which was run to create the response to render.
    • getResponse

      public Response getResponse()
      The response render callbacks are generated from.
    • clientClosed

      protected boolean clientClosed()
      Returns whether the client this is rendering to has closed the connection
    • onClientClosed

      protected void onClientClosed()
      This hook is called once when the renderer detects that the client has closed the connection
    • getRecursionLevel

      public int getRecursionLevel()
      How deep into the tree of nested data lists the callback currently is. beginList() is invoked after this is increased, and endList() is invoked before it is decreased.
      Returns:
      an integer of 1 or above
    • setNetworkWiring

      public final void setNetworkWiring(com.yahoo.jdisc.handler.ContentChannel channel, com.yahoo.jdisc.handler.CompletionHandler completionHandler)
      For internal use: Expose JDisc wiring to ensure asynchronous cleanup.
      Specified by:
      setNetworkWiring in class AsynchronousRenderer<RESPONSE extends Response>
      Parameters:
      channel - the channel to the client receiving the response
      completionHandler - the JDisc completion handler which will be invoked at the end of the rendering
      Throws:
      IllegalStateException - if attempted invoked more than once
    • init

      public void init()
      Do per instance initialization. If overriding this in a subclass, not invoking it in the subclass' implementation will most likely cause the rendering to fail with an exception.
      Overrides:
      init in class Renderer<RESPONSE extends Response>