Advertisement
Guest User

Untitled

a guest
Apr 15th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.60 KB | None | 0 0
  1. // remote actor
  2. main.scala
  3. ----
  4. package com.fullfacing.ticketingworker
  5.  
  6. import akka.actor.{Props, ActorSystem}
  7. import com.fullfacing.ticketingworker.actor.TicketActor
  8. import com.typesafe.config.ConfigFactory
  9.  
  10. /**
  11.  * Created by ryno on 2015/04/15.
  12.  */
  13. object main {
  14.   val name = "TicketHandlingSystem"
  15.   val system = ActorSystem(name, ConfigFactory.load())
  16.  
  17.   def shutdown(): Unit = {
  18.     system.shutdown()
  19.   }
  20.   def main(args: Array[String]) = {
  21.     system.actorOf(Props[TicketActor], "TicketActor")
  22.     sys.addShutdownHook { shutdown() }
  23.     println("started TicketHandlingSystem")
  24.   }
  25. }
  26.  
  27. ---
  28. TicketActor.scala
  29. ---
  30. package com.fullfacing.ticketingworker.actor
  31.  
  32. import akka.actor.Actor
  33.  
  34. /**
  35.  * Created by ryno on 2015/04/15.
  36.  */
  37. class TicketActor extends Actor {
  38.   override def receive: Receive = {
  39.     case msg: String =>
  40.       println(s"tickets: $msg have been booked.")
  41.       sender() ! "done booking"
  42.   }
  43. }
  44.  
  45. ---
  46. application.conf
  47. ---
  48. akka {
  49.   actor {
  50.     provider = "akka.remote.RemoteActorRefProvider"
  51.   }
  52.   remote {
  53.     enabled-transports = ["akka.remote.netty.tcp"]
  54.     netty.tcp {
  55.       hostname = "localhost"
  56.       port = 5150
  57.     }
  58.   }
  59. }
  60.  
  61.  
  62. ------------------------------------------------
  63. local actor
  64. ------------------------------------------------
  65. main.scala
  66. ---
  67. package com.fullfacing.ticketing
  68.  
  69. import akka.actor.{ActorSystem, Props}
  70. import com.fullfacing.ticketing.actor.{TestActor, ApiActor}
  71. import com.typesafe.config.ConfigFactory
  72. /**
  73.  * Created by ryno on 2015/04/15.
  74.  */
  75.  
  76. object main {
  77.   val name = "TicketHandlingSystem"
  78.   val system = ActorSystem(name, ConfigFactory.load())
  79.  
  80.   def shutdown(): Unit = {
  81.     system.shutdown()
  82.   }
  83.   def main(args: Array[String]) = {
  84.     val actor = system.actorOf(Props[TestActor], "TicketActor")
  85.     sys.addShutdownHook { shutdown() }
  86.     println("started TicketHandlingSystem")
  87.     Thread.sleep(1000)
  88.     actor ! "moo moo cow"
  89.   }
  90. }
  91.  
  92. ---
  93. TestActor.scala
  94. ---
  95. package com.fullfacing.ticketing.actor
  96.  
  97. import akka.actor._
  98.  
  99. /**
  100.  * Created by ryno on 2015/04/15.
  101.  */
  102. class TestActor extends Actor {
  103.   val handler = context.actorSelection("akka.tcp://TicketHandlingSystem@localhost:5150/user/TicketActor")
  104.   handler ! Identify(None)
  105.  
  106.   override def receive: Receive = {
  107.     case ActorIdentity(_, Some(actorRef)) =>
  108.       println("identified")
  109.       context become ready(actorRef)
  110.     case ActorIdentity(_, None) => println("its not alive")// not alive
  111.   }
  112.  
  113.   def ready(remote: ActorRef): Receive = {
  114.     case m: String => remote ! m
  115.   }
  116. }
  117.  
  118. ---
  119. application.conf
  120. ---
  121. akka {
  122.   loglevel = "DEBUG"
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement