-
public class SessionManagerTracks the current
{@code #session_id}using Amplitude's session gap rule. The id is a UUID v7 string. Its first 48 bits are the session start time in milliseconds. A new id is assigned when the gap since the last event reaches the configured timeout, or when the account identity changes. App foreground/background transitions do not affect the event gap.
-
-
Field Summary
Fields Modifier and Type Field Description public final static longDEFAULT_TIMEOUT_MILLISprivate StringsessionId
-
Constructor Summary
Constructors Constructor Description SessionManager(SessionStore store)Creates a session manager.
-
Method Summary
Modifier and Type Method Description synchronized StringgetSessionId()static SessionManagergetInstance(String instanceKey, SessionStore store)Returns the session manager for an SDK instance. synchronized StringonEvent(long now, long timeoutMillis)Assigns or extends the session for an event processed at {@code now}.synchronized voidonEnterBackground(long now)Handles an app background transition without changing session state.Lifecycle transitions are not events and therefore must not extend the session timeout. synchronized StringonEnterForeground(long now, long timeoutMillis)Handles an app foreground transition without changing session state. synchronized voidonAccountChanged()Invalidates the current session after the account identity changes.The next event starts a new session; no session is created by the account operation itself. -
-
Constructor Detail
-
SessionManager
SessionManager(SessionStore store)
Creates a session manager.- Parameters:
store- persistence, or null to keep state in memory only
-
-
Method Detail
-
getSessionId
synchronized String getSessionId()
-
getInstance
static SessionManager getInstance(String instanceKey, SessionStore store)
Returns the session manager for an SDK instance. The first caller supplies persistence;later callers with the same key share that instance.
- Parameters:
instanceKey- SDK instance namestore- persistence used when this key has no manager yet; ignored when one already exists
-
onEvent
synchronized String onEvent(long now, long timeoutMillis)
Assigns or extends the session for an event processed at
{@code now}.- Parameters:
now- event processing time in epoch millisecondstimeoutMillis- session timeout; values <= 0 use DEFAULT_TIMEOUT_MILLIS
-
onEnterBackground
synchronized void onEnterBackground(long now)
Handles an app background transition without changing session state.Lifecycle transitions are not events and therefore must not extend the session timeout.
- Parameters:
now- background time in epoch milliseconds; ignored for session timing
-
onEnterForeground
synchronized String onEnterForeground(long now, long timeoutMillis)
Handles an app foreground transition without changing session state.A timed-out session is rotated only when the next event is processed by onEvent.
- Parameters:
now- foreground time in epoch milliseconds; ignored for session timingtimeoutMillis- session timeout; ignored until the next event
-
onAccountChanged
synchronized void onAccountChanged()
Invalidates the current session after the account identity changes.The next event starts a new session; no session is created by the account operation itself.
-
-
-
-