akka & pekko/actors
-
Akka TestProbesakka & pekko/actors 2023. 7. 18. 16:55
actor A, B 가 메시지를 교환하도록 개발되었다면, A actor 를 TestProbe 로 대체해 메시지가 잘 교환되는지 확인할 수 있다. val master = system.actorOf(Props[Master]) val slave = TestProbe("slave") // testActor 객체로 actor 가 생성? master ! "some message" expectMsg("Ack") // slave 가 받아야 할 것으로 예상되는 메시지
-
Akka Testakka & pekko/actors 2023. 7. 18. 16:30
libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % "2.6.18" libraryDependencies += "com.typesafe.akka" %% "akka-actor-testkit-typed" % "2.6.18" libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" import AkkaBasicSpec._ import akka.actor.{Actor, ActorSystem, Props} import akka.testkit.{ImplicitSender, TestKit} import org.scalatest.BeforeAndAfterAll import org.scalatest..
-
Akka Configakka & pekko/actors 2023. 7. 17. 19:53
typesafe config 를 사용 ActorSystem 을 생성할 때 옵션으로 config 를 지정할 수 있음 import akka.actor.{Actor, ActorLogging, ActorSystem, Props} import com.typesafe.config.ConfigFactory val actorSystem: ActorSystem = ActorSystem(name = "actorDemo", config=ConfigFactory.load(...)) ActorSystem 생성할 때 config 옵션이 지정되지 않으면 /resources/application.conf 파일을 읽어 설정함
-
Akka Loggingakka & pekko/actors 2023. 7. 17. 19:40
import akka.actor.{Actor, ActorLogging, ActorSystem, Props} object AkkaMain { class SimpleActor extends Actor with ActorLogging { override def receive: Receive = { case message => log.info(message.toString) case (a, b) => log.warning(s"tuple {} and {}", a, b) } } object SimpleActor { def props() = Props(new SimpleActor()) } def main(args: Array[String]): Unit = { val actorSystem: ActorSystem = A..
-
Child Actorakka & pekko/actors 2023. 7. 17. 18:23
import akka.actor.{Actor, ActorRef, ActorSystem, Props} object AkkaMain { object Parent { def props() = Props(new Parent()) case class CreateChild(name: String) case class TellChild(message: String) } import Parent.{CreateChild, TellChild} class Parent extends Actor { override def receive: Receive = { case CreateChild(name) => println(s"${self.path} creating child") val childRef = context.actorO..
-
Behavior control (become / unbecome)akka & pekko/actors 2023. 7. 17. 17:13
1. Become() 을 사용하지 않고 구현 mutable variable 로 상태 관리 import akka.actor.{Actor, ActorSystem, Props} object AkkaTest { object Kid { def props() = Props(new Kid()) case object KidAccept case object KidReject val HAPPY = "happy" val SAD = "sad" } object Mom { def props() = Props(new Mom()) case class Food(food: String) case class Ask(message: String) val VEGETABLE = "vegetable" val CHOCOLATE = "chocola..
-
Messages and Behaviorakka & pekko/actors 2023. 7. 17. 13:23
ref ! s"Forwards: $message" 1. messages can be of any type messages must be IMMUTABLE messages must be SERIALIZABLE (in practice use case class and case objects import akka.actor.{Actor, ActorSystem, Props} object TestActor { def main(args: Array[String]): Unit = { val actorSystem: ActorSystem = ActorSystem(name = "actorCapabilitiesDemo") class SimpleActor extends Actor { override def receive: R..
-
Actor 생성akka & pekko/actors 2023. 7. 17. 11:15
import akka.actor.{Actor, ActorRef, ActorSystem, Props} object AkkaMain { def main(args: Array[String]): Unit = { // name 으로는 alphabet 만 사용 가능 ('-, _, space' 도 사용 불가) val actorSystem: ActorSystem = ActorSystem(name = "firstActorSystem") println(actorSystem.name) class Person(name: String) extends Actor { // type Receive = PartialFunction[Any, Unit] override def receive: Receive = { case "hi" => ..