├── .gitignore ├── README.md ├── build.sbt ├── project └── build.properties └── src └── main ├── resources ├── local_application.conf └── remote_application.conf └── scala └── com └── madhukaraphatak └── akka ├── local └── LocalActor.scala └── remote └── RemoteActor.scala /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea/ 3 | *.iml 4 | target/ 5 | project/target 6 | dependency-reduced-pom.xml 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple Akka Remote example in Scala 2 | =================================== 3 | 4 | This repository contains a simple example for akka remote. It shows how to create both remote and local actors in same 5 | project.It also shows how different configurations are read and interpreted. -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | 2 | name := "akka-remote-simple-scala" 3 | 4 | version := "1.0" 5 | 6 | scalaVersion := "2.11.4" 7 | 8 | resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" 9 | 10 | libraryDependencies += 11 | "com.typesafe.akka" %% "akka-actor" % "2.3.7" 12 | 13 | libraryDependencies += 14 | "com.typesafe.akka" %% "akka-remote" % "2.3.7" 15 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 0.13.5 -------------------------------------------------------------------------------- /src/main/resources/local_application.conf: -------------------------------------------------------------------------------- 1 | akka { 2 | loglevel = "INFO" 3 | actor { 4 | provider = "akka.remote.RemoteActorRefProvider" 5 | } 6 | remote { 7 | enabled-transports = ["akka.remote.netty.tcp"] 8 | netty.tcp { 9 | hostname = "127.0.0.1" 10 | port = 0 11 | } 12 | log-sent-messages = on 13 | log-received-messages = on 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/resources/remote_application.conf: -------------------------------------------------------------------------------- 1 | akka { 2 | loglevel = "INFO" 3 | actor { 4 | provider = "akka.remote.RemoteActorRefProvider" 5 | } 6 | remote { 7 | enabled-transports = ["akka.remote.netty.tcp"] 8 | netty.tcp { 9 | hostname = "127.0.0.1" 10 | port = 5150 11 | } 12 | log-sent-messages = on 13 | log-received-messages = on 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akka/local/LocalActor.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akka.local 2 | 3 | import java.io.File 4 | 5 | import akka.actor.{Props, Actor, ActorSystem} 6 | import com.typesafe.config.ConfigFactory 7 | 8 | /** 9 | * Local actor which listens on any free port 10 | */ 11 | class LocalActor extends Actor{ 12 | @throws[Exception](classOf[Exception]) 13 | override def preStart(): Unit = { 14 | /* 15 | Connect to remote actor. The following are the different parts of actor path 16 | 17 | akka.tcp : enabled-transports of remote_application.conf 18 | 19 | RemoteSystem : name of the actor system used to create remote actor 20 | 21 | 127.0.0.1:5150 : host and port 22 | 23 | user : The actor is user defined 24 | 25 | remote : name of the actor, passed as parameter to system.actorOf call 26 | 27 | */ 28 | val remoteActor = context.actorSelection("akka.tcp://RemoteSystem@127.0.0.1:5150/user/remote") 29 | println("That 's remote:" + remoteActor) 30 | remoteActor ! "hi" 31 | } 32 | override def receive: Receive = { 33 | 34 | case msg:String => { 35 | println("got message from remote" + msg) 36 | } 37 | } 38 | } 39 | 40 | 41 | 42 | object LocalActor { 43 | 44 | def main(args: Array[String]) { 45 | 46 | val configFile = getClass.getClassLoader.getResource("local_application.conf").getFile 47 | val config = ConfigFactory.parseFile(new File(configFile)) 48 | val system = ActorSystem("ClientSystem",config) 49 | val localActor = system.actorOf(Props[LocalActor], name="local") 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akka/remote/RemoteActor.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akka.remote 2 | 3 | import java.io.File 4 | 5 | import akka.actor._ 6 | import com.typesafe.config.ConfigFactory 7 | 8 | /** 9 | * Remote actor which listens on port 5150 10 | */ 11 | class RemoteActor extends Actor { 12 | override def receive: Receive = { 13 | case msg: String => { 14 | println("remote received " + msg + " from " + sender) 15 | sender ! "hi" 16 | } 17 | case _ => println("Received unknown msg ") 18 | } 19 | } 20 | 21 | object RemoteActor{ 22 | def main(args: Array[String]) { 23 | //get the configuration file from classpath 24 | val configFile = getClass.getClassLoader.getResource("remote_application.conf").getFile 25 | //parse the config 26 | val config = ConfigFactory.parseFile(new File(configFile)) 27 | //create an actor system with that config 28 | val system = ActorSystem("RemoteSystem" , config) 29 | //create a remote actor from actorSystem 30 | val remote = system.actorOf(Props[RemoteActor], name="remote") 31 | println("remote is ready") 32 | 33 | 34 | } 35 | } 36 | 37 | 38 | --------------------------------------------------------------------------------