Sunday, April 20, 2014

How do I change the default port (9000) that Play uses ?

http://stackoverflow.com/questions/8205067/how-do-i-change-the-default-port-9000-that-play-uses-when-i-execute-the-run


Play 1.x

Change the http.port value in the conf/application.conf file or pass it command line:
play run --http.port=8080

Play 2.x - Dev Mode

For browser-reload mode:
play "run 8080"
For continuous-reload mode:
play "~run 8080"

Play 2.x - Debug Mode

To run in debug mode with the http listener on port 8080, run:
play debug "run 8080"

Play 2.x - Prod Mode

Start in Prod mode:
play "start -Dhttp.port=8080"

Play 2.x - Staged Distribution

Create a staged distribution:
play stage
For Play 2.0.x and 2.1.x use the target/start script (Unix Only):
target/start -Dhttp.port=8080
For Play 2.2.x use the appropriate start script in the target/universal/stage/bin directory:
target/universal/stage/bin/[appname] -Dhttp.port=8080
With Play 2.2.x on Windows:
target\universal\stage\bin\[appname].bat -Dhttp.port=8080

Play 2.x - Zip Distribution

To create a zip distribution:
play dist
For Play 2.0.x and 2.1.x use the start script (Unix Only) in the extracted zip:
start -Dhttp.port=8080
For Play 2.2.x use the appropriate script in the [appname]-[version]/bin directory:
[appname]-[version]/bin/[appname] -Dhttp.port=8080
With Play 2.2.x on Windows:
[appname]-[version]\bin\[appname].bat -Dhttp.port=8080

Thursday, April 17, 2014

How to run an Actor ?

http://doc.akka.io/docs/akka/2.2.0/scala/hello-world.html

As a Scala developer you will probably want to tell us that there is no main(Array[String]) method anywhere in these classes, so how do we run this program? The answer is that the appropriate main method is implemented in the generic launcher class akka.Main which expects only one command line argument: the class name of the application’s main actor. This main method will then create the infrastructure needed for running the actors, start the given main actor and arrange for the whole application to shut down once the main actor terminates. Thus you will be able to run the above code with a command similar to the following:
> java -classpath <all those JARs> akka.Main com.example.HelloWorl

import akka.actor.Actor
import akka.actor.Props
class HelloWorld extends Actor {
  override def preStart(): Unit = {
  // create the greeter actor
  val greeter = context.actorOf(Props[Greeter], "greeter")
  // tell it to perform the greeting
  greeter ! Greeter.Greet
  }

def receive = {
  // when the greeter is done, stop this actor and with it the application
  case Greeter.Done ⇒ context.stop(self)
  }
}


object Greeter { 
  case object Greet
  case object Done
}
class Greeter extends Actor {

  def receive = {
   case Greeter.Greet ⇒
   println("Hello World!")
   sender ! Greeter.Done
  }
}

 

Tuesday, April 15, 2014

Simple Scala REST example using Spray

http://sysgears.com/articles/building-rest-service-with-scala/
https://github.com/oermolaev/simple-scala-rest-example

Enterprise application might be developed using a variety of architectural styles. And REST is one of the most powerful of them. It allows us to build simple, scalable, highly productive APIs with independent components on the basis of widespread standards like HTTP, MIME etc, engaging their true potential.
Let's discuss how to create a lightweight, but full-featured RESTful service from scratch.
Consider building REST [1] service that doesn't contain any complicated functionality, but provide basic CRUD operations and have following HTTP endpoints as an API:
  • POST /customer/ to create Customer.
  • GET /customer// to retrieve specific Customer.
  • PUT /customer// to update specific Customer.
  • DELETE /customer// to delete specific Customer.
  • GET /customer/ to search for Customers with specific parameters.

Technology stack

Scala was chosen as the foundation for the REST service we are going to implement. On the Scala website [2] it is described as a "general purpose programming language designed to express common programming patterns in a concise, elegant and type-safe way. Also, it smoothly integrates features of object-oriented and functional languages."
Despite the fact that Scala is a relatively new language and may have some drawbacks, there are several attractive elements, that made it not just an "another new programming language":
  • Operating on the JVM. It is known fact that Java is de facto the most popular programming language for enterprise. Many libraries are written in Java. A variety of tools are designed for JVM. Environments have been stable for years. It can be rather risky to change the entire programming stack even if this step may provide apparent benefits. But Scala has a great interoperability with existing Java code because it still operates on the JVM, has compatible byte code and allows Scala applications to use most of the JVM libraries. Furthermore, developers are able to apply their existing Java skills after migration. Therefore, Scala could be integrated into enterprise infrastructure quite smoothly.
  • Functional programming. Except that Scala is a pure object-oriented language it provides syntactic sugar for functional programming. Like a pattern matching, anonymous and higher-order functions, carrying, immutable collections, etc.
  • Concise and powerful syntax. Code size is reduced significantly compared to an equivalent application written in Java. This may improve development performance: less key-strokes to make, easier code review and testing. Moreover, many features like function passing and type inference can reduce syntactic overhead.
  • Static typing. Scala is equipped with a rich and balanced type system. It provides compile time constraints that could help to avoid certain erroneous scenarios. On the other hand, a local type inference mechanism allows developers not to annotate the program with redundant type information.
The following software was also used:
  •  SBT (Simple Build Tool) - Build tool for Scala and Java projects. Maven or Gradle with appropriate Scala plug-ins can be used as well, however, SBT became de facto the number one build tool for Scala. It is easy-to-use but quite powerful utility. [3]
  •  Akka - Asynchronous event-driven middleware framework implemented in Scala, for building high performance and reliable distributed applications. Akka decouples business logic from low-level mechanisms such as threads, locks and non-blocking IO. [4]
  •  Spray - Scala framework for building RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, modular, testable. [5]
  •  Slick - Database query and access library for Scala. It provides a toolkit to work with stored data almost as using Scala collections. Features an extensible query compiler which can generate code for different backends. [6]
  •  MySQL - Well-known open-source RDBMS. [7]
  •  Lift-json - Parsing and formatting utilities library for JSON. [8]
  •  Logback - Fast and stable logging utility. Considered as a successor to the log4j project. Natively implements the SLF4J API. [9]

Spray + Bootstrap

https://bitbucket.org/diversit/spray-twirl-bootstrap
https://github.com/spray/spray-template

"Spray - Twirl - Twitter Bootstrap" Template Project

This project is based on the Spray Template project. I added support for serving static files, added Twitter Bootstrap files and added Twirl for templating. You can use this project as a template for a REST API or even serve a website.
Spray routing is split up in serveral traits for static routing, twirl pages and inline html.
This project uses Git Flow.
Follow these steps to get started:
  1. Git-clone this repository.
    $ git clone git@bitbucket.org:diversit/spray-twirl-bootstrap.git my-project
    
  2. Change directory into your clone:
    $ cd my-project
    
  3. Launch SBT:
    $ sbt
    
  4. Compile everything and run all tests:
    > test
    
    Note: For the Selenium Chome test to be able to run, you need to install thechromedriver locally. Read my blogpost about how Selenium is setup for ScalaTest. And read the ScalaTest Selenium documentation for using Selenium in ScalaTests.
  5. Start the application:
    > re-start
    
  6. Browse to http://localhost:8080/
  7. Start the application:
    > re-stop
    
  8. Start hacking on src/main/scala/com/example/MyService.scala



Spray Project
This projects provides a starting point for your own spray-routing endeavors. There are 8 branches, providing templates for spray-routing on
  • spray-can, Scala 2.9 + Akka 2.0 + spray 1.0 (the on_spray-can_1.0 branch)
  • spray-can, Scala 2.10 + Akka 2.1 + spray 1.1 (the on_spray-can_1.1 branch)
  • spray-can, Scala 2.10 + Akka 2.2 + spray 1.2 (the on_spray-can_1.2 branch)
  • spray-can, Scala 2.10 + Akka 2.3 + spray 1.3 (the on_spray-can_1.3 branch)
  • Jetty, Scala 2.9 + Akka 2.0 + spray 1.0 (the on_jetty_1.0 branch)
  • Jetty, Scala 2.10 + Akka 2.1 + spray 1.1 (the on_jetty_1.1 branch)
  • Jetty, Scala 2.10 + Akka 2.2 + spray 1.2 (the on_jetty_1.2 branch)
  • Jetty, Scala 2.10 + Akka 2.3 + spray 1.3 (the on_jetty_1.3 branch)
You are currently on the on_spray-can_1.1 branch.
> git clone git://github.com/spray/spray-template.git my-project -b on_spray-can_1.3

Thursday, April 10, 2014

AKKA 2.2.3 tutorial




http://www.reactive.io/tips/2014/03/28/getting-started-with-actor-based-programming-using-scala-and-akka/
http://roestenburg.agilesquad.com/2010/07/upgrading-to-akka-master-010-and-scala.html
 





http://saburi380.blogspot.com/2014/01/akka-223.html

It is the notes about how can you easily try on a single server, the Akka 2.2.3. ( Http://Akka.Io/ ) Akka 2.2.3 in CentOS6 on, + Java 7 a (OpenJDK) + scala 2.10. The multiple servers (three or 
four) example is, here .