これは、Akka Cookbookの第10章の「Enveloping actor」のレシピに関する質問です。誰かがなぜ単に "ヘッダー"関数を引数として渡すことはできないのですか?代わりに、envelopingActorを作成する際に "ヘッダー_"として渡す必要がある理由を説明してください。この場合、関数名を引数として渡せないのはなぜですか?
元のコードはここで見つけることができます:https://github.com/PacktPublishing/Akka-Cookbook/tree/master/Chapter10/src/main/scala/com/packt/chapter10
package com.packt.chapter10
import java.util.UUID
import akka.actor.{Actor, ActorRef, ActorLogging, ActorSystem, Props}
import Envelope._
object Envelope {
type Headers = Map[String, Any]
case class Envelope[T](msg: T, headers: Headers = Map.empty)
}
class EnvelopingActor(nextActor: ActorRef, addHeaders: Any => Headers) extends Actor {
def this(nextActor: ActorRef) =
this(nextActor, _ => Map())
override def receive: Receive = {
case msg => nextActor ! new Envelope(msg, addHeaders(msg))
}
}
class EnvelopeReceiver extends Actor with ActorLogging {
override def receive: Receive = {
case x => log.info(s"Received [$x]")
}
}
object EnvelopingActorApp extends App {
val actorSystem = ActorSystem()
val envelopeReceiver = actorSystem.actorOf(Props[EnvelopeReceiver], "receiver")
val envelopingActor = actorSystem.actorOf(
Props(classOf[EnvelopingActor], envelopeReceiver, headers _))
envelopingActor ! "Hello!"
def headers(msg: Any) = Map(
"t" -> System.currentTimeMillis(),
"cId" -> UUID.randomUUID().toString
)
}