Class AsynchronousSectionedRenderer<RESPONSE extends com.yahoo.processing.Response>

  • All Implemented Interfaces:
    com.yahoo.component.Component, java.lang.Cloneable, java.lang.Comparable<com.yahoo.component.Component>
    Direct Known Subclasses:
    ProcessingRenderer

    public abstract class AsynchronousSectionedRenderer<RESPONSE extends com.yahoo.processing.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
      AsynchronousSectionedRenderer()
      Create an renderer instance not yet associated with any request processing or network for easy subclassing.
      AsynchronousSectionedRenderer​(java.util.concurrent.Executor executor)
      Create an renderer using the specified executor instead of the default one which should be used for production.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract void beginList​(com.yahoo.processing.response.DataList<?> list)
      Invoked at the beginning of each data list, including the implicit, outermost one in the response.
      abstract void beginResponse​(java.io.OutputStream stream)
      Invoked once at the beginning of rendering a response.
      protected boolean clientClosed()
      Returns whether the client this is rendering to has closed the connection
      abstract void data​(com.yahoo.processing.response.Data data)
      Invoked for each leaf node in the data tree
      void deconstruct()  
      abstract void endList​(com.yahoo.processing.response.DataList<?> list)
      Invoked at the end of each data list, including the implicit, outermost one in the response.
      abstract void endResponse()
      Invoked once at the end of rendering a response.
      com.yahoo.processing.execution.Execution getExecution()
      The outermost execution which was run to create the response to render.
      int getRecursionLevel()
      How deep into the tree of nested data lists the callback currently is.
      com.yahoo.processing.Response getResponse()
      The response render callbacks are generated from.
      void init()
      Do per instance initialization.
      protected void onClientClosed()
      This hook is called once when the renderer detects that the client has closed the connection
      com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> render​(java.io.OutputStream stream, RESPONSE response, com.yahoo.processing.execution.Execution execution, com.yahoo.processing.Request request)
      Render this response using the renderer's own threads and return a future indicating whether the rendering was successful.
      com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> renderBeforeHandover​(java.io.OutputStream stream, RESPONSE response, com.yahoo.processing.execution.Execution execution, com.yahoo.processing.Request request)
      Initiate rendering before handover to rendering threads.
      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.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 Detail

      • 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​(java.util.concurrent.Executor executor)
        Create an 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 Detail

      • beginResponse

        public abstract void beginResponse​(java.io.OutputStream stream)
                                    throws java.io.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:
        java.io.IOException - passed on from the stream
      • beginList

        public abstract void beginList​(com.yahoo.processing.response.DataList<?> list)
                                throws java.io.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:
        java.io.IOException - passed on from the stream
      • data

        public abstract void data​(com.yahoo.processing.response.Data data)
                           throws java.io.IOException
        Invoked for each leaf node in the data tree
        Parameters:
        data - the leaf node to render
        Throws:
        java.io.IOException - passed on from the stream
      • endList

        public abstract void endList​(com.yahoo.processing.response.DataList<?> list)
                              throws java.io.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:
        java.io.IOException - passed on from the stream
      • endResponse

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

        public final com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> render​(java.io.OutputStream stream,
                                                                                                  RESPONSE response,
                                                                                                  com.yahoo.processing.execution.Execution execution,
                                                                                                  com.yahoo.processing.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:
        render in class Renderer<RESPONSE extends com.yahoo.processing.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()
        Overrides:
        deconstruct in class com.yahoo.component.AbstractComponent
      • renderBeforeHandover

        public final com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> renderBeforeHandover​(java.io.OutputStream stream,
                                                                                                                RESPONSE response,
                                                                                                                com.yahoo.processing.execution.Execution execution,
                                                                                                                com.yahoo.processing.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 com.yahoo.processing.execution.Execution getExecution()
        The outermost execution which was run to create the response to render.
      • getResponse

        public com.yahoo.processing.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 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 com.yahoo.processing.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:
        java.lang.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 com.yahoo.processing.Response>