Home > Atmosphere > @Cluster: Clustering your Comet application using Atmosphere

@Cluster: Clustering your Comet application using Atmosphere

It is really simple to add clustering support to an Atmosphere’s Comet based application, and deploy it inside any Servlet Container supporting Servlet 3.0, Comet or not. You just have to decide which group technology you want to use, thanks to Atmosphere Plug in: Shoal or JGroups!

Note: The Atmosphere Framework have evolved since the release of that blog. Please visit our web site for an updated sample and white paper

IMG_0135.JPG

First, if you want to get started with Atmosphere, I recommend to take a look at atmosphere-cpr (POJO based) and atmosphere-core (Annotations/REST based) introduction first. Let’s start with the Chat sample written against atmosphere-cpr. The AtmosphereHandler#onEvent looks like

 88         if (req.getMethod().equalsIgnoreCase("GET")) {
 89             event.suspend();
 90             Broadcaster bc = event.getBroadcaster();
 91             bc.getBroadcasterConfig().addFilter(new XSSHtmlFilter());
 92             bc.broadcast(event.getAtmosphereConfig().getWebServerName()
 93                     + "**has suspended a connection from "
 94                     + req.getRemoteAddr());
 95         } else if (req.getMethod().equalsIgnoreCase("POST")) {
 96             res.setCharacterEncoding("UTF-8");
 97             String action = req.getParameterValues("action")[0];
 98             String name = req.getParameterValues("name")[0];
 99             
100             if ("login".equals(action)) {
101                 req.getSession().setAttribute("name", name);
102                 event.getBroadcaster().broadcast("System Message from "
103                         + event.getAtmosphereConfig().getWebServerName() + "**" + name + " has joined.");
104                 res.getWriter().write("success");
105                 res.getWriter().flush();
106             } else if ("post".equals(action)) {
107                 String message = req.getParameterValues("message")[0];
108                 event.getBroadcaster().broadcast(name + "**" + message);
109                 res.getWriter().write("success");
110                 res.getWriter().flush();
111             } else {
112                 res.setStatus(422);
113                 
114                 res.getWriter().write("success");
115                 res.getWriter().flush();
116             }   
117         }   
118         return event;
119     }

One nice feature of Atmosphere is the support of Broadcaster. Broadcaster are used to broadcast messages between suspended responses, e.g. for the chat sample, every time a new message is entered by a user, we use a Broadcaster to broadcast that message to all other users. Broadcaster supports BroadcastFilter, which are really useful for manipulating messages before they get written by the set of suspended connections. If you look at line 80/81 above, we configure the Broadcaster with the XSSHtmlFilter to prevent a malicious chatter to send us Javascript as message, and push that script back to other user. Indeed, BroadcastFilters are quite useful when it’s time to filter/transform/agreggate messages or to broadcast messages to other components like EJB, JMS topics/queues…or cluster!. The good news is Atmosphere supports two clusters technology: JGroups and Shoal. To add cluster support to the above Chat application, all we need to do is:

 84         res.setContentType("text/html");
 85         res.addHeader("Cache-Control", "private");
 86         res.addHeader("Pragma", "no-cache");
 87 
 88         if (req.getMethod().equalsIgnoreCase("GET")) {
 89             event.suspend();
 90             Broadcaster bc = event.getBroadcaster();
 91             bc.getBroadcasterConfig().addFilter(
 92                 new JGroupsFilter(bc, event.getAtmosphereConfig().getWebServerName()));
 93             bc.getBroadcasterConfig().addFilter(new XSSHtmlFilter());

Just need to add the XXXFilter, where xxx is the group technology you want to use: ShoalFilter or JGroupsFilter, and that’s it! Just adding line 91 makes your atmosphere-cpr application clustered and any invocation of Broadcaster.broadcast will reach all AtmosphereHandler instance in the same cluster.

HUH that was too complicated :-)! Let’s now use atmosphere-core’s support for annotations. The Atmosphere’s REST based Chat sample looks like:

 53 @Path("/chat")
 54 public class ResourceChat {
 55 
 56     @Suspend()
 57     @GET
 58     @Produces("text/html")
 59     public String suspend() {
 60         return "";
 61     }           
 62     
 63     @Broadcast
 64     @Consumes("application/x-www-form-urlencoded")
 65     @POST
 66     @Produces("text/html")
 67     @BroadcastFilter({XSSHtmlFilter.class,JsonpFilter.class})
 68     public String publishMessage(MultivaluedMap form) {
 69         String action = form.getFirst("action");
 70         String name = form.getFirst("name");
 71         
 72         if ("login".equals(action)) {
 73             return ("System Message" + "__" + name + " has joined.");
 74         } else if ("post".equals(action)) { 
 75             return name + "__" + form.getFirst("message");
 76         } else {
 77             throw new WebApplicationException(422);
 78         }   
 79     }   
 80 } 

In order to add clustering support, we just need to annotate the method that broadcast message, e.g:

 65     @Broadcast
 66     @Consumes("application/x-www-form-urlencoded")
 67     @POST
 68     @Produces("text/html")
 69     @BroadcastFilter({XSSHtmlFilter.class,JsonpFilter.class})
 70     @Cluster(
 71         name="chat",
 72         value=ShoalFilter.class
 73     )
 74     public String publishMessage(MultivaluedMap form) {
 75         String action = form.getFirst("action");
 76         String name = form.getFirst("name");
 77         
 78         if ("login".equals(action)) {
 79             return ("System Message" + "__" + name + " has joined.");
 80         } else if ("post".equals(action)) {
 81             return name + "__" + form.getFirst("message");
 82         } else {
 83             throw new WebApplicationException(422);
 84         }
 85     }
 86 }

That’s it!. By just annotating the proper method, Atmosphere will makes sure to properly configure the Shoal/JGroups so every broadcast will be shared inside the cluster.

For any questions or to download the above sample, go to our main site and use our Nabble forum (no subscription needed) or follow us on Twitter and tweet your questions there!

var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));

var pageTracker = _gat._getTracker(“UA-3111670-3″);
pageTracker._initData();
pageTracker._trackPageview();

technorati:

About these ads
Categories: Atmosphere

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: