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 if such adapter registry has been set in the context
(see SpringWebFluxContext.getReactiveAdapterRegistry()
).
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 sseEventsFirstID)
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.
|
ReactiveDataDriverContextVariable(Object dataStream,
int dataStreamBufferSizeElements,
String sseEventsPrefix)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a
buffer size and a prefix for all the names and IDs of events generated from a specific SSE stream.
|
ReactiveDataDriverContextVariable(Object dataStream,
int dataStreamBufferSizeElements,
String sseEventsPrefix,
long sseEventsFirstID)
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 and a prefix for all
the names and IDs of events generated from a specific SSE stream.
|
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(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
Returns the reactive asynchronous object being wrapped, (perhaps) having been re-shaped into a
Publisher stream. |
long |
getSseEventsFirstID()
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.
|
String |
getSseEventsPrefix()
Returns the (optional) prefix to be used for SSE event names and IDs.
|
public static final int DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTS
Default buffer size to be applied if none is specified. Value = 10.
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. If no adapter registry has been set into the context
(see SpringWebFluxContext.getReactiveAdapterRegistry()
) this data stream must mandatorily
be a Flux
.
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 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. If no adapter registry has been set into the context
(see SpringWebFluxContext.getReactiveAdapterRegistry()
) this data stream must mandatorily
be a Flux
.
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, String sseEventsPrefix)
Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a buffer size and a prefix for all the names and IDs of events generated from a specific SSE stream.
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. If no adapter registry has been set into the context
(see SpringWebFluxContext.getReactiveAdapterRegistry()
) this data stream must mandatorily
be a Flux
.
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).sseEventsPrefix
- the prefix to be used for event names and IDs, so that events coming from a specific
SSE stream can be identified (if applies). Can be null.public ReactiveDataDriverContextVariable(Object dataStream, int dataStreamBufferSizeElements, long sseEventsFirstID)
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 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. If no adapter registry has been set into the context
(see SpringWebFluxContext.getReactiveAdapterRegistry()
) this data stream must mandatorily
be a Flux
.
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).sseEventsFirstID
- the first value to be used as event ID in SSE scenarios (if applies).public ReactiveDataDriverContextVariable(Object dataStream, int dataStreamBufferSizeElements, String sseEventsPrefix, long sseEventsFirstID)
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 and a prefix for all the names and IDs of events generated from a specific SSE stream.
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. If no adapter registry has been set into the context
(see SpringWebFluxContext.getReactiveAdapterRegistry()
) this data stream must mandatorily
be a Flux
.
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).sseEventsPrefix
- the prefix to be used for event names and IDs, so that events coming from a specific
SSE stream can be identified (if applies). Can be null.sseEventsFirstID
- the first value to be used as event ID in SSE scenarios (if applies).public org.reactivestreams.Publisher<Object> getDataStream(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
IReactiveDataDriverContextVariable
Returns the reactive asynchronous object being wrapped, (perhaps) having been re-shaped into a
Publisher
stream.
getDataStream
in interface IReactiveDataDriverContextVariable
reactiveAdapterRegistry
- the Spring reactive adapter registry that should be used in order to transform
other reactive implementations (RxJava, etc.) into
Flux
. Can be null (no adaptations will be
available).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 String getSseEventsPrefix()
IReactiveSSEDataDriverContextVariable
Returns the (optional) prefix to be used for SSE event names and IDs.
Using a prefix for SSE events can be useful in scenarios such as UI composition, in which streams of markup events coming from different sources (e.g. different parts of a page) can be sent to the browser combined in a single EventSource SSE stream. That way client JavaScript code will be able to identify which part of the page the event belongs to by means of its prefix. Also, combining several (prefixed) SSE streams into one can also serve to overcome limitations in the amount of concurrent active EventSource allowed.
getSseEventsPrefix
in interface IReactiveSSEDataDriverContextVariable
public final long getSseEventsFirstID()
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.
getSseEventsFirstID
in interface IReactiveSSEDataDriverContextVariable
Copyright © 2017 The THYMELEAF team. All rights reserved.