original:http://charless.org/?p=105
Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages. Scala programs run on the Java VM, are byte code compatible with Java so you can make full use of existing Java libraries or existing application code.
Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM.
Sbt is a build tool for Scala and Java projects that aims to do the basics well.
In this tutorial, I will show you how to quickly setup your environment to build and run the akka-sample-chat, a sample that comes with the akka distribution, that implements a simple chat system.
- - Linux with bash
- - A Java runtime version 1.6 or later
- - less than 1 hour
Setting up your local environment
Required environment
charless@linux-bash:~$ cd $WORKDIR
charless@linux-bash:~/Work$ JAVA_HOME=/opt/jdk1.6.0_24/
charless@linux-bash:~/Work$ $JAVA_HOME/bin/java -version
java version ”1.6.0_24″
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)
Install SBT
SBT is a build tool for Scala projects that aims to do the basics well. It requires Java 1.6 or later.
- Download sbt-launch.jar
- Create the launcher
- Run sbt
Let’s begin by getting the sbt jar:
charless@linux-bash:~$ cd bin
charless@linux-bash:~/bin$ curl -C - -O http://typesafe.artifactoryonline.com/typesafe/ivy-releases/org.scala-tools.sbt/sbt-launch/0.11.0/sbt-launch.jar
(…)
charless@linux-bash:~/bin$ ls
sbt-launch.jar
To create the launcher, create a file named sbt in ~/bin with the content of the following gist
By using shell commands:
(…)
charless@linux-bash:~/bin$ chmod ugo+rx sbt
Open a new terminal and type the following command to test sbt (this may take some minutes as the first run downloads all dependencies):
charless@linux-bash:/tmp$ ~/bin/sbt
(….)
[info] Set current project to default-8c3933 (in build file:/tmp/)
Ctrl-D
charless@linux-bash:/tmp$
If you have an error like this one
:: problems summary :: :::: WARNINGS [NOT FOUND ] commons-codec#commons-codec;1.2!commons-codec.jar (0ms) ==== Maven2 Local: tried file:///home/charless/.m2/repository/commons-codec/commons-codec/1.2/commons-codec-1.2.jar
Make sure to delete the directory of the uncomplete artifact from your maven local directory (“~/.m2/path/to/uncomplete/artifact”) and the”~/.ivy2″ directory; restart then the “sbt” command.
For the example:
rm -rf /home/charless/.m2/repository/commons-codec/commons-codec/1.2 rm -rf ~/.ivy2
Create the chat application
- Create the project akka-chat-sample
- Run the chat application with
sbt console
By default, sbt works purely by convention. sbt will find the following automatically:
- - Sources in the base directory
- - Sources in
src/main/scalaorsrc/main/java - - Tests in
src/test/scalaorsrc/test/java - - Data files in
src/main/resourcesorsrc/test/resources - - jars in
lib
You can run the project with sbt run or enter the Scala REPL with sbt console. sbt console sets up your project’s classpath so you can try out live Scala examples based on your project’s code.
For our example, we will build the following project in $WORKDIR/akka-sample-chat:
build.sbt
src/main/scala/ChatServer.scala
First, prepare the project:
charless@linux-bash:~/Work$ cd akka-sample-chat/
charless@linux-bash:~/Work/akka-sample-chat$ mkdir -p src/main/scala
Create a file named build.sbt in the $WORKDIR/akka-sample-chat directory with the content of the following gist
Then, create a file named ChatServer.scala in the $WORKDIR/akka-sample-chat/src/main/scala directory with the content of the following gist
By using shell commands:
(…)
charless@linux-bash:~/Work/akka-sample-chat$ cd src/main/scala/
charless@linux-bash:~/Work/akka-sample-chat/src/main/scala$ curl -C - -O https://raw.github.com/gist/1299742/db527df6babc07827293f5ffefcd57a47ea8a39b/ChatServer.scala
(…)
charless@linux-bash:~/Work/akka-sample-chat/src/main/scala$ ls
ChatServer.scala
charless@linux-bash:~/Work/akka-sample-chat/src/main/scala$ cd ../../..
charless@linux-bash:~/Work/akka-sample-chat$
Launch the sbt console from the $WORKDIR/akka-sample-chat project directory and type the following commands:
import sample.chat._import akka.actor.Actor._val chatService = actorOf[ChatService].start()
To do so, open a new terminal and type the following:
charless@linux-bash:~$ cd $PROJECTDIR
charless@linux-bash:~/Work/akka-sample-chat$ ~/bin/sbt console
[info] Set current project to AkkaSampleChat (in build file:/home/charless/Work/akka-sample-chat/)
(…)
Welcome to Scala version 2.9.1.final (OpenJDK Server VM, Java 1.6.0_22).
scala› import sample.chat._
import sample.chat._
scala› import akka.actor.Actor._
import akka.actor.Actor._
scala› val chatService = actorOf[ChatService].start()
[INFO] [10/19/11 11:45 PM] [run-main] [ChatService] Chat server is starting up…
(…)
[INFO] [10/19/11 11:45 PM] [run-main] [MemoryChatStorage] Memory-based chat storage is starting up…
(…)
scala›
In another terminal, launch the sbt console form the $WORKDIR/akka-sample-chat project directory and run the chat simulation run:
charless@linux-bash:~$ cd $PROJECTDIR
charless@linux-bash:~/Work/akka-sample-chat$ ~/bin/sbt console
[info] Set current project to AkkaSampleChat (in build file:/home/charless/Work/akka-sample-chat/)
(…)
Welcome to Scala version 2.9.1.final (OpenJDK Server VM, Java 1.6.0_22).
scala› import sample.chat._
import sample.chat._
scala› ClientRunner.run
[GENERIC] [10/19/11 11:57 PM] [RemoteClientStarted(akka.remote.netty.NettyRemoteSupport@acd5d4,localhost/127.0.0.1:2552)]
[GENERIC] [10/19/11 11:57 PM] [RemoteClientConnected(akka.remote.netty.NettyRemoteSupport@acd5d4,localhost/127.0.0.1:2552)]
CHAT LOG:
jonas: Hi there
CHAT LOG:
jonas: Hi there
patrik: Hello
CHAT LOG:
jonas: Hi there
patrik: Hello
jonas: Hi again
scala›
Run it again to see full speed after first initialization, and the chat log growing up.
Conclusion
In this article, we have seen how to install sbt and use it to build and test the akka-sample-chat application.
In a next article, we will see how to work on an akka project by using Eclipse.