public class ReactiveDataDriverContextVariable extends Object implements IReactiveSSEDataDriverContextVariable
Basic implementation of the IReactiveDataDriverContextVariable
interface, including also
the extensions specified in IReactiveSSEDataDriverContextVariable
.
The reactive data stream wrapped by this class will usually have the shape of an implementation of the
Publisher
interface, such as Flux
. But other types of reactive
artifacts are supported thanks to Spring's ReactiveAdapterRegistry
mechanism.
Data-driver context variables are required to be multi-valued.
Being multi-valued does not mean to necessarily return more than one value,
but simply to have the capability to do so. E.g. a Flux
object will
be considered multi-valued even if it publishes none or just one result, whereas a
Mono
object will be considered single-valued.
Example use:
@RequestMapping("/something")
public String doSomething(final Model model) {
final Publisher<Item> data = ...; // This has to be MULTI-VALUED (e.g. Flux)
model.addAttribute("data", new ReactiveDataDriverContextVariable(data, 100));
return "view";
}
And then at the template:
<table>
<tbody>
<tr th:each="item : ${data}">
<td th:text="${item}">some item...</td>
</tr>
</tbody>
</table>
For more information on the way this class would work in SSE (Server-Sent Event) scenarios, see
IReactiveSSEDataDriverContextVariable
.
This class is NOT thread-safe. Thread-safety is not a requirement for context variables.
IReactiveDataDriverContextVariable
,
IReactiveSSEDataDriverContextVariable
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTS
Default buffer size to be applied if none is specified.
|
static long |
DEFAULT_FIRST_EVENT_ID
Default value for the first event ID (for SSE scenarios).
|
Constructor and Description |
---|
ReactiveDataDriverContextVariable(Object dataStream)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream.
|
ReactiveDataDriverContextVariable(Object dataStream,
int dataStreamBufferSizeElements)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a
buffer size.
|
ReactiveDataDriverContextVariable(Object dataStream,
int dataStreamBufferSizeElements,
long firstEventID)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a
buffer size and a value for the ID of the first event generated in SSE scenarios.
|
Modifier and Type | Method and Description |
---|---|
int |
getBufferSizeElements()
Returns the size (in elements) of the buffers that will be created from the data-driver stream
before triggering the execution of the template (for each buffer).
|
org.reactivestreams.Publisher<Object> |
getDataStream()
Returns the reactive asynchronous object being wrapped, (perhaps) having been re-shaped into a
Publisher stream. |
long |
getFirstEventID()
Returns the first value to be used as an id in the case this response is rendered as SSE
(Server-Sent Events) with content type text/event-stream.
|
void |
setReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
Sets the
ReactiveAdapterRegistry used for converting (if necessary) the wrapped asynchronous
object into a Publisher . |
public static final int DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTS
Default buffer size to be applied if none is specified. Value = 100.
public static final long DEFAULT_FIRST_EVENT_ID
Default value for the first event ID (for SSE scenarios). Value = 0.
public ReactiveDataDriverContextVariable(Object dataStream)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream.
Buffer size will be set to DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTS
.
The specified dataStream must be adaptable to a Reactive Stream's
Publisher
by means of Spring's ReactiveAdapterRegistry
mechanism. If no
adapter has been registered for the type of the asynchronous object, and exception will be
thrown during lazy resolution.
Note the specified dataStream must be multi-valued.
Examples of supported implementations are Reactor's Flux
(but not
Mono
), and also RxJava's Observable
(but not Single).
dataStream
- the asynchronous object, which must be convertible to a multi-valued Publisher
by
means of Spring's ReactiveAdapterRegistry
.public ReactiveDataDriverContextVariable(Object dataStream, int dataStreamBufferSizeElements)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a buffer size.
The specified dataStream must be adaptable to a Reactive Streams
Publisher
by means of Spring's ReactiveAdapterRegistry
mechanism. If no
adapter has been registered for the type of the asynchronous object, and exception will be
thrown during lazy resolution.
Note the specified dataStream must be multi-valued.
Examples of supported implementations are Reactor's Flux
(but not
Mono
), and also RxJava's Observable
(but not Single).
dataStream
- the asynchronous object, which must be convertible to a multi-valued Publisher
by
means of Spring's ReactiveAdapterRegistry
.dataStreamBufferSizeElements
- the buffer size to be applied (in elements).public ReactiveDataDriverContextVariable(Object dataStream, int dataStreamBufferSizeElements, long firstEventID)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a buffer size and a value for the ID of the first event generated in SSE scenarios.
The specified dataStream must be adaptable to a Reactive Streams
Publisher
by means of Spring's ReactiveAdapterRegistry
mechanism. If no
adapter has been registered for the type of the asynchronous object, and exception will be
thrown during lazy resolution.
Note the specified dataStream must be multi-valued.
Examples of supported implementations are Reactor's Flux
(but not
Mono
), and also RxJava's Observable
(but not Single).
dataStream
- the asynchronous object, which must be convertible to a multi-valued Publisher
by
means of Spring's ReactiveAdapterRegistry
.dataStreamBufferSizeElements
- the buffer size to be applied (in elements).firstEventID
- the first value to be used as event ID in SSE scenarios (if applies).public final void setReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
Sets the ReactiveAdapterRegistry
used for converting (if necessary) the wrapped asynchronous
object into a Publisher
.
This method is transparently called before template execution in order
to initialize lazy context variables. It can also be called programmatically, but there is normally
no reason to do this. If not called at all, only Flux
will be allowed as valid type
for the wrapped data stream.
reactiveAdapterRegistry
- the reactive adapter registry.public org.reactivestreams.Publisher<Object> getDataStream()
IReactiveDataDriverContextVariable
Returns the reactive asynchronous object being wrapped, (perhaps) having been re-shaped into a
Publisher
stream.
getDataStream
in interface IReactiveDataDriverContextVariable
Publisher
).public final int getBufferSizeElements()
IReactiveDataDriverContextVariable
Returns the size (in elements) of the buffers that will be created from the data-driver stream before triggering the execution of the template (for each buffer).
Normally there is no need to execute the template engine and generate output for each element of data published by the data stream, so this buffering prevents Thymeleaf from executing more times than actually needed.
getBufferSizeElements
in interface IReactiveDataDriverContextVariable
public final long getFirstEventID()
IReactiveSSEDataDriverContextVariable
Returns the first value to be used as an id in the case this response is rendered as SSE (Server-Sent Events) with content type text/event-stream.
After the first generated events, subsequent ones will be assigned an id by incrementing this first value.
getFirstEventID
in interface IReactiveSSEDataDriverContextVariable
Copyright © 2017 The THYMELEAF team. All rights reserved.