A Play Scala Server Running on IBM Bluemix

Share: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditEmail this to someonePrint this page

This blog article describes how to create a simple Play application written in Scala and deploy it on IBM Bluemix.The article assumes that the reader is familiar with Scala, the Play application framework and IBM Bluemix. Please see the Reference section at the end of this article for links that provide more information about these technologies

IBM’s Bluemix

IBM Bluemix is the latest Cloud offering from IBM. It enables organizations and developers to quickly and easily create, deploy, and manage applications on the Cloud. IBM Bluemix is an implementation of IBM’s Open Cloud Architecture based on Cloud Foundry, an open source Platform as a Service (PaaS). IBM Bluemix delivers enterprise-level services that can easily integrate with your cloud applications without you needing to know how to install or configure them.

Scala

Scala is a type-save programming language with powerful concurrency primitives build on top of a mature JVM technology platform. The language is designed to be clear and concise, which cuts down on boilerplate so programmers can concentrate on the logic of their codethat implements their business logic. Scala supports both functional programming and object-oriented programming styles.

Play Framework

Play Framework is an open source code written in Scala and Java. The Play Framework doesn’t support the J2EE standards, but was written to bring all of the good things from scripting frameworks such as Django, Grails, and Ruby On Rails. These frameworks use Conventions over Configuration which seeks to gain simplicity without losing flexibility. The framework matches the desired behavior by convention without having to write complex configuration files.

The Application

The application provides a REST interface which makes a request to IBM Bluemix to retrieve the API version.

Plan of Attack
  1. Use a play template to create a Scala play application.
  2. Implement the REST interface using a Play controller.
  3. Deploy the application on IBM Bluemix.
  4. Access the REST interface using a browser.
Create a Play/Scala application

The Play framework has a Scala template that will create the basic structure and files for a Play application written in Scala. To create a vanilla Play Scala Application, run:

The my-bluemix-app directory will contain the files for a Play/Scala application.

Implement the Controller

In the Play framework, Controllers provide the glue between the business logic and transport layer.  A Controller is implemented to a Request/Response API.  This Controller will make a simple REST call to IBM Bluemix to get the API version, which is provided by the api_version attribute. The Controller returns the version in the request response.

Create a file in the app/controllers directory called Bluemix.scala containing the following code:

The URL to get the IBM Bluemix API version must be passed as an environment variable when deploying the application on IBM Bluemix. In the conf/application.conf file add the following entry to pick up the environment variable BLUEMIX_INFO_URL:

Finally, define the route that will be used to access the REST interface using the browser. Routes are defined in the conf/routes configuration file. Each route consists of an HTTP method, URI pattern, and a method to be called to process the request. Add the following route to conf/routes:

Deploy the Application to IBM BlueMix

The application is now ready to be deployed to IBM Bluemix.  As Scala applications are compiled to Java bytecode, our Play application will use the default Java buildpack when it is deployed to IBM Bluemix.  Run the following command in the my-bluemix-app directory to compile a binary version of the application:

This creates a zip file target/universal/my-bluemix-app-1.0-SNAPSHOT.zip in the my-bluemix-app directory.

The next step is to create an application manifest (.yml) file to provide IBM Bluemix the information it needs to deploy the application. The YAML file should include the following information:

  1. The host name of the application.
  2. The Display name of the application.
  3. The Java buildpack
  4. The location of the zip file containing the application.
  5. The environment variable BLUEMIX_INFO_URL

In this example, the application manifest is named MyManifest.yml, and it should contain the following:

NOTE: You must change the “host: xxxBluemixApp” and “name: xxxBluemixApp” entries, since they need to be unique within the IBM Bluemix environment.

The BLUEMIX_INFO_URL is the URL of the REST API endpoint in the IBM Bluemix US South region.  It is accessible even if you deploy your application to another IBM Bluemix region.

Now all you need to do is push the application to Bluemix using the cf command:

Accessing the Rest Interface using the Browser

Once the application is pushed, you can go to the URLwe define below in your browser and get the IBM Bluemix API Version.

http://xxxbluemixapp.mybluemix.net/bluemix/api

The browser should provide output similar to the following:
{“api_version”:”2.27.0″}
Note: The actual value returned may change as IBM Bluemix is updated.
Summary

This article shows how to create a Scala application, written to the Play Framework, and deploy it to IBM Bluemix.  IBM Bluemix supports Play applications using the standard Java buildpack.

If you are interested in learning more about our organization or emerging technologies such as Scala or Spark, contact us.

References

IBM Bluemix quick-start

Tutorials on IBM Bluemix services

IBM Bluemix Documentation

IBM Bluemix Application Manifest

IBM Bluemix cf command line

Play Framework

Scala Language

Share: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditEmail this to someonePrint this page
Robert Goodman
Robert is a Senior Software Developers in IBM Emerging Internet Technologies. Over the years he has worked on a number of technologies such as Mobile, Hadoop, Watson, Bluemix and lately has be working with Scala and Apache Spark.
Robert Goodman

Latest posts by Robert Goodman (see all)

Robert Goodman

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *