Class ReactiveDataDriverContextVariable

  • All Implemented Interfaces:
    IReactiveDataDriverContextVariable, IReactiveSSEDataDriverContextVariable

    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.

    Since:
    3.0.3
    Author:
    Daniel Fernández
    See Also:
    IReactiveDataDriverContextVariable, IReactiveSSEDataDriverContextVariable
    • Constructor Summary

      Constructors 
      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.
    • Field Detail

      • DEFAULT_DATA_DRIVER_BUFFER_SIZE_ELEMENTS

        public 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_ID

        public 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

      • ReactiveDataDriverContextVariable

        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).

        Parameters:
        dataStream - the asynchronous object, which must be convertible to a multi-valued Publisher by means of Spring's ReactiveAdapterRegistry.
      • ReactiveDataDriverContextVariable

        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).

        Parameters:
        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).
      • ReactiveDataDriverContextVariable

        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).

        Parameters:
        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.
        Since:
        3.0.8
      • ReactiveDataDriverContextVariable

        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).

        Parameters:
        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).
        Since:
        3.0.4
      • ReactiveDataDriverContextVariable

        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).

        Parameters:
        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).
        Since:
        3.0.8
    • Method Detail

      • getDataStream

        public org.reactivestreams.Publisher<Object> getDataStream​(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
        Description copied from interface: IReactiveDataDriverContextVariable

        Returns the reactive asynchronous object being wrapped, (perhaps) having been re-shaped into a Publisher stream.

        Specified by:
        getDataStream in 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).
      • getBufferSizeElements

        public final int getBufferSizeElements()
        Description copied from interface: 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.

        Specified by:
        getBufferSizeElements in interface IReactiveDataDriverContextVariable
        Returns:
        the size (in elements) of the buffers to be created for each (partial) execution of the engine.
      • getSseEventsPrefix

        public final String getSseEventsPrefix()
        Description copied from interface: 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.

        Specified by:
        getSseEventsPrefix in interface IReactiveSSEDataDriverContextVariable
        Returns:
        the prefix to be applied to event names and IDs, or null if no prefix has been set.
      • getSseEventsFirstID

        public final long getSseEventsFirstID()
        Description copied from interface: 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.

        Specified by:
        getSseEventsFirstID in interface IReactiveSSEDataDriverContextVariable
        Returns:
        the first value to be used for returning the data driver variable values as SSE events.