Package org.atmosphere.cpr
Class AtmosphereSession
- java.lang.Object
-
- org.atmosphere.cpr.AtmosphereSession
-
public class AtmosphereSession extends java.lang.ObjectAn AtmosphereSession allow an application to keep track of the AtmosphereResource associated with a remote client. For example, with the long-polling transport, a newAtmosphereResourcewill be created every time a reconnect occurs. If an application has a reference to theAtmosphereResource, the object will become out of scope, or unusable, after the reconnection. To fix this problem, you can use this class to track and invoketryAcquire()()} oracquire()} in order to get theAtmosphereResource. AtmosphereResource are tracked using the list of associatedBroadcaster, e.g you must make sure the AtmosphereResource has calledBroadcaster.addAtmosphereResource(AtmosphereResource)once if you want this class to work.- Author:
- Jeanfrancois Arcand
-
-
Field Summary
Fields Modifier and Type Field Description protected BroadcasterListenerAdapterbroadcasterListenerprotected java.util.concurrent.Semaphorelatchprotected org.slf4j.Loggerloggerprotected Broadcaster[]relatedBroadcastersprotected java.util.concurrent.atomic.AtomicReference<AtmosphereResource>resourceprotected booleanusesLongPollingprotected java.lang.Stringuuid
-
Constructor Summary
Constructors Constructor Description AtmosphereSession(AtmosphereResource resource)Track the currentAtmosphereResourcewith all createdBroadcasterAtmosphereSession(AtmosphereResource r, Broadcaster... broadcasters)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AtmosphereResourceacquire()Retrieve theAtmosphereResourceassociated with this session.voidclose()AtmosphereResourcetryAcquire()Retrieve theAtmosphereResourceassociated with this session.AtmosphereResourcetryAcquire(int timeInSecond)Retrieve theAtmosphereResourceassociated with this session.java.lang.Stringuuid()
-
-
-
Field Detail
-
logger
protected final org.slf4j.Logger logger
-
resource
protected final java.util.concurrent.atomic.AtomicReference<AtmosphereResource> resource
-
uuid
protected java.lang.String uuid
-
latch
protected final java.util.concurrent.Semaphore latch
-
broadcasterListener
protected BroadcasterListenerAdapter broadcasterListener
-
relatedBroadcasters
protected Broadcaster[] relatedBroadcasters
-
usesLongPolling
protected final boolean usesLongPolling
-
-
Constructor Detail
-
AtmosphereSession
public AtmosphereSession(AtmosphereResource r, Broadcaster... broadcasters)
-
AtmosphereSession
public AtmosphereSession(AtmosphereResource resource)
Track the currentAtmosphereResourcewith all createdBroadcaster- Parameters:
resource- anAtmosphereResource
-
-
Method Detail
-
acquire
public AtmosphereResource acquire()
Retrieve theAtmosphereResourceassociated with this session. If there is noAtmosphereResourceassociated, return null.- Returns:
- an
AtmosphereResource
-
tryAcquire
public AtmosphereResource tryAcquire() throws java.lang.InterruptedException
Retrieve theAtmosphereResourceassociated with this session. If there is noAtmosphereResourceassociated, wait until theAtmosphereResourceis retrieved. This method will wait 60 seconds and then return.- Returns:
- an
AtmosphereResource - Throws:
java.lang.InterruptedException
-
tryAcquire
public AtmosphereResource tryAcquire(int timeInSecond) throws java.lang.InterruptedException
Retrieve theAtmosphereResourceassociated with this session. If there is noAtmosphereResourceassociated, wait until theAtmosphereResourceis retrieved.If the resource uses long polling as its transport, this method treats the resource as a single use connection and will make subsequent callers wait until the client reconnects and the
broadcasterListener'sBroadcasterListenerAdapter.onAddAtmosphereResource(org.atmosphere.cpr.Broadcaster, org.atmosphere.cpr.AtmosphereResource)method gets called again.WARNING: Use this method with long polling only if you intend to broadcast to the returned resource. If no broadcast is made, the client won't have to reconnect, the resource won't get re-added, and any subsequent calls will have to wait until the timeout is reached.
- Parameters:
timeInSecond- The timeToWait before continuing the execution- Returns:
- an
AtmosphereResourceornullif the resource was not set and it didn't get set during the timeout - Throws:
java.lang.InterruptedException
-
close
public void close()
-
uuid
public java.lang.String uuid()
-
-