I am currently working on a simple PuSH subscriber written in Scala using the Restlet framework. The code is available at: http://github.com/kafecho/scala-push. It is a port of a Groovy version that I wrote a while ago. As I mentioned above, PuSH uses HTTP REST invocations, and to that effect the code leverages Restlet, a very good lightweight framework for writing RESTful web services in Java. I am planning on using RESTlet in 2 places. First is to implement the PuSH protocol itself as I need to provide GET endpoints used by the HUB to send subscribe/unsubscribe request and a POST endpoint used by the Hub to publish content. Second is to implement a simple admin REST API that I can use to query the state of subscriptions and add or remove subscriptions. Having the admin API makes it easy to implement front end UIs either in a web browser or as a command line using a tool such as Curl.
The port from Groovy to Scala was fairly effortless and both versions look actually very similar in terms of length and conciseness.
If you are new to Scala, there are few things to note if you look at the code.
- You don't have to put all classes in separate files.
- As Scala is based on the JVM, you can reuse existing Java libraries.
- Scala supports traits which can be thought of Interfaces with associated code. A class can mix-it multiple traits thus providing the benefit of multiple inheritance without the complications.
- Scala type inference is very powerful so in most cases no need to type variables. Parentheses are also optional in method calls.
In plain English, we find which of the link node within the XML document has a rel attribute set to hub. The result (hubXML) is an optional value; we use Scala's pattern matching to extract its actual value or react accordingly if the value does not exist. This may look a bit verbose but it is actually designed to reduce the amount of null pointer exceptions at run-time.
No comments:
Post a Comment