Class SessionSerializer
java.lang.Object
com.vaadin.kubernetes.starter.sessiontracker.SessionSerializer
- All Implemented Interfaces:
com.vaadin.flow.server.VaadinServiceInitListener,Serializable,EventListener,org.springframework.context.Lifecycle,org.springframework.context.Phased,org.springframework.context.SmartLifecycle
public class SessionSerializer
extends Object
implements com.vaadin.flow.server.VaadinServiceInitListener, org.springframework.context.SmartLifecycle
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
SmartLifecycle to using the default
phase to be notified early about shutdown, so it can complete its work before
other service and beans like the BackendConnector are stopped.- See Also:
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionSessionSerializer(BackendConnector backendConnector, TransientHandler transientHandler, SessionExpirationPolicy sessionExpirationPolicy, SessionSerializationCallback sessionSerializationCallback, SerializationStreamFactory serializationStreamFactory, SerializationProperties serializationProperties) Creates a newSessionSerializer.SessionSerializer(BackendConnector backendConnector, BiFunction<String, String, TransientHandler> transientHandlerProvider, SessionExpirationPolicy sessionExpirationPolicy, SessionSerializationCallback sessionSerializationCallback, SerializationStreamFactory serializationStreamFactory, SerializationProperties serializationProperties) Creates a newSessionSerializer. -
Method Summary
Modifier and TypeMethodDescriptionvoiddeserialize(SessionInfo sessionInfo, jakarta.servlet.http.HttpSession session) Deserializes binary data from the distributed storage into the given HTTP session.booleanbooleanvoidserialize(com.vaadin.flow.server.WrappedSession session) Serializes the given Vaadin Wrapped session and stores data on a distributed storage.voidserialize(jakarta.servlet.http.HttpSession session) Serializes the given HTTP session and stores data on a distributed storage.voidserviceInit(com.vaadin.flow.server.ServiceInitEvent event) voidsetInjectableFilter(Predicate<Class<?>> injectableFilter) Provide a filter to restrict classes suitable for transients field inspection.voidstart()voidstop()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.SmartLifecycle
getPhase, isAutoStartup, stop
-
Constructor Details
-
SessionSerializer
public SessionSerializer(BackendConnector backendConnector, TransientHandler transientHandler, SessionExpirationPolicy sessionExpirationPolicy, SessionSerializationCallback sessionSerializationCallback, SerializationStreamFactory serializationStreamFactory, SerializationProperties serializationProperties) Creates a newSessionSerializer.- 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 happensserializationProperties- the serialization properties
-
SessionSerializer
public SessionSerializer(BackendConnector backendConnector, BiFunction<String, String, TransientHandler> transientHandlerProvider, SessionExpirationPolicy sessionExpirationPolicy, SessionSerializationCallback sessionSerializationCallback, SerializationStreamFactory serializationStreamFactory, SerializationProperties serializationProperties) Creates a newSessionSerializer.
TheTransientHandlerprovider is called when serialization process start, providingsession IDandcluster 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 happensserializationProperties- the serialization properties
-
-
Method Details
-
setInjectableFilter
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.
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceorg.springframework.context.Lifecycle
-
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.
-
start
public void start()- Specified by:
startin interfaceorg.springframework.context.Lifecycle
-
isPauseable
public boolean isPauseable()- Specified by:
isPauseablein interfaceorg.springframework.context.SmartLifecycle
-
stop
public void stop()- Specified by:
stopin interfaceorg.springframework.context.Lifecycle
-
serviceInit
public void serviceInit(com.vaadin.flow.server.ServiceInitEvent event) - Specified by:
serviceInitin interfacecom.vaadin.flow.server.VaadinServiceInitListener
-