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
,java.lang.Cloneable
,java.lang.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
-
-
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(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 connectionabstract void
data(Data data)
Invoked for each leaf node in the data treevoid
deconstruct()
abstract void
endList(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.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.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 connectioncom.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>
render(java.io.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.com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>
renderBeforeHandover(java.io.OutputStream stream, RESPONSE response, Execution execution, 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.processing.rendering.Renderer
clone, getEncoding, getMimeType
-
-
-
-
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(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(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(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, 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:
render
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
-
renderBeforeHandover
public final com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean> renderBeforeHandover(java.io.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 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:
java.lang.IllegalStateException
- if attempted invoked more than once
-
-