Over a million developers have joined DZone.

Writing Comet Applications Using JRuby and the Atmosphere Framework

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Writing Atmosphere's Comet based applications is simple. Imagine using JRuby instead of Java...it becomes really simple!. As with Scala, it is also possible to write Comet application with Atmosphere Framework using JRuby. A user of Atmosphere recently posted the famous chat application written using JRuby and an embedded instance of Jetty. I've decided to write the same application using the Atmosphere Spade Server, which build on top of Jersey and Grizzly (will soon support Netty and Jetty). The idea behind the Atmosphere Spade Server is to make it really simple to test and embed your Comet application. Note that any application written using the Atmosphere Spade Server is portable, e.g. it will also deploy into any Servlet Container like Tomcat, Glassfish, Weblogic etc.

Before jumping into JRuby, let's just explore the Atmosphere Spade Server main class, AtmosphereSpadeServer:

public static AtmosphereSpadeServer.build(String uri);

Invoking that simple method will automatically creates, under the hood, a Grizzly Servlet Container instance ready to receive requests based on the URI entered

AtmosphereSpadeServer.build("http://localhost:8080/")

Next step is to add your Atmosphere POJO aka AtmosphereHandler (I recommend you read that post for more details about AtmosphereHandler):

AtmosphereSpadeServer.build("http://localhost:8080/").addAtmosphereHandler(...);

And finally, you are ready to start it:

AtmosphereSpadeServer.build("http://localhost:8080/").addAtmosphereHandler(...).start();

If you are using Atmosphere Core/REST (powered by Jersey), it event simpler as you don't need to define AtmosphereHandler, but instead just tell Jersey the name of the package to look for JAX RS resources:

AtmosphereSpadeServer build("http://localhost:8080/","org.atmosphere.myresources").start()

Now let's jump into JRuby world and uses the AtmosphereSpadeServer:

  require 'java'
   
  Dir["./Java/atmosphere/atmosphere-spade-server.jar"].each { |jar| require jar }
   
  include_class 'javax.servlet.http.HttpServlet'
  include_class 'org.atmosphere.cpr.AtmosphereHandler'
  include_class 'org.atmosphere.grizzly.AtmosphereSpadeServer'
   
  #setup and start the server
  def main
    AtmosphereSpadeServer.build("http://localhost:8080/")
              .addAtmosphereHandler("", ChatPage.new())
                  .addAtmosphereHandler("/chat-stream", ChatStream.new()).start
  end

The above code starts the server using two AtmosphereHandler. The first will send back a chat.html page to be rendered by the browser

  #serve the chat page
  class ChatPage
    include AtmosphereHandler
    
    def onEvent(event)
      Kernel.load(__FILE__)
      res = event.getResponse()
      
      res.setContentType("text/html")
      res.addHeader("Cache-Control", "private")
      res.addHeader("Pragma", "no-cache")
      File.open(File.dirname(__FILE__)+'/chat.html').each { |line|
        res.getWriter().write(line)
      }
      res.getWriter().flush()
      event
   end
  end

The second one is used for suspending the response and broadcasting chat messages

  #serve the chat stream and post messages
  class ChatStream
      include AtmosphereHandler
    
      BEGIN_SCRIPT_TAG = '<script>'
      END_SCRIPT_TAG = '</script>'
    
      def onEvent(event)
          req = event.getRequest();
          res = event.getResponse();
          
          res.setContentType("text/html")
          res.addHeader("Cache-Control", "private")
          res.addHeader("Pragma", "no-cache")
          res.setCharacterEncoding("UTF-8");
         
          if (req.getMethod().upcase === "GET")
              # for IE
              res.getWriter().write("<!-- Comet  enables web " +
                                    "servers to send data without having any need " +
                                    "for the client to request it. -->\n");
              res.getWriter().flush();
              <b>event.suspend();</b>
              
          elsif (req.getMethod().upcase === "POST")
              message = req.getParameterValues("message")[0].to_s
              ip = req.getRemoteAddr().to_s
              <b>event.getBroadcaster().broadcast(
                  BEGIN_SCRIPT_TAG +
                  "window.parent.say('<small>#{ip} - 
                              #{req.getHeader("User-Agent").to_s}:
                             </small><br><b>#{message}</b>')" +
                  END_SCRIPT_TAG);</b>
                  
                  res.getWriter().write('success')
                  res.getWriter().flush();
          end     
          
          event
      end 
      
      def onMessage(event)
          writer = event.getResponse().getWriter()
          writer.write(event.getMessage().to_s.ljust(1*1024))
          writer.flush()
          event
      end 
  end

With Atmosphere, every suspended connection are represented by an instance of AtmosphereEvent, and when a Broadcast operations happens, the AtmosphereHandler.onMessage(AtmosphereEvent) will be invoked hence an application can write back the broadcasted message.

Of course, you can do much more complex application using Atmosphere and JRuby, but my goal here was just to show how easy it can be.

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!

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}