public class Session extends Object
A Session represents a single execution of a SpeechletV2 by a user.
Multiple executions of a SpeechletV2 are tied to separate Sessions with different
identifiers. If the user explicitly exits the SpeechletV2 and restarts it immediately
after, the new execution of the SpeechletV2 will be associated with a new
Session, regardless of how much time elapsed between the two runs.
A Session is tied to the specific user registered to the device initiating the
SpeechletV2 Session. A Session provides state that is maintained between
the multiple invocations of a SpeechletV2.
The attributes of a Session are simple key-value pairs that are maintained between
invocations. You can use them to store lightweight data. They are comparable to session cookies
that you would use in a system where the client is a browser. Ending the session by closing the
browser would clear those cookies.
Attributes can be modified within a SpeechletV2 invocation, but not after the
SpeechletV2 method has returned. The modified attributes are sent back with the response
and kept for the next SpeechletV2 invocation. The one exception is the SpeechletV2.onSessionEnded(SpeechletRequestEnvelope), as the
Session and its attributes are discarded immediately after.
If the SpeechletV2 throws an exception, modifications to the attributes up to that point
are applied, even if the request was not processed successfully.
Because a SpeechletV2 is a cloud-based service, the life-cycle of the actual session
object passed in the SpeechletV2 methods is unrelated to the Alexa skill execution as
experienced by the user interacting with the speech-enabled device. A SpeechletV2
receives a new Session instance for each SpeechletV2 invocation. The identifier
of the Session is what uniquely identifies it between invocations. References to a
Session should never be stored as its contents is invalid after returning from a
SpeechletV2 method.
SpeechletV2,
SpeechletException| Modifier and Type | Class and Description |
|---|---|
static class |
Session.Builder
Builder used to construct a new
Session. |
| Modifier and Type | Method and Description |
|---|---|
static Session.Builder |
builder()
Returns a new builder instance used to construct a new
Session. |
Application |
getApplication()
Returns the application.
|
Object |
getAttribute(String name)
Returns the attribute associated with the provided name.
|
Map<String,Object> |
getAttributes()
Returns all the session attributes.
|
String |
getSessionId()
Returns the session Id.
|
User |
getUser()
Returns the user associated with this session.
|
boolean |
isNew()
Return whether this is a new session, meaning the current request is the initial request
starting the speechlet.
|
void |
removeAttribute(String name)
Remove the attribute associated with the provided name.
|
void |
setAttribute(String name,
Object value)
Add or modify the attribute with the provided name.
|
public static Session.Builder builder()
Session.public boolean isNew()
true if this is a new session; otherwise, false if this is a
subsequent request within an existing sessionpublic String getSessionId()
public Application getApplication()
public Map<String,Object> getAttributes()
public Object getAttribute(String name)
name - the name of the attribute to retrievenullpublic void setAttribute(String name, Object value)
name - the name of the attribute to setvalue - the new value for the attributepublic void removeAttribute(String name)
name - the name of the attribute to removepublic User getUser()
Copyright © 2016. All rights reserved.