저는 Scala와 Akka를 처음 접했지만 간단한 시나리오가 있습니다. URL 목록을 가져 와서 각 URL을 http GET 요청을 할 새 액터에 메시지로 보내려고합니다. 16 개의 URL 목록만으로는 약 40-65 %의 성공률을 얻었으며 다른 사람들은 "메시지가 배달되지 않았습니다"라는 데드-레터를 받았습니다. 그러나 액터를 만들기 전에 Thread.sleep (50)을 넣으면 데드-레터 미스가 발생하지 않습니다. 참고 : HTTP Get은 요소 일 수도 있고 아닐 수도있는 스칼라 클래스가 아닌 Java 클래스를 사용합니다. 아래 코드 예제를 참조하십시오 ... Akka가 메시지를 보장하지 않는다는 것을 알고 있지만이 성공률이 옳지 않은 것 같습니다. 잘못하고있는 것 같습니다. 어떤 조언을 해주셔서 감사합니다.
def ParallelTest(urls: List[String]): String = {
val system = ActorSystem("HelloSystem")
var cnt: Int = 0
for (item <- urls){
Thread.sleep(50)
createActor(system, "helloactor" + cnt, item)
cnt += 1
/*
historical example
helloActor ! "hello"
helloActor ! "hello"
helloActor ! "Buenos dias"
*/
}
system.shutdown
println("parallel done");
"done"
}
def createActor(actorSystem: ActorSystem, actorName: String, urlItem: String) = {
val helloActor = actorSystem.actorOf(Props[HelloActor], name = actorName)
helloActor ! UrlTransport(urlItem)
}
배우가 언제 끝났는지 알 수 있도록 매니저를 두는 등 이와 같은 일을하는 것을 고려할 수 있습니다.
class HelloManager(urls: List[String]) extends Actor {
var completed = 0
def remaining = urls.size - completed
def receive: Receive = {
case StartSystem => startRequests
case RequestComplete => handleComplete
}
def startRequests(): Unit = {
for ((url, i) <- urls.zipWithIndex) {
val helloActor = context.actorOf(Props[HelloActor], name = s"helloActor$i")
helloActor ! UrlTransport(url)
}
}
def handleComplete(): Unit = {
completed += 1
if (remaining == 0)
// do something like
// context.stop(self)
}
}
그런 다음 sender ! RequestComplete
작업이 끝나면 HelloActor가 필요합니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다