Showing results for 
Search instead for 
Did you mean: 

Scala, Stingray Traffic Manager and Java Extensions (06/30/2009)

Not applicable

Scala, Stingray Traffic Manager and Java Extensions (06/30/2009)


There has been a lot of talk over the past few years about new and different languages targeting the JVM as their runtime platform. Scala has been one of the most high-profile languages in the JVM vanguard. Most notably, in the past year Twitter has announced that it is migrating away from Ruby on Rails to Scala because of RoR's lack of scalability.

Scala is a neat hybrid language which is both object oriented and functional. Its best aspect is that you can utilize all of the standard Java libraries without writing any Java code. Check out  for more information on the language itself, as I'm just going to talk about how to get a simple 'Hello World' example working with Stingray.

Why is it possible for us to use Scala in place of Java with Stingray? Well, since Scala targets the JVM we can just import the compiled class files into Stingray and they are recognized in the same way as any Java class file. The only extra step we need to take is to import the Scala libraries into Stingray, so that they are available to the same Java runtime. This additional file, scala-library.jar, can be found in your Scala distribution and all you have to do is upload it to Stingray using the Java Extensions Catalog.

If you want more information on Java Extensions in Stingray, then have a look at the related articles Introducing Java Extensions and "Hello, World!" Java Extension.

Ok, let's look at a simple hello world example in Scala.

  1. import javax.servlet.http._;
  2. import com.zeus.ZXTMServlet._;
  3. class MyServlet extends HttpServlet
  4. {
  5.    override def doGet(zreq: HttpServletRequest, zres: HttpServletResponse)
  6.    {
  7.       zres.setContentType("text/html");
  8.       var out = zres.getWriter();
  9.       out.println("Hello World");
  10.    }
  11. }

Quite simple and no magic, we just extend an HttpServlet as we would using Java and act on the Response object providing some content. Put this in a file MyServlet.scala and compiling is just a matter of invoking the Scala compiler.

> scalac MyServlet.scala

This will produce a class file, MyServlet.class, which you will need to upload to Stingray. Stingray looks at class files and determines which can be called via TrafficScript, creating stub TrafficScript rules as necessary. Attach this new rule to the HTTP VirtualServer of your choosing as a request rule and see your Scala code in action. Now you are all set to work with Scala in Stingray!

UPDATE: Incorporated changes suggested by James Iry.