akka & pekko
-
fibonacci 수열 구현하기akka & pekko/streams 2023. 7. 26. 18:23
akka/pekko streams 를 이용해 fibonacci 수열을 출력한다. cyclic graph 를 구성하게 되는데, 이때 deadlock 이 발생할 수 있다. (특히 Merge 할 때) https://pekko.apache.org/docs/pekko/current//stream/stream-graphs.html#graph-cycles-liveness-and-deadlocks 이를 대응하기 위해, Merge 대신에 MergePreferred 를 사용했다. import org.apache.pekko.actor.ActorSystem import org.apache.pekko.stream.scaladsl.{Broadcast, Flow, GraphDSL, MergePreferred, RunnableGra..
-
stream 처리 완료 후 ActorSystem 종료하기akka & pekko/streams 2023. 7. 22. 18:03
toMat + run, runWith 로 실행하면 최종 리턴값이 Future 가 되는데, Future 의 onComplete method 를 이용해 종료한다. import org.apache.pekko.Done import org.apache.pekko.actor.ActorSystem import org.apache.pekko.stream.scaladsl.{Flow, Keep, Sink, Source} import scala.concurrent.Future object TestMain { def main(args: Array[String]): Unit = { implicit val system: ActorSystem = ActorSystem("pekko") val source = Source[Int](1..
-
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..