DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Clustering Your Comet Application Using Atmosphere

Clustering Your Comet Application Using Atmosphere

Jean-Francois Arcand user avatar by
Jean-Francois Arcand
·
Jul. 07, 09 · Java Zone · Interview
Like (0)
Save
Tweet
8.02K Views

Join the DZone community and get the full member experience.

Join For Free

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!

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

  if (req.getMethod().equalsIgnoreCase("GET")) {
event.suspend();
Broadcaster bc = event.getBroadcaster();
bc.getBroadcasterConfig().addFilter(new XSSHtmlFilter());
bc.broadcast(event.getAtmosphereConfig().getWebServerName()
+ "**has suspended a connection from "
+ req.getRemoteAddr());
} else if (req.getMethod().equalsIgnoreCase("POST")) {
res.setCharacterEncoding("UTF-8");
String action = req.getParameterValues("action")[0];
String name = req.getParameterValues("name")[0];

if ("login".equals(action)) {
req.getSession().setAttribute("name", name);
event.getBroadcaster().broadcast("System Message from "
+ event.getAtmosphereConfig().getWebServerName() + "**" + name + " has joined.");
res.getWriter().write("success");
res.getWriter().flush();
} else if ("post".equals(action)) {
String message = req.getParameterValues("message")[0];
event.getBroadcaster().broadcast(name + "**" + message);
res.getWriter().write("success");
res.getWriter().flush();
} else {
res.setStatus(422);

res.getWriter().write("success");
res.getWriter().flush();
}
}
return event;
}

 

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:

 res.setContentType("text/html");
res.addHeader("Cache-Control", "private");
res.addHeader("Pragma", "no-cache");

if (req.getMethod().equalsIgnoreCase("GET")) {
event.suspend();
Broadcaster bc = event.getBroadcaster();
bc.getBroadcasterConfig().addFilter(
new JGroupsFilter(bc, event.getAtmosphereConfig().getWebServerName()));
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:

 @Path("/chat")
public class ResourceChat {

@Suspend()
@GET
@Produces("text/html")
public String suspend() {
return "";
}

@Broadcast
@Consumes("application/x-www-form-urlencoded")
@POST
@Produces("text/html")
@BroadcastFilter({XSSHtmlFilter.class,JsonpFilter.class})
public String publishMessage(MultivaluedMap form) {
String action = form.getFirst("action");
String name = form.getFirst("name");

if ("login".equals(action)) {
return ("System Message" + "__" + name + " has joined.");
} else if ("post".equals(action)) {
return name + "__" + form.getFirst("message");
} else {
throw new WebApplicationException(422);
}
}
}

 

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

@Broadcast
@Consumes("application/x-www-form-urlencoded")
@POST
@Produces("text/html")
@BroadcastFilter({XSSHtmlFilter.class,JsonpFilter.class})
@Cluster(
name="chat",
value=ShoalFilter.class
)
public String publishMessage(MultivaluedMap form) {
String action = form.getFirst("action");
String name = form.getFirst("name");

if ("login".equals(action)) {
return ("System Message" + "__" + name + " has joined.");
} else if ("post".equals(action)) {
return name + "__" + form.getFirst("message");
} else {
throw new WebApplicationException(422);
}
}
}

 

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!

From http://weblogs.java.net/blog/jfarcand

Atmosphere (architecture and spatial design) application clustering Comet (pinball)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The End of the Beginning for Apache Cassandra
  • Top 11 Cloud Platforms for Internet of Things (IoT)
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Ultra-Fast Microservices: When Microstream Meets Payara

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo