Skip to content

Navigation Menu

Sign in
Sign up

Understanding AtmosphereResource

jfarcand edited this page Feb 18, 2026 · 22 revisions

Understanding AtmosphereResource

Advanced topic. Most applications use @ManagedService with @Inject and don't need to interact with AtmosphereResource directly.

An AtmosphereResource represents a single client connection. It wraps the underlying HTTP request/response and provides methods for suspending, resuming, and writing to the connection.

Accessing AtmosphereResource

Via Injection (recommended)

@ManagedService(path = "/chat")
public class Chat {
 @Inject
 private AtmosphereResource r;
 @Ready
 public void onReady() {
 String uuid = r.uuid(); // Unique connection ID
 String transport = r.transport().name(); // WEBSOCKET, SSE, LONG_POLLING, etc.
 }
}

Via Method Parameter

@Ready
public void onReady(AtmosphereResource r) {
 // r is the current resource
}

Key Methods

r.uuid(); // Unique connection identifier
r.transport(); // Transport type (WEBSOCKET, SSE, LONG_POLLING, etc.)
r.suspend(); // Suspend the connection (keep it open)
r.resume(); // Resume and complete the response
r.close(); // Close the connection
r.getRequest(); // AtmosphereRequest (wraps HttpServletRequest)
r.getResponse(); // AtmosphereResponse (wraps HttpServletResponse)
r.getAtmosphereConfig(); // Framework configuration
r.getBroadcaster(); // The Broadcaster this resource is attached to
r.setBroadcaster(b); // Change broadcaster
r.addEventListener(listener); // Add lifecycle event listener
r.session(); // Get/create AtmosphereResourceSession

AtmosphereResourceEvent

Injected or passed to @Disconnect handlers:

@Inject
private AtmosphereResourceEvent event;
@Disconnect
public void onDisconnect() {
 event.isCancelled(); // Unexpected disconnect (e.g., network error)
 event.isClosedByClient(); // Client closed intentionally
 event.isClosedByApplication(); // Server closed the connection
 event.getResource(); // The AtmosphereResource
 event.getMessage(); // Last message (if any)
}

Event Listeners

For programmatic lifecycle tracking (alternative to annotations):

r.addEventListener(new AtmosphereResourceEventListenerAdapter() {
 @Override
 public void onSuspend(AtmosphereResourceEvent event) {
 // Connection suspended
 }
 @Override
 public void onDisconnect(AtmosphereResourceEvent event) {
 // Client disconnected
 }
 @Override
 public void onBroadcast(AtmosphereResourceEvent event) {
 // Message broadcast to this resource
 }
 @Override
 public void onHeartbeat(AtmosphereResourceEvent event) {
 // Heartbeat received
 }
});

Convenience inner classes are available for single-event listeners:

r.addEventListener(new AtmosphereResourceEventListenerAdapter.OnDisconnect() {
 @Override
 public void onDisconnect(AtmosphereResourceEvent event) {
 // Only handle disconnect
 }
});

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /