Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
- }
Add Comment
Please, Sign In to add comment