Last active
January 8, 2018 19:52
-
-
Save thomasweitzel/fa8d2691e0d8fbf207fe6f0451cee088 to your computer and use it in GitHub Desktop.
Simple Kotlin Akka example (local and remote)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.AbstractActor | |
import akka.actor.ActorRef | |
import akka.actor.ActorSystem | |
import akka.actor.Props | |
import com.typesafe.config.Config | |
import com.typesafe.config.ConfigFactory | |
class Worker : AbstractActor() { | |
override fun createReceive(): Receive? { | |
return receiveBuilder() | |
.matchAny { msg -> println(msg) } | |
.build() | |
} | |
} | |
fun main(args: Array<String>) { | |
val actorSystemName = "ExampleActorSystem" | |
val actorName = "workerActor" | |
val localMsgs = listOf("Hello World!", "Another message.", "Good bye!") | |
val remoteMsgs = listOf("Hello Universe!", "Another message to all aliens.", "Good luck!") | |
// Create the actor system | |
val system = ActorSystem.create(actorSystemName) | |
// Local actor | |
val localWorkerActorRef = system.actorOf(Props.create(Worker::class.java), actorName) | |
localMsgs.forEach { msg -> localWorkerActorRef.tell(msg, ActorRef.noSender()) } | |
// Remote example, see application.conf for configuration | |
val config: Config = ConfigFactory.load() | |
val remoteHost: String = config.getString("akka.remote.netty.tcp.hostname") | |
val remotePort: Int = config.getInt("akka.remote.netty.tcp.port") | |
val remoteWorkerActorRef = system.actorSelection("akka.tcp://$actorSystemName@$remoteHost:$remotePort/user/$actorName") | |
remoteMsgs.forEach { msg -> remoteWorkerActorRef.tell(msg, ActorRef.noSender()) } | |
// Terminate the actor system | |
system.terminate() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
akka { | |
actor { | |
provider = remote | |
} | |
remote { | |
enabled-transports = ["akka.remote.netty.tcp"] | |
netty.tcp { | |
hostname = "127.0.0.1" | |
port = 9005 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment