Class SessionSerializer

java.lang.Object
com.vaadin.kubernetes.starter.sessiontracker.SessionSerializer
All Implemented Interfaces:
EventListener, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextClosedEvent>

public class SessionSerializer extends Object implements org.springframework.context.ApplicationListener<org.springframework.context.event.ContextClosedEvent>
Component responsible for replicating HTTP session attributes to a distributed storage. HTTP session attributes are serialized and deserialized by SessionSerializer, using Java Serialization specifications. Transient fields of serialized objects are inspected by a pluggable TransientHandler component to gather metadata that is stored along the session attributes and then used during deserialization to populate the fields on the new instances. When serializing an HTTP session, SessionSerializer marks the current session as pending before starting an async serialization process. Pending state is hold by the backend connector and removed once the data has been written on the distributed storage. Concurrent attempts are ignored until the pending state is cleared. The operation is safe and will not lose any Vaadin related data (UI or VaadinSession attributes) because the serializer is always working on the same VaadinSession instance. However, it may potentially be possible to lose attributes that are directly added on the HTTP session, because the asynchronous job does not work directly on the HttpSession, but on a map that references the original attributes. For the same reason, the serializer may persist attributes that a request has removed during the pending state. VaadinSession data integrity is granted by an optimistic/pessimistic handling, based on VaadinSession lock timestamp. Session serialization process works as following:
  • it first checks that VaadinSession is currently unlocked. If so it performs serialization
  • if VaadinSession is locked it schedules another attempt
  • Once data serialization is completed, it checks if the VaadinSession has been locked and unlocked in the meanwhile
  • If so, it discards serialized data and schedules another attempt
  • If after a timeout of 30 seconds it has not been possible to complete the serialization without VaadinSession locks/unlocks it falls back to a pessimistic approach
  • Pessimistic approach locks the VaadinSession during the serialization
  • Finally serialized data is written to the distributes storage
In case of a server shutdown, it waits for pending session serializations to complete.
  • Constructor Details

    • SessionSerializer

      public SessionSerializer(BackendConnector backendConnector, TransientHandler transientHandler, SessionExpirationPolicy sessionExpirationPolicy, SessionSerializationCallback sessionSerializationCallback, SerializationStreamFactory serializationStreamFactory)
      Creates a new SessionSerializer.
      Parameters:
      backendConnector - backend connector to store serialized data on distributed storage.
      transientHandler - handler to inspect and inject transient fields.
      sessionSerializationCallback - callbacks for successful serialization and deserialization or when an error happens
    • SessionSerializer

      public SessionSerializer(BackendConnector backendConnector, BiFunction<String,String,TransientHandler> transientHandlerProvider, SessionExpirationPolicy sessionExpirationPolicy, SessionSerializationCallback sessionSerializationCallback, SerializationStreamFactory serializationStreamFactory)
      Creates a new SessionSerializer.

      The TransientHandler provider is called when serialization process start, providing session ID and cluster key, to allow the implementor to track or provide actions based on the current processing.

      This constructor is basically an internal API, meant to be used only by the serialization debug tool.

      For internal use only,
      Parameters:
      backendConnector - backend connector to store serialized data on distributed storage.
      transientHandlerProvider - provides handler to inspect and inject transient fields.
      sessionSerializationCallback - callbacks for successful serialization and deserialization or when an error happens
  • Method Details

    • setInjectableFilter

      public void setInjectableFilter(Predicate<Class<?>> injectableFilter)
      Provide a filter to restrict classes suitable for transients field inspection. If null all classes are inspected. This is the default behavior.
      Parameters:
      injectableFilter - a filter to restrict classes suitable for transients field * inspection.
    • serialize

      public void serialize(jakarta.servlet.http.HttpSession session)
      Serializes the given HTTP session and stores data on a distributed storage.
      Parameters:
      session - the HTTP session.
    • serialize

      public void serialize(com.vaadin.flow.server.WrappedSession session)
      Serializes the given Vaadin Wrapped session and stores data on a distributed storage.
      Parameters:
      session - the Vaadin Wrapped session.
    • deserialize

      public void deserialize(SessionInfo sessionInfo, jakarta.servlet.http.HttpSession session) throws Exception
      Deserializes binary data from the distributed storage into the given HTTP session.
      Parameters:
      sessionInfo - session data from distributed storage.
      session - the HTTP session
      Throws:
      Exception - any of the deserialization related exceptions.
    • onApplicationEvent

      public void onApplicationEvent(org.springframework.context.event.ContextClosedEvent event)
      Specified by:
      onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextClosedEvent>