0
# Core Framework
1
2
Central framework management including configuration, resource handling, and the primary AtmosphereResource interface for managing suspended connections and broadcasting.
3
4
## Capabilities
5
6
### AtmosphereFramework
7
8
Central framework entry point that manages the entire Atmosphere runtime, including configuration, handler registration, and lifecycle management.
9
10
```java { .api }
11
/**
12
* Central framework entry point for Atmosphere runtime
13
*/
14
public class AtmosphereFramework {
15
/**
16
* Initialize the framework with configuration
17
* @return this framework instance for chaining
18
*/
19
public AtmosphereFramework init();
20
21
/**
22
* Initialize with specific servlet config
23
* @param sc ServletConfig for initialization
24
* @return this framework instance
25
*/
26
public AtmosphereFramework init(ServletConfig sc);
27
28
/**
29
* Destroy the framework and cleanup resources
30
*/
31
public void destroy();
32
33
/**
34
* Add an AtmosphereHandler for a specific path mapping
35
* @param mapping URL pattern to handle
36
* @param handler AtmosphereHandler instance
37
* @return this framework instance
38
*/
39
public AtmosphereFramework addAtmosphereHandler(String mapping, AtmosphereHandler handler);
40
41
/**
42
* Get the broadcaster factory for managing broadcasters
43
* @return BroadcasterFactory instance
44
*/
45
public BroadcasterFactory getBroadcasterFactory();
46
47
/**
48
* Get the atmosphere configuration
49
* @return AtmosphereConfig instance
50
*/
51
public AtmosphereConfig getAtmosphereConfig();
52
53
/**
54
* Add an interceptor to the processing pipeline
55
* @param interceptor AtmosphereInterceptor to add
56
* @return this framework instance
57
*/
58
public AtmosphereFramework intercept(AtmosphereInterceptor interceptor);
59
}
60
```
61
62
**Usage Examples:**
63
64
```java
65
// Initialize framework
66
AtmosphereFramework framework = new AtmosphereFramework();
67
framework.init();
68
69
// Add handlers
70
framework.addAtmosphereHandler("/chat/*", new ChatHandler());
71
72
// Add interceptors
73
framework.intercept(new CorsInterceptor());
74
75
// Cleanup when done
76
framework.destroy();
77
```
78
79
### AtmosphereConfig
80
81
Configuration container that provides access to servlet configuration, context, and framework properties.
82
83
```java { .api }
84
/**
85
* Configuration container for the Atmosphere framework
86
*/
87
public class AtmosphereConfig {
88
/**
89
* Get the servlet configuration
90
* @return ServletConfig instance
91
*/
92
public ServletConfig getServletConfig();
93
94
/**
95
* Get the servlet context
96
* @return ServletContext instance
97
*/
98
public ServletContext getServletContext();
99
100
/**
101
* Get initialization parameter by name
102
* @param name parameter name
103
* @return parameter value or null
104
*/
105
public String getInitParameter(String name);
106
107
/**
108
* Get all framework properties
109
* @return Map of configuration properties
110
*/
111
public Map<String, Object> properties();
112
113
/**
114
* Get the AtmosphereFramework instance
115
* @return AtmosphereFramework
116
*/
117
public AtmosphereFramework framework();
118
}
119
```
120
121
### AtmosphereResource
122
123
Primary interface representing a suspended HTTP connection with broadcast capabilities. This is the core abstraction for real-time communication.
124
125
```java { .api }
126
/**
127
* Encapsulates suspended HTTP connections and provides broadcast mechanism
128
*/
129
public interface AtmosphereResource {
130
/**
131
* Suspend the connection indefinitely for real-time updates
132
* @return this resource for chaining
133
*/
134
public AtmosphereResource suspend();
135
136
/**
137
* Suspend with timeout
138
* @param timeout timeout value
139
* @return this resource for chaining
140
*/
141
public AtmosphereResource suspend(long timeout);
142
143
/**
144
* Suspend with timeout and time unit
145
* @param timeout timeout value
146
* @param timeunit TimeUnit for timeout
147
* @return this resource for chaining
148
*/
149
public AtmosphereResource suspend(long timeout, TimeUnit timeunit);
150
151
/**
152
* Resume the suspended connection
153
* @return this resource for chaining
154
*/
155
public AtmosphereResource resume();
156
157
/**
158
* Check if connection is currently suspended
159
* @return true if suspended
160
*/
161
public boolean isSuspended();
162
163
/**
164
* Check if connection has been resumed
165
* @return true if resumed
166
*/
167
public boolean isResumed();
168
169
/**
170
* Check if connection has been cancelled
171
* @return true if cancelled
172
*/
173
public boolean isCancelled();
174
175
/**
176
* Get the broadcaster associated with this resource
177
* @return Broadcaster instance
178
*/
179
public Broadcaster getBroadcaster();
180
181
/**
182
* Set the broadcaster for this resource
183
* @param broadcaster Broadcaster instance
184
* @return this resource for chaining
185
*/
186
public AtmosphereResource setBroadcaster(Broadcaster broadcaster);
187
188
/**
189
* Get the enhanced request object
190
* @return AtmosphereRequest instance
191
*/
192
public AtmosphereRequest getRequest();
193
194
/**
195
* Get the enhanced response object
196
* @return AtmosphereResponse instance
197
*/
198
public AtmosphereResponse getResponse();
199
200
/**
201
* Get the transport type for this connection
202
* @return TRANSPORT enum value
203
*/
204
public TRANSPORT transport();
205
206
/**
207
* Add event listener for resource state changes
208
* @param listener AtmosphereResourceEventListener
209
* @return this resource for chaining
210
*/
211
public AtmosphereResource addEventListener(AtmosphereResourceEventListener listener);
212
213
/**
214
* Remove event listener
215
* @param listener AtmosphereResourceEventListener to remove
216
* @return this resource for chaining
217
*/
218
public AtmosphereResource removeEventListener(AtmosphereResourceEventListener listener);
219
220
/**
221
* Set whether to resume on broadcast
222
* @param resumeOnBroadcast true to resume after broadcast
223
* @return this resource for chaining
224
*/
225
public AtmosphereResource resumeOnBroadcast(boolean resumeOnBroadcast);
226
227
/**
228
* Write data directly to this resource
229
* @param data data to write
230
* @return this resource for chaining
231
*/
232
public AtmosphereResource write(Object data);
233
}
234
```
235
236
**Usage Examples:**
237
238
```java
239
public class ChatHandler implements AtmosphereHandler {
240
@Override
241
public void onRequest(AtmosphereResource resource) throws IOException {
242
// Basic suspension
243
resource.suspend();
244
245
// Suspend with timeout
246
resource.suspend(30, TimeUnit.SECONDS);
247
248
// Add event listener
249
resource.addEventListener(new AtmosphereResourceEventListener() {
250
@Override
251
public void onSuspend(AtmosphereResourceEvent event) {
252
System.out.println("Resource suspended");
253
}
254
255
@Override
256
public void onBroadcast(AtmosphereResourceEvent event) {
257
System.out.println("Broadcast received: " + event.getMessage());
258
}
259
});
260
261
// Check transport type
262
TRANSPORT transport = resource.transport();
263
System.out.println("Using transport: " + transport);
264
}
265
}
266
```
267
268
### AtmosphereResourceFactory
269
270
Factory interface for creating and finding AtmosphereResource instances.
271
272
```java { .api }
273
/**
274
* Factory for creating AtmosphereResource instances
275
*/
276
public interface AtmosphereResourceFactory {
277
/**
278
* Create a new AtmosphereResource
279
* @param config AtmosphereConfig
280
* @param request HttpServletRequest
281
* @param response HttpServletResponse
282
* @return new AtmosphereResource instance
283
*/
284
public AtmosphereResource create(AtmosphereConfig config,
285
HttpServletRequest request,
286
HttpServletResponse response);
287
288
/**
289
* Find an existing AtmosphereResource by UUID
290
* @param uuid unique identifier
291
* @return AtmosphereResource or null if not found
292
*/
293
public AtmosphereResource find(String uuid);
294
}
295
```
296
297
### AtmosphereRequest
298
299
Enhanced HttpServletRequest with Atmosphere-specific features for body access and attribute handling.
300
301
```java { .api }
302
/**
303
* Enhanced HttpServletRequest with Atmosphere-specific features
304
*/
305
public interface AtmosphereRequest extends HttpServletRequest {
306
/**
307
* Get the request body as string
308
* @return request body content
309
*/
310
public String getBody();
311
312
/**
313
* Check if request has body content
314
* @return true if body is present
315
*/
316
public boolean hasBody();
317
318
/**
319
* Get local address for this request
320
* @return local address string
321
*/
322
public String getLocalAddr();
323
324
/**
325
* Get local name for this request
326
* @return local name string
327
*/
328
public String getLocalName();
329
330
/**
331
* Get local port for this request
332
* @return local port number
333
*/
334
public int getLocalPort();
335
}
336
```
337
338
### AtmosphereResponse
339
340
Enhanced HttpServletResponse with async writing capabilities and additional methods for real-time communication.
341
342
```java { .api }
343
/**
344
* Enhanced HttpServletResponse with async writing capabilities
345
*/
346
public interface AtmosphereResponse extends HttpServletResponse {
347
/**
348
* Get async I/O writer for non-blocking writes
349
* @return AsyncIOWriter instance
350
*/
351
public AsyncIOWriter getAsyncIOWriter();
352
353
/**
354
* Close the response and connection
355
*/
356
public void close();
357
358
/**
359
* Flush the response buffer
360
* @throws IOException if flush fails
361
*/
362
public void flushBuffer() throws IOException;
363
}
364
```
365
366
### AtmosphereHandler Interface
367
368
Core interface for handling incoming requests and broadcast events in the Atmosphere framework.
369
370
```java { .api }
371
/**
372
* Handle incoming AtmosphereResource requests and broadcast events
373
*/
374
public interface AtmosphereHandler {
375
/**
376
* Handle incoming request for AtmosphereResource
377
* @param resource AtmosphereResource to handle
378
* @throws IOException if handling fails
379
*/
380
public void onRequest(AtmosphereResource resource) throws IOException;
381
382
/**
383
* Handle broadcast events and state changes
384
* @param event AtmosphereResourceEvent containing broadcast data
385
* @throws IOException if handling fails
386
*/
387
public void onStateChange(AtmosphereResourceEvent event) throws IOException;
388
389
/**
390
* Destroy handler and cleanup resources
391
*/
392
public void destroy();
393
}
394
```
395
396
**Usage Examples:**
397
398
```java
399
@AtmosphereHandlerService(path = "/chat")
400
public class ChatHandler implements AtmosphereHandler {
401
402
@Override
403
public void onRequest(AtmosphereResource resource) throws IOException {
404
// Suspend connection for real-time updates
405
resource.suspend();
406
407
// Send welcome message
408
resource.write("Welcome to chat!");
409
}
410
411
@Override
412
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
413
// Handle broadcast messages
414
if (event.getMessage() != null && !event.isCancelled()) {
415
AtmosphereResource resource = event.getResource();
416
resource.write(event.getMessage().toString());
417
}
418
419
// Handle disconnections
420
if (event.isCancelled() || event.isResumedOnTimeout()) {
421
System.out.println("Client disconnected");
422
}
423
}
424
425
@Override
426
public void destroy() {
427
// Cleanup handler resources
428
System.out.println("ChatHandler destroyed");
429
}
430
}
431
```
432
433
### Meteor Interface
434
435
Simplified API for servlet-based applications providing easy access to Atmosphere functionality.
436
437
```java { .api }
438
/**
439
* Simplified servlet-based API for Atmosphere functionality
440
*/
441
public class Meteor {
442
/**
443
* Build Meteor instance from HttpServletRequest
444
* @param request HttpServletRequest
445
* @return Meteor instance
446
*/
447
public static Meteor build(HttpServletRequest request);
448
449
/**
450
* Build Meteor with custom configuration
451
* @param request HttpServletRequest
452
* @param broadcaster Broadcaster to use
453
* @param handler AtmosphereHandler for processing
454
* @return Meteor instance
455
*/
456
public static Meteor build(HttpServletRequest request, Broadcaster broadcaster,
457
AtmosphereHandler handler);
458
459
/**
460
* Suspend the connection
461
* @param timeout timeout in milliseconds (-1 for indefinite)
462
* @return Meteor instance for chaining
463
*/
464
public Meteor suspend(long timeout);
465
466
/**
467
* Resume the connection
468
* @return Meteor instance
469
*/
470
public Meteor resume();
471
472
/**
473
* Add AtmosphereHandler for processing
474
* @param handler AtmosphereHandler instance
475
* @return Meteor instance
476
*/
477
public Meteor addAtmosphereHandler(AtmosphereHandler handler);
478
479
/**
480
* Get the associated AtmosphereResource
481
* @return AtmosphereResource instance
482
*/
483
public AtmosphereResource getAtmosphereResource();
484
485
/**
486
* Get the Broadcaster for this Meteor
487
* @return Broadcaster instance
488
*/
489
public Broadcaster getBroadcaster();
490
}
491
```
492
493
**Usage Examples:**
494
495
```java
496
// In a servlet
497
public class ChatServlet extends HttpServlet {
498
499
@Override
500
protected void doGet(HttpServletRequest request, HttpServletResponse response)
501
throws ServletException, IOException {
502
503
// Create Meteor and suspend connection
504
Meteor meteor = Meteor.build(request)
505
.addAtmosphereHandler(new ChatHandler())
506
.suspend(-1); // Suspend indefinitely
507
508
// Get broadcaster for messaging
509
Broadcaster broadcaster = meteor.getBroadcaster();
510
broadcaster.broadcast("User joined chat");
511
}
512
513
@Override
514
protected void doPost(HttpServletRequest request, HttpServletResponse response)
515
throws ServletException, IOException {
516
517
// Handle chat message
518
String message = request.getParameter("message");
519
520
Meteor meteor = Meteor.build(request);
521
meteor.getBroadcaster().broadcast(message);
522
}
523
}
524
```
525
526
### Event Interfaces
527
528
Event system for monitoring AtmosphereResource state changes and lifecycle events.
529
530
```java { .api }
531
/**
532
* Event object containing state change information
533
*/
534
public interface AtmosphereResourceEvent {
535
/**
536
* Get the broadcast message
537
* @return message object
538
*/
539
public Object getMessage();
540
541
/**
542
* Get the associated AtmosphereResource
543
* @return AtmosphereResource instance
544
*/
545
public AtmosphereResource getResource();
546
547
/**
548
* Check if this is a resuming event
549
* @return true if resuming
550
*/
551
public boolean isResuming();
552
553
/**
554
* Check if this is a cancelled event
555
* @return true if cancelled
556
*/
557
public boolean isCancelled();
558
559
/**
560
* Check if resumed on timeout
561
* @return true if resumed due to timeout
562
*/
563
public boolean isResumedOnTimeout();
564
565
/**
566
* Get any exception that occurred
567
* @return Throwable or null
568
*/
569
public Throwable throwable();
570
}
571
572
/**
573
* Listener for AtmosphereResource state changes
574
*/
575
public interface AtmosphereResourceEventListener {
576
/**
577
* Called when resource is suspended
578
* @param event AtmosphereResourceEvent
579
*/
580
public void onSuspend(AtmosphereResourceEvent event);
581
582
/**
583
* Called when resource is resumed
584
* @param event AtmosphereResourceEvent
585
*/
586
public void onResume(AtmosphereResourceEvent event);
587
588
/**
589
* Called when resource is disconnected
590
* @param event AtmosphereResourceEvent
591
*/
592
public void onDisconnect(AtmosphereResourceEvent event);
593
594
/**
595
* Called when broadcast occurs
596
* @param event AtmosphereResourceEvent
597
*/
598
public void onBroadcast(AtmosphereResourceEvent event);
599
600
/**
601
* Called when exception occurs
602
* @param event AtmosphereResourceEvent
603
*/
604
public void onThrowable(AtmosphereResourceEvent event);
605
606
/**
607
* Called when connection is closed
608
* @param event AtmosphereResourceEvent
609
*/
610
public void onClose(AtmosphereResourceEvent event);
611
612
/**
613
* Called on heartbeat events
614
* @param event AtmosphereResourceEvent
615
*/
616
public void onHeartbeat(AtmosphereResourceEvent event);
617
}
618
```
619
620
### Dependency Injection System
621
622
Atmosphere's built-in dependency injection system for managing component lifecycle and dependencies.
623
624
```java { .api }
625
/**
626
* Factory for creating injectable objects
627
*/
628
public interface InjectableObjectFactory {
629
/**
630
* Create instance of specified class with dependency injection
631
* @param clazz class to instantiate
632
* @param config AtmosphereConfig for context
633
* @return injected instance
634
*/
635
public <T> T newClassInstance(Class<T> clazz, AtmosphereConfig config);
636
637
/**
638
* Configure the factory
639
* @param config AtmosphereConfig instance
640
*/
641
public void configure(AtmosphereConfig config);
642
}
643
644
/**
645
* Base interface for injectable components
646
*/
647
public interface Injectable {
648
/**
649
* Set AtmosphereConfig for the injectable component
650
* @param config AtmosphereConfig instance
651
*/
652
public void configure(AtmosphereConfig config);
653
}
654
```
655
656
#### Injectable Annotations
657
658
```java { .api }
659
/**
660
* Mark field or parameter for application-scoped injection
661
*/
662
@Target({ElementType.FIELD, ElementType.PARAMETER})
663
@Retention(RetentionPolicy.RUNTIME)
664
public @interface ApplicationScoped {
665
}
666
667
/**
668
* Mark field or parameter for request-scoped injection
669
*/
670
@Target({ElementType.FIELD, ElementType.PARAMETER})
671
@Retention(RetentionPolicy.RUNTIME)
672
public @interface RequestScoped {
673
}
674
```
675
676
#### Injectable Components
677
678
Components that can be injected into Atmosphere services and handlers:
679
680
```java { .api }
681
// Core injectable types:
682
AtmosphereConfig
683
AtmosphereFramework
684
BroadcasterFactory
685
AtmosphereResource
686
AtmosphereResourceFactory
687
MetaBroadcaster
688
WebSocketFactory
689
```
690
691
**Usage Examples:**
692
693
```java
694
@ManagedService(path = "/api/chat")
695
public class ChatService {
696
697
@ApplicationScoped
698
private BroadcasterFactory broadcasterFactory;
699
700
@ApplicationScoped
701
private AtmosphereConfig config;
702
703
@RequestScoped
704
private AtmosphereResource resource;
705
706
@Get
707
public void onConnect() {
708
// Use injected broadcaster factory
709
Broadcaster broadcaster = broadcasterFactory.lookup("/chat", true);
710
711
// Configure resource with injected config
712
String timeout = config.getInitParameter("chat.timeout");
713
if (timeout != null) {
714
resource.suspend(Long.parseLong(timeout));
715
} else {
716
resource.suspend();
717
}
718
}
719
}
720
721
// Custom injectable component
722
@Injectable
723
public class UserService implements Injectable {
724
private AtmosphereConfig config;
725
726
@Override
727
public void configure(AtmosphereConfig config) {
728
this.config = config;
729
}
730
731
public User getUser(String userId) {
732
// Custom user lookup logic
733
return new User(userId);
734
}
735
}
736
```
737
738
### Configuration Classes
739
740
Core configuration classes providing access to framework properties and servlet configuration.
741
742
```java { .api }
743
/**
744
* Application-level configuration constants and parameters
745
*/
746
public class ApplicationConfig {
747
// WebSocket configuration
748
public static final String WEBSOCKET_SUPPORT = "org.atmosphere.websocket.WebSocketSupport";
749
public static final String WEBSOCKET_PROTOCOL = "org.atmosphere.websocket.protocol";
750
751
// Comet support
752
public static final String NATIVE_COMETSUPPORT = "org.atmosphere.useNative";
753
public static final String BLOCKING_COMETSUPPORT = "org.atmosphere.useBlocking";
754
755
// Broadcasting configuration
756
public static final String BROADCASTER_FACTORY = "org.atmosphere.cpr.BroadcasterFactory";
757
public static final String BROADCASTER_CACHE = "org.atmosphere.cpr.BroadcasterCache";
758
public static final String BROADCASTER_LIFECYCLE_POLICY = "org.atmosphere.cpr.BroadcasterLifeCyclePolicy";
759
760
// Serialization and encoding
761
public static final String SERIALIZER = "org.atmosphere.cpr.Serializer";
762
public static final String MESSAGE_DELIMITER = "org.atmosphere.cpr.messageDelimiter";
763
764
// Performance and scaling
765
public static final String SUSPENDED_RESOURCE_TIMEOUT = "org.atmosphere.cpr.suspendedResourceTimeout";
766
public static final String BROADCASTER_MESSAGE_PROCESSING_THREADPOOL_MAXSIZE =
767
"org.atmosphere.cpr.BroadcasterMessageProcessingThreadPoolMaxSize";
768
769
// Security
770
public static final String ALLOW_QUERYSTRING_AS_REQUEST = "org.atmosphere.cpr.allowQueryStringAsRequest";
771
public static final String EXCLUDED_CONTENT_TYPES = "org.atmosphere.cpr.excludedContentTypes";
772
}
773
774
/**
775
* Framework-level configuration constants
776
*/
777
public class FrameworkConfig {
778
// Internal framework constants
779
public static final String ATMOSPHERE_RESOURCE = AtmosphereResource.class.getName();
780
public static final String ATMOSPHERE_HANDLER = AtmosphereHandler.class.getName();
781
public static final String BROADCASTER_FACTORY = BroadcasterFactory.class.getName();
782
public static final String INJECTED_ATMOSPHERE_RESOURCE = "org.atmosphere.cpr.AtmosphereResource.injected";
783
784
// Runtime configuration
785
public static final String CONTAINER_AUTO_DETECTION = "org.atmosphere.container.autoDetection";
786
public static final String WRITE_HEADERS = "org.atmosphere.cpr.writeHeaders";
787
public static final String RESUME_AND_KEEPALIVE = "org.atmosphere.cpr.resumeAndKeepAlive";
788
}
789
```
790
791
**Usage Examples:**
792
793
```java
794
// Configure Atmosphere in web.xml
795
<servlet>
796
<servlet-name>AtmosphereServlet</servlet-name>
797
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
798
<init-param>
799
<param-name>org.atmosphere.websocket.WebSocketSupport</param-name>
800
<param-value>true</param-value>
801
</init-param>
802
<init-param>
803
<param-name>org.atmosphere.cpr.BroadcasterCache</param-name>
804
<param-value>org.atmosphere.cache.UUIDBroadcasterCache</param-value>
805
</init-param>
806
<init-param>
807
<param-name>org.atmosphere.cpr.suspendedResourceTimeout</param-name>
808
<param-value>300000</param-value>
809
</init-param>
810
</servlet>
811
812
// Programmatic configuration
813
@ApplicationScoped
814
public class AtmosphereConfiguration {
815
816
public void configureFramework(AtmosphereFramework framework) {
817
AtmosphereConfig config = framework.getAtmosphereConfig();
818
819
// Enable WebSocket support
820
config.properties().put(ApplicationConfig.WEBSOCKET_SUPPORT, "true");
821
822
// Set broadcaster cache
823
config.properties().put(ApplicationConfig.BROADCASTER_CACHE,
824
"org.atmosphere.cache.UUIDBroadcasterCache");
825
826
// Configure timeouts
827
config.properties().put(ApplicationConfig.SUSPENDED_RESOURCE_TIMEOUT, "300000");
828
829
// Set message processing thread pool
830
config.properties().put(ApplicationConfig.BROADCASTER_MESSAGE_PROCESSING_THREADPOOL_MAXSIZE, "10");
831
}
832
}
833
```
834
835
## Transport Types
836
837
```java { .api }
838
/**
839
* Available transport mechanisms
840
*/
841
public enum TRANSPORT {
842
POLLING, // Traditional polling
843
LONG_POLLING, // Long polling (Comet)
844
STREAMING, // HTTP streaming
845
WEBSOCKET, // WebSocket protocol
846
JSONP, // JSONP for cross-domain
847
SSE, // Server-Sent Events
848
AJAX, // AJAX requests
849
HTMLFILE, // HTML file transport
850
CLOSE, // Connection close
851
UNDEFINED // Unknown transport
852
}
853
```