Hi all,
My favorite show in television is Airtel Super Singer Junior (that identifies and encourages music talent in kids). The season 3 of the show had its Grand Finale yesterday(26th Oct 2012). Final results were based on the Audience poll. Inspired by this – I wanted to try creating a web app that can leverage the ATMOSPHERE SERVER PUSH to show live updates as votes are being recorded.
I wanted to use Jersey RESTful webservices & a simple HTML form for gathering the VOTES. Reasoning behind this choice was that – existing user flows in applications I work with, have the UI interacts with RESTful webservices to capture data.
I wanted to understand how the java based atmosphere framework can be plugged into this scheme and be used for server push purposes alone. As a week old toddler in Atmosphere – I’m still exploring the framework – so there could be other, probably better integrations possible. But this acted as a very good exercise for me to explore different parts of atmosphere.
So here we go –
1. As a first step – I created a pom.xml (I extended the pom.xml from Atmosphere samples base directory). My pom.xml was again a verbatim replica of the pom.xml shipped with rest-chat sample.
2. I updated the web.xml to redirect all /rest/* URLs to Jersey REST Servlet. Similarly /assp/* (assp was actually airtel super singer poll that was the initial trigger ) URLs to Atmosphere Servlet
3. Next I wanted 2 sets of files
a. Voting related files based on JERSEY REST that consists of
(i) vote.html – basic html form with radio buttons (representing choices in the opinion poll)
(ii) voting.js – my custom script that will send the votes to server through ajax
(iii) VoteResource.java – Jersey REST based resource that will have the @POST method.
(iv) Contestant.java – A simple POJO with 2 attributes – contestantName, noOfVotes. For making sure that the noOfVotes is incremented in a consistent way from multiple threads of the web application – I used a atomic long ( incrementAndGet is used to increment the votes)
(v) VoteMap.java – For the purposes of this post – this will be our in memory store and will be modeled as a singleton. This will contain a ConcurrentHashMap<String,Contestant> – contestantName will be the key and the instance of Contestant POJO will be the value.
In a typical flow – users will visit vote.html to make their choice for the opinion poll. This information will be posted back to server through ajax. @POST method in VoteResource will be invoked to record the vote. The contestantName will be used to lookup the Contestant instance from the voteMap. Increment method of Contestant POJO will use increment method of atomic long to increment the votes.
After updating the no of votes – it has to send a update to all the connected clients through server push. It will use the
MetaBroadcaster.getDefault().broadcastTo(“/”, VoteMap.getInstance().toString());
Above line does the magic to sending the JSON containing contestants and votes to clients.
For purposes of this post – I overrode the toString method of Contestant & VoteMap to return a JSON representation of these objects. I would definitely look forward to making use of automatic JSON conversion using JAXB / Jackson Mapping capabilities of Jersey.
Till now – this has been a regular JERSEY REST project that records the vote choices from UI sent through ajax.
b. Server Push related files that will display the results sent periodically through server push.
(i) results.html – a simple HTML file that will display the results of the poll as a bar chart
(ii)application.js – will use atmosphere based javascript APIs to connect to the server. Further everytime, server sends a update – it will parse the JSON to update the graph
(iii) jqBarChart.js – third party http://www.workshop.rs/jqbargraph/ that will be used for plotting the graph
(iv) AsspResource.java – Main Atmosphere based resource. It has a @GET method annotated additionally @Suspend. As explained in docs – requests to this resource through GET method will be suspended.
When a broadcaster sends data – it is received by these suspended broadcasters
I’m cleaning up the source code removing all unnecessary code – would attach the same soon!!
Here we go – I’ve uploaded the source code zip file to ubuntu one – rest-assp.zip