我的简化Akka Camel
应用程序设置如下:
AppleProducer -> seda:appleRoute -> AppleConsumer
OrangeProducer -> seda:orangeRoute -> OrangeConsumer
我看到的是,Apple
事件间歇性地被 . 所消耗OrangeConsumer
,反之亦然。
在下面运行这个示例(可能几次)会重新创建它。
我不明白这只是间歇性地发生。我究竟做错了什么?
object TestApp extends App {
implicit val system = ActorSystem()
val camel = CamelExtension(system)
val appleProducer = system.actorOf(Props(classOf[MyProducer], "seda:appleRoute"), "AppleProducer")
system.actorOf(Props(classOf[MyAppleConsumer], "seda:appleRoute"), "AppleConsumer")
val orangeProducer = system.actorOf(Props(classOf[MyProducer], "seda:orangeRoute"), "OrangeProducer")
system.actorOf(Props(classOf[MyOrangeConsumer], "seda:orangeRoute"), "OrangeConsumer")
appleProducer ! new Apple("1")
orangeProducer ! new Orange("1")
appleProducer ! new Apple("2")
orangeProducer ! new Orange("2")
appleProducer ! new Apple("3")
orangeProducer ! new Orange("3")
appleProducer ! new Apple("4")
orangeProducer ! new Orange("4")
appleProducer ! new Apple("5")
orangeProducer ! new Orange("5")
appleProducer ! new Apple("6")
orangeProducer ! new Orange("6")
}
class MyProducer(route: String) extends Actor with ActorLogging {
def receive = {
case payload: Any =>
val template = CamelExtension(context.system).template
template.setDefaultEndpointUri(route)
template.sendBody(payload)
}
}
class MyAppleConsumer(route: String) extends Consumer with ActorLogging {
override def endpointUri: String = route
override def receive: Receive = {
case event: CamelMessage if event.body.isInstanceOf[Apple] =>
log.info("Received event {}", event.body)
case _ => throw new IllegalArgumentException("Invalid entity")
}
}
class MyOrangeConsumer(route: String) extends Consumer with ActorLogging {
override def endpointUri: String = route
override def receive: Receive = {
case event: CamelMessage if event.body.isInstanceOf[Orange] =>
log.info("Received event {}", event.body)
case _ => throw new IllegalArgumentException("Invalid entity")
}
}
class Apple(id: String)
class Orange(id: String)