Guest User

Untitled

a guest
Jan 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import akka.actor.AbstractActor
  2. import akka.actor.ActorRef
  3. import akka.actor.ActorSystem
  4. import akka.actor.Props
  5. import com.typesafe.config.Config
  6. import com.typesafe.config.ConfigFactory
  7.  
  8. class Worker : AbstractActor() {
  9. override fun createReceive(): Receive? {
  10. return receiveBuilder()
  11. .matchAny { msg -> println(msg) }
  12. .build()
  13. }
  14. }
  15.  
  16. fun main(args: Array<String>) {
  17.  
  18. val actorSystemName = "ExampleActorSystem"
  19. val actorName = "workerActor"
  20. val localMsgs = listOf("Hello World!", "Another message.", "Good bye!")
  21. val remoteMsgs = listOf("Hello Universe!", "Another message to all aliens.", "Good luck!")
  22.  
  23. // Create the actor system
  24. val system = ActorSystem.create(actorSystemName)
  25.  
  26. // Local actor
  27. val localWorkerActorRef = system.actorOf(Props.create(Worker::class.java), actorName)
  28. localMsgs.forEach { msg -> localWorkerActorRef.tell(msg, ActorRef.noSender()) }
  29.  
  30. // Remote example, see application.conf for configuration
  31. val config: Config = ConfigFactory.load()
  32. val remoteHost: String = config.getString("akka.remote.netty.tcp.hostname")
  33. val remotePort: Int = config.getInt("akka.remote.netty.tcp.port")
  34. val remoteWorkerActorRef = system.actorSelection("akka.tcp://$actorSystemName@$remoteHost:$remotePort/user/$actorName")
  35. remoteMsgs.forEach { msg -> remoteWorkerActorRef.tell(msg, ActorRef.noSender()) }
  36.  
  37. // Terminate the actor system
  38. system.terminate()
  39.  
  40. }
Add Comment
Please, Sign In to add comment