Class ReactiveDataDriverContextVariable
- Object
- 
- org.thymeleaf.spring5.context.webflux.ReactiveDataDriverContextVariable
 
- 
- All Implemented Interfaces:
- IReactiveDataDriverContextVariable,- IReactiveSSEDataDriverContextVariable
 
 public class ReactiveDataDriverContextVariable extends Object implements IReactiveSSEDataDriverContextVariable Basic implementation of the IReactiveDataDriverContextVariableinterface, including also the extensions specified inIReactiveSSEDataDriverContextVariable.The reactive data stream wrapped by this class will usually have the shape of an implementation of the Publisherinterface, such asFlux. But other types of reactive artifacts are supported thanks to Spring'sReactiveAdapterRegistrymechanism if such adapter registry has been set in the context (seeSpringWebFluxContext.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 Fluxobject will be considered multi-valued even if it publishes none or just one result, whereas aMonoobject 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. - Since:
- 3.0.3
- Author:
- Daniel Fernández
- See Also:
- IReactiveDataDriverContextVariable,- IReactiveSSEDataDriverContextVariable
 
- 
- 
Field SummaryFields Modifier and Type Field Description static intDEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTSDefault buffer size to be applied if none is specified.static longDEFAULT_FIRST_EVENT_IDDefault value for the first event ID (for SSE scenarios).
 - 
Constructor SummaryConstructors Constructor 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.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetBufferSizeElements()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 aPublisherstream.longgetSseEventsFirstID()Returns the first value to be used as anidin the case this response is rendered as SSE (Server-Sent Events) with content typetext/event-stream.StringgetSseEventsPrefix()Returns the (optional) prefix to be used for SSE event names and IDs.
 
- 
- 
- 
Field Detail- 
DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTSpublic static final int DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTS Default buffer size to be applied if none is specified. Value = 10.- See Also:
- Constant Field Values
 
 - 
DEFAULT_FIRST_EVENT_IDpublic static final long DEFAULT_FIRST_EVENT_ID Default value for the first event ID (for SSE scenarios). Value = 0.- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
ReactiveDataDriverContextVariablepublic 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 dataStreammust be adaptable to a Reactive Stream'sPublisherby means of Spring'sReactiveAdapterRegistrymechanism. 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 (seeSpringWebFluxContext.getReactiveAdapterRegistry()) this data stream must mandatorily be aFlux.Note the specified dataStreammust be multi-valued.Examples of supported implementations are Reactor's Flux(but notMono), and also RxJava'sObservable(but notSingle).- Parameters:
- dataStream- the asynchronous object, which must be convertible to a multi-valued- Publisherby means of Spring's- ReactiveAdapterRegistry.
 
 - 
ReactiveDataDriverContextVariablepublic ReactiveDataDriverContextVariable(Object dataStream, int dataStreamBufferSizeElements) Creates a new lazy context variable, wrapping a reactive asynchronous data stream and specifying a buffer size. The specified dataStreammust be adaptable to a Reactive Stream'sPublisherby means of Spring'sReactiveAdapterRegistrymechanism. 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 (seeSpringWebFluxContext.getReactiveAdapterRegistry()) this data stream must mandatorily be aFlux.Note the specified dataStreammust be multi-valued.Examples of supported implementations are Reactor's Flux(but notMono), and also RxJava'sObservable(but notSingle).- Parameters:
- dataStream- the asynchronous object, which must be convertible to a multi-valued- Publisherby means of Spring's- ReactiveAdapterRegistry.
- dataStreamBufferSizeElements- the buffer size to be applied (in elements).
 
 - 
ReactiveDataDriverContextVariablepublic 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 dataStreammust be adaptable to a Reactive Stream'sPublisherby means of Spring'sReactiveAdapterRegistrymechanism. 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 (seeSpringWebFluxContext.getReactiveAdapterRegistry()) this data stream must mandatorily be aFlux.Note the specified dataStreammust be multi-valued.Examples of supported implementations are Reactor's Flux(but notMono), and also RxJava'sObservable(but notSingle).- Parameters:
- dataStream- the asynchronous object, which must be convertible to a multi-valued- Publisherby 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.
- Since:
- 3.0.8
 
 - 
ReactiveDataDriverContextVariablepublic 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 dataStreammust be adaptable to a Reactive Stream'sPublisherby means of Spring'sReactiveAdapterRegistrymechanism. 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 (seeSpringWebFluxContext.getReactiveAdapterRegistry()) this data stream must mandatorily be aFlux.Note the specified dataStreammust be multi-valued.Examples of supported implementations are Reactor's Flux(but notMono), and also RxJava'sObservable(but notSingle).- Parameters:
- dataStream- the asynchronous object, which must be convertible to a multi-valued- Publisherby 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).
- Since:
- 3.0.4
 
 - 
ReactiveDataDriverContextVariablepublic 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 dataStreammust be adaptable to a Reactive Stream'sPublisherby means of Spring'sReactiveAdapterRegistrymechanism. 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 (seeSpringWebFluxContext.getReactiveAdapterRegistry()) this data stream must mandatorily be aFlux.Note the specified dataStreammust be multi-valued.Examples of supported implementations are Reactor's Flux(but notMono), and also RxJava'sObservable(but notSingle).- Parameters:
- dataStream- the asynchronous object, which must be convertible to a multi-valued- Publisherby 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).
- Since:
- 3.0.8
 
 
- 
 - 
Method Detail- 
getDataStreampublic org.reactivestreams.Publisher<Object> getDataStream(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry) Description copied from interface:IReactiveDataDriverContextVariableReturns the reactive asynchronous object being wrapped, (perhaps) having been re-shaped into a Publisherstream.- Specified by:
- getDataStreamin interface- IReactiveDataDriverContextVariable
- Parameters:
- 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).
- Returns:
- the asynchronous object (as a Publisher).
 
 - 
getBufferSizeElementspublic final int getBufferSizeElements() Description copied from interface:IReactiveDataDriverContextVariableReturns 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. - Specified by:
- getBufferSizeElementsin interface- IReactiveDataDriverContextVariable
- Returns:
- the size (in elements) of the buffers to be created for each (partial) execution of the engine.
 
 - 
getSseEventsPrefixpublic final String getSseEventsPrefix() Description copied from interface:IReactiveSSEDataDriverContextVariableReturns 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 EventSourceSSE 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 activeEventSourceallowed.- Specified by:
- getSseEventsPrefixin interface- IReactiveSSEDataDriverContextVariable
- Returns:
- the prefix to be applied to event names and IDs, or nullif no prefix has been set.
 
 - 
getSseEventsFirstIDpublic final long getSseEventsFirstID() Description copied from interface:IReactiveSSEDataDriverContextVariableReturns the first value to be used as an idin the case this response is rendered as SSE (Server-Sent Events) with content typetext/event-stream.After the first generated events, subsequent ones will be assigned an idby incrementing this first value.- Specified by:
- getSseEventsFirstIDin interface- IReactiveSSEDataDriverContextVariable
- Returns:
- the first value to be used for returning the data driver variable values as SSE events.
 
 
- 
 
-