Class MultiplexedChannelHandler<ConnectionPayload,​MessagePayload>

  • Type Parameters:
    ConnectionPayload - type of the payload for the connection
    MessagePayload - type of the payload for each message

    public abstract class MultiplexedChannelHandler<ConnectionPayload,​MessagePayload>
    extends ChannelHandler
    Implements a ChannelHandler on top of a single subscription and multiplexes all reads on top of it.

    This abstract handler takes care of forwarding the connection and message events of a single connection to multiple readers and writers. One needs to:

    Author:
    carcassi
    • Constructor Detail

      • MultiplexedChannelHandler

        public MultiplexedChannelHandler​(String channelName)
        Creates a new channel handler.
        Parameters:
        channelName - the name of the channel this handler will be responsible of
      • MultiplexedChannelHandler

        public MultiplexedChannelHandler​(String channelName,
                                         boolean readOnly)
        Creates a new channel handler.
        Parameters:
        channelName - the name of the channel this handler will be responsible of
        readOnly - whether the channel is read-only
    • Method Detail

      • reportExceptionToAllReadersAndWriters

        protected final void reportExceptionToAllReadersAndWriters​(Exception ex)
        Notifies all readers and writers of an error condition.
        Parameters:
        ex - the exception to notify
      • reportExceptionToAllWriters

        protected final void reportExceptionToAllWriters​(Exception ex)
        Notifies all writers of an error condition.
        Parameters:
        ex - the exception to notify
      • getConnectionPayload

        protected final ConnectionPayload getConnectionPayload()
        The last processes connection payload.
        Returns:
        the connection payload or null
      • getLastMessagePayload

        protected final MessagePayload getLastMessagePayload()
        The last processed message payload.
        Returns:
        the message payload or null
      • processConnection

        protected final void processConnection​(ConnectionPayload connectionPayload)
        Process the next connection payload. This should be called whenever the connection state has changed.
        Parameters:
        connectionPayload - connection payload; not null
      • findTypeAdapter

        protected DataSourceTypeAdapter<ConnectionPayload,​MessagePayload> findTypeAdapter​(ReadCollector<?,​?> cache,
                                                                                                ConnectionPayload connection)
        Finds the right adapter to use for the particular cache given the information of the channels in the connection payload. By overriding this method a datasource can implement their own matching logic. One can use the logic provided in DataSourceTypeSupport as a good first implementation.
        Parameters:
        cache - the cache that will store the data
        connection - the connection payload
        Returns:
        the matched type adapter
      • getUsageCounter

        public int getUsageCounter()
        Description copied from class: ChannelHandler
        Returns how many readers or writers are open on this channel.
        Specified by:
        getUsageCounter in class ChannelHandler
        Returns:
        the number of open readers and writers
      • getReadUsageCounter

        public int getReadUsageCounter()
        Description copied from class: ChannelHandler
        Returns how many readers are open on this channel.
        Specified by:
        getReadUsageCounter in class ChannelHandler
        Returns:
        the number of open readers
      • getWriteUsageCounter

        public int getWriteUsageCounter()
        Description copied from class: ChannelHandler
        Returns how many writers are open on this channel.
        Specified by:
        getWriteUsageCounter in class ChannelHandler
        Returns:
        the number of open writers
      • addReader

        protected void addReader​(ReadCollector subscription)
        Description copied from class: ChannelHandler
        Starts sending read notification to the given collector.
        Specified by:
        addReader in class ChannelHandler
        Parameters:
        subscription - the data collector
      • removeReader

        protected void removeReader​(ReadCollector subscription)
        Description copied from class: ChannelHandler
        Stops sending read notification to the given collector.
        Specified by:
        removeReader in class ChannelHandler
        Parameters:
        subscription - the data collector
      • addWriter

        protected void addWriter​(WriteCollector subscription)
        Description copied from class: ChannelHandler
        Starts sending/receiving write notification to the given collector.
        Specified by:
        addWriter in class ChannelHandler
        Parameters:
        subscription - the data collector
      • write

        protected void write​(Object newValue)
        Write the value. Override this method to implement writes synchronously. Simply return if the write was successful or throw an exception if it wasn't. The exception is propagated up to the client code, so the error message should be short but descriptive.

        To implement writes, either this method or write(java.lang.Object) should be overriden.

        Parameters:
        newValue - the new value to write.
      • removeWriter

        protected void removeWriter​(WriteCollector subscription)
        Description copied from class: ChannelHandler
        Stops sending/receiving write notification to the given collector.
        Specified by:
        removeWriter in class ChannelHandler
        Parameters:
        subscription - the data collector
      • resetMessage

        protected final void resetMessage()
        Resets the last message to null. This can be used to invalidate the last message without triggering a notification. It is useful when a reconnect should behave as the first connection.
      • processMessage

        protected final void processMessage​(MessagePayload payload)
        Process the payload for this channel. This should be called whenever a new value needs to be processed. The handler will take care of using the correct DataSourceTypeAdapter for each read monitor that was setup.
        Parameters:
        payload - the payload of for this type of channel
      • saveMessageAfterDisconnect

        protected boolean saveMessageAfterDisconnect()
        Signals whether the last message received after the disconnect should be kept so that it is available at reconnect.

        By default, the message is discarded so that no memory is kept allocated.

        Returns:
        true if the message should be kept
      • connect

        protected abstract void connect()
        Used by the handler to open the connection. This is called whenever the first read or write request is made.
      • disconnect

        protected abstract void disconnect()
        Used by the handler to close the connection. This is called whenever the last reader or writer is de-registered.
      • isConnected

        protected boolean isConnected​(ConnectionPayload payload)
        Determines from the payload whether the channel is connected or not.

        By default, this uses the usage counter to determine whether it's connected or not. One should override this to use the actual connection payload to check whether the actual protocol connection has been established.

        Parameters:
        payload - the connection payload
        Returns:
        true if connected
      • isWriteConnected

        protected boolean isWriteConnected​(ConnectionPayload payload)
        Determines from the payload whether the channel can be written to.

        By default, this always return false. One should override this if it's implementing a write-able data source.

        Parameters:
        payload - connection payload; not null
        Returns:
        true if ready for writes
      • isConnected

        public final boolean isConnected()
        Description copied from class: ChannelHandler
        Returns true if it is connected.
        Specified by:
        isConnected in class ChannelHandler
        Returns:
        true if underlying channel is connected
      • isWriteConnected

        public final boolean isWriteConnected()
        Returns true if it is channel can be written to.
        Returns:
        true if underlying channel is write ready
      • setProcessMessageOnDisconnect

        protected final void setProcessMessageOnDisconnect​(boolean processMessageOnDisconnect)
        Determines whether processConnection(java.lang.Object) should trigger processMessage(java.lang.Object) with the same (non-null) payload in case the channel has been disconnected. Default is true.
        Parameters:
        processMessageOnDisconnect - whether to process the message on disconnect
      • setProcessMessageOnReconnect

        protected final void setProcessMessageOnReconnect​(boolean processMessageOnReconnect)
        Determines whether processConnection(java.lang.Object) should trigger processMessage(java.lang.Object) with the same (non-null) payload in case the channel has reconnected. Default is true.
        Parameters:
        processMessageOnReconnect - whether to process the message on disconnect