Concurrency
case class User(name: String, surname: String)
class MyActor extends Actor {
def receive = {
// Matches any string at all
case s: String =>
println(s"You sent me a string: $s")
// Match a more complex case class structure
case user: User =>
println(s"You sent me a user: $user")
// Catch all. Matches any message type
case _ =>
println("Huh?")
}
}
val system = ActorSystem("MyActorSystem")
val system = ActorSystem("MyActorSystem")
val myActor: ActorRef = system.actorOf(Props[MyActor], name = "myActor")
object App {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MyActorSystem")
val myActor: ActorRef = system.actorOf(Props[MyActor], name = "myActor")
myActor ! User("Borja", "Gorriz")
}
}
class MyActor extends Actor {
def receive = {
// Match a more complex case class structure
case user: User =>
println(s"You sent me a user: $user ")
}
}
case class User(name: String, surname: String)
myActor ! User("Borja", "Gorriz")
object App {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MyActorSystem")
val myActor: ActorRef = system.actorOf(Props[MyActor], name = "myActor")
myActor ! User("Borja", "Gorriz")
}
}
class MyActor extends Actor {
def receive = {
// Match a more complex case class structure
case user: User =>
println(s"You sent me a user: $user ")
}
}
case class User(name: String, surname: String)
myActor ? User("Borja", "Gorriz")
object App {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MyActorSystem")
val myActor = system.actorOf(Props[MyActor], name = "myActor")
implicit val askTimeout = Timeout(1.second)
(myActor ? User("Borja", "Gorriz")).mapTo[String]
.onSuccess({ case response: String => println(response) })
}
}
class MyActor extends Actor {
def receive = {
case s: String =>
println(s"You sent me a string: $s")
case user: User =>
self ! "just created a user"
sender() ! s"New $user created"
}
}
case class User(name: String, surname: String)
myActor.forward(User("Borja", "Gorriz"))
object App {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MyActorSystem")
val RootActor = system.actorOf(Props[RootActor], name = "RootActor")
RootActor ! User("Borja", "Gorriz")
}
}
class RootActor() extends Actor {
def receive = {
case user: User =>
val forwardActor = context.actorOf(Props[ForwardActor])
forwardActor ! user
case msg: String => println(s"Result: $msg")
}
}
class ForwardActor() extends Actor {
def receive = {
case user: User =>
val myActor = context.actorOf(Props[MyActor])
myActor.forward(user)
}
}
class MyActor extends Actor {
def receive = {
case user: User =>
sender() ! s"New $user created"
}
}
case class User(name: String, surname: String)
pipe(Future) to MyActor
object App {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MyActorSystem")
val pipeActor = system.actorOf(Props[PipeActor], name = "pipeActor")
implicit val askTimeout = Timeout(1.second)
(pipeActor ? User("Borja","Gorriz"))
.mapTo[String].onSuccess({ case response: String => println(response) })
}
}
class MyActor extends Actor {
def receive = {
case user: User =>
sender() ! s"New $user created"
}
}
class PipeActor() extends Actor {
def receive = {
case user: User =>
implicit val askTimeout = Timeout(1.second)
val myActor = context.actorOf(Props[MyActor])
pipe(myActor ? user) to sender()
case _ =>
}
}
case class User(name: String, surname: String)
object Boot extends App {
val system = ActorSystem("MyActorSystem")
//now we can just start building actors
}
object DemoActor {
def props(magicNumber: Int): Props = Props(new DemoActor(magicNumber))
}
class DemoActor(magicNumber: Int) extends Actor {
def receive = {
case x: Int => sender() ! (x + magicNumber)
}
}
class SomeOtherActor extends Actor {
context.actorOf(DemoActor.props(42), "demo")
}