Package org.atmosphere.cpr
Interface AtmosphereHandler
-
- All Known Subinterfaces:
AnnotatedProxy,AtmosphereServletProcessor
- All Known Implementing Classes:
AbstractReflectorAtmosphereHandler,AbstractReflectorAtmosphereHandler.Default,AtmosphereHandlerAdapter,ManagedAtmosphereHandler,OnMessage,ReflectorServletProcessor
public interface AtmosphereHandlerImplementation ofAtmosphereHandlerallows creation of event-driven web applications which are hosted in the browser. AnAtmosphereHandlerallows a web application to suspend and resume an HTTP response. An HTTP response can be suspendedAtmosphereResource.suspend()and later resume viaAtmosphereResource.resume(). Messages can also be shared between suspended responses and their associatedAtmosphereHandlerusing aBroadcaster. Any invocation ofBroadcaster.broadcast(java.lang.Object)will allow a suspended response to write the content of the messageonStateChange(org.atmosphere.cpr.AtmosphereResourceEvent). A class implementingAtmosphereHandlermust be thread safe. For example, a simple pubsub based AtmosphereHandler will take the form of
It is recommended to use thepublic class AtmosphereHandlerPubSub extends AbstractReflectorAtmosphereHandler { public void onRequest(AtmosphereResource r) throws IOException { AtmosphereRequest req = r.getRequest(); AtmosphereResponse res = r.getResponse(); String method = req.getMethod(); // Suspend the response. if ("GET".equalsIgnoreCase(method)) { // Log all events on the console, including WebSocket events. r.addEventListener(new WebSocketEventListenerAdapter()); res.setContentType("text/html;charset=ISO-8859-1"); Broadcaster b = lookupBroadcaster(req.getPathInfo()); r.setBroadcaster(b).suspend(); } else if ("POST".equalsIgnoreCase(method)) { Broadcaster b = lookupBroadcaster(req.getPathInfo()); String message = req.getReader().readLine(); if (message != null && message.indexOf("message") != -1) { b.broadcast(message.substring("message=".length())); } } } public void destroy() { } Broadcaster lookupBroadcaster(String pathInfo) { String[] decodedPath = pathInfo.split("/"); Broadcaster b = BroadcasterFactory.getDefault().lookup(decodedPath[decodedPath.length - 1], true); return b; } }AtmosphereHandlerServiceorManagedServiceannotation to reduce the number of lines of code and take advantage ofAtmosphereInterceptorsuch asAtmosphereResourceStateRecoveryandAtmosphereResourceLifecycleInterceptor.- Author:
- Jeanfrancois Arcand
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voiddestroy()Destroy this handlervoidonRequest(AtmosphereResource resource)When a client sends a request to its associatedAtmosphereHandler, it can decide if the underlying connection can be suspended (creating a Continuation) or handle the connection synchronously.voidonStateChange(AtmosphereResourceEvent event)This method is invoked when theBroadcasterexecutes a broadcast operation.
-
-
-
Method Detail
-
onRequest
void onRequest(AtmosphereResource resource) throws java.io.IOException
When a client sends a request to its associatedAtmosphereHandler, it can decide if the underlying connection can be suspended (creating a Continuation) or handle the connection synchronously. It is recommended to only suspend requests for which HTTP method is a GET and use the POST method to send data to the server, without marking the connection as asynchronous.- Parameters:
resource- anAtmosphereResource- Throws:
java.io.IOException
-
onStateChange
void onStateChange(AtmosphereResourceEvent event) throws java.io.IOException
This method is invoked when theBroadcasterexecutes a broadcast operation. When this method is invoked its associatedBroadcaster, any suspended connection will be allowed to write the data back to its associated clients. This method will also be invoked when a response get resumed, e.g. whenAtmosphereResource.resume()gets invoked. In that case,AtmosphereResourceEvent.isResuming()will return true. This method will also be invoked when theAtmosphereResource.suspend(long)expires. In that case,AtmosphereResourceEvent.isResumedOnTimeout()will return true.- Parameters:
event- anAtmosphereResourceEvent- Throws:
java.io.IOException
-
destroy
void destroy()
Destroy this handler
-
-