Play 2 Scala Console on Heroku

Posted by: James Ward on 06/11/2012

I’ve been working on a Play 2 application that I’ll be using for the Grails vs Play Smackdown at ÜberConf next week. The app is running in production on Heroku but since I don’t have an admin UI yet, I needed a quick and easy way to create a new entity. I could have gone straight to the database but thought it would be better to run Play 2′s Scala Console on Heroku and then just run some arbitrary Scala code. The Scala Console in Play 2 is really just the Scala REPL in SBT but it allows you to interact with a Play application.

I’ll walk you through how to use the Play 2 Scala Console on Heroku. If you’d like to follow along, grab the play2bars app by running:

git clone https://github.com/jamesward/play2bars.git
cd play2bars
git checkout java-ebean

To run the console locally, run:

play -DapplyEvolutions.default=true console

You will see something like:

[info] Loading project definition from /home/jamesw/Desktop/play2bars/project
[info] Set current project to play2bars-java (in build file:/home/jamesw/Desktop/play2bars/)
[info] Updating {file:/home/jamesw/Desktop/play2bars/}play2bars-java...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin                                                                                [info] Done updating.                                                        
[info] Compiling 4 Scala sources and 3 Java sources to /home/jamesw/Desktop/play2bars/target/scala-2.9.1/classes...
[warn] Note: /home/jamesw/Desktop/play2bars/app/models/Bar.java uses unchecked or unsafe operations.
[warn] Note: Recompile with -Xlint:unchecked for details.
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0_03).
Type in expressions to have them evaluated.
Type :help for more information.
 
scala>

Start a Play app from the Scala console:

new play.core.StaticApplication(new java.io.File("."))

You will see something like:

[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Prod)
res0: play.core.StaticApplication = play.core.StaticApplication@2e338c56

Now you can interact with the Play application. Lets create a new “Bar” entity:

var bar = new models.Bar()
bar.name = "foo bar"
bar.save

And now you can query the “Bar” entities:

import scala.collection.JavaConversions._
models.Bar.find.all.foreach(bar => println(bar.name))

So that’s pretty cool, right?

If you want to run this example on Heroku, then install the Heroku Toolbelt and run:

heroku create -s cedar

And push the application to Heroku:

git push heroku java-ebean:master

Once the application is built and deployed on Heroku, check to make sure it works:

heroku open

To run the Scala console on Heroku, run:

heroku run bash
java -DapplyEvolutions.default=true -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=$DATABASE_URL -jar ~/.sbt_home/bin/sbt-launch-0.11.3-2.jar

SBT will resolve some dependencies and then you will be in the SBT console. From there run:

set fullClasspath in Compile += Attributed.blank(file("target/staged/*"))
console

Now you will be in the Scala REPL for your Play app on Heroku. So you can run things like:

new play.core.StaticApplication(new java.io.File("."))
var bar = new models.Bar()
bar.name = "foo bar"
bar.save

Reload your app on Heroku in the browser and you should now see the new Bar listed on the page!

Let me know if you have any questions.


About James Ward

James Ward

James Ward (www.jamesward.com) works for Typesafe where he teaches developers the Typesafe Stack (Play Framework, Scala, and Akka) . James frequently presents at conferences around the world such as JavaOne, Devoxx, and many other Java get-togethers. Along with Bruce Eckel, James co-authored First Steps in Flex. He has also published numerous screencasts, blogs, and technical articles. Starting with Pascal and Assembly in the 80′s, James found his passion for writing code. Beginning in the 90′s he began doing web development with HTML, Perl/CGI, then Java. After building a Flex and Java based customer service portal in 2004 for Pillar Data Systems he became a Technical Evangelist for Flex at Adobe. In 2011 James became a Principal Developer Evangelist at Salesforce.com where he taught developers how to deploy apps on the cloud with Heroku. James Tweets as @_JamesWard and posts code at github.com/jamesward.

More About James »

Northern Virginia Software Symposium

November 1 - 3, 2013

Reston, VA

Current Topics on the NFJS Tour

  • Core Java, JEE
  • Dynamic Languages: Groovy, JRuby, Scala, Clojure
  • RESTful Web Apps
  • Frameworks: Hibernate, Grails, Spring, JSF, GWT, more
  • Agility
  • Test Driven Design
  • Security
  • Ajax, Flex, RIA
View Event Details »

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 7
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 3
  • Automation Tools: Git, Hudson, Sonar
  • HTML5, Ajax, jQuery, Usability
  • Mobile Applications - iPhone and Android
  • More...
Learn More »

NFJS, the Magazine

May Issue Now Available
  • On the road to learning

    by Raju Gandhi
  • Refactoring to Modularity

    by Kirk Knoernschild
  • RESTful Groovy

    by Kenneth Kousen
  • Getting Started with D3.js

    by Brian Sletten
Learn More »