Package com.yahoo.processing.rendering
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
ConstructorDescriptionCreate an renderer instance not yet associated with any request processing or network for easy subclassing.AsynchronousSectionedRenderer
(Executor executor) Create a renderer using the specified executor instead of the default one which should be used for production. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
Invoked at the beginning of each data list, including the implicit, outermost one in the response.abstract void
beginResponse
(OutputStream stream) Invoked once at the beginning of rendering a response.protected boolean
Returns whether the client this is rendering to has closed the connectionabstract void
Invoked for each leaf node in the data treevoid
abstract void
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
init()
Do per instance initialization.protected void
This hook is called once when the renderer detects that the client has closed the connectionfinal 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.final CompletableFuture<Boolean>
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
-
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
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
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
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
Invoked for each leaf node in the data tree- Parameters:
data
- the leaf node to render- Throws:
IOException
- passed on from the stream
-
endList
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
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 classRenderer<RESPONSE extends Response>
- Parameters:
stream
- a stream API bridge to JDiscresponse
- the response to renderexecution
- the execution which created this responserequest
- the request matching the response- Returns:
- a future indicating whether rendering was successful
-
deconstruct
public void deconstruct()- Specified by:
deconstruct
in interfacecom.yahoo.component.Deconstructable
- Overrides:
deconstruct
in classcom.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
The outermost execution which was run to create the response to render. -
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 classAsynchronousRenderer<RESPONSE extends Response>
- Parameters:
channel
- the channel to the client receiving the responsecompletionHandler
- 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.
-