Akka:优先邮箱无法与Akka Typed一起使用

5us2dqdw  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(178)

我正在尝试创建一个自定义的优先级邮箱,该邮箱使用akka类型,根据邮件的类型来确定邮件的优先级。下面是我创建参与者并向其发送邮件的代码。但是,邮件是按照接收的顺序进行处理的,而不是按照自定义邮箱定义的顺序。

package com.akka.prac

import akka.actor.typed.{ActorSystem, Behavior, DispatcherSelector, Props, Settings}
import akka.actor.typed.scaladsl.Behaviors
import akka.dispatch.{PriorityGenerator, UnboundedPriorityMailbox}
import com.typesafe.config.{Config, ConfigFactory}

object PriorityMailBoxTyped extends App {

  val config = ConfigFactory.load("applicationtyped.conf")
  val actorSystem = ActorSystem(
    MyPriorityActorTyped.receive,
    "PriorityMailBox",
    config,
    DispatcherSelector.fromConfig("prio-dispatcher")
  )

  actorSystem ! DoubleMessage(6.0)
  actorSystem ! IntMessage(1)
  actorSystem ! DoubleMessage(5.0)
  actorSystem ! IntMessage(3)
  actorSystem ! StringMessage("Hello")
  actorSystem ! IntMessage(5)
  actorSystem ! StringMessage("I am priority actor")
  actorSystem ! StringMessage(
    "I process string messages first,then integer, long and others"
  )

}

class MyPriorityActorMailBoxTyped(settings: Settings, config: Config)
    extends UnboundedPriorityMailbox(
      // Create a new PriorityGenerator,lower prio means more important
      PriorityGenerator {
        // Int Messages
        case x: IntMessage => 1
        // String Messages
        case x: StringMessage => 0
        // Long messages
        case x: LongMessage => 2
        // other messages
        case x: DoubleMessage => 3
        case _                => 4
      }
    )

trait MyMessage
case class IntMessage(x: Int) extends MyMessage
case class LongMessage(x: Long) extends MyMessage
case class StringMessage(x: String) extends MyMessage
case class DoubleMessage(x: Double) extends MyMessage

object MyPriorityActorTyped {

  def receive: Behavior[MyMessage] = Behaviors.receive { (context, message) =>
    message match {
      case IntMessage(x)    => println(x)
      case StringMessage(x) => println(x)
      case LongMessage(x)   => println(x)
      case DoubleMessage(x) => println(x)
      case _                => println()
    }
    Behaviors.same
  }
}

配置文件

prio-dispatcher {
    mailbox-type = "com.akka.prac.MyPriorityActorMailBoxTyped"
}

同样的例子也适用于 akka 经典。我在这里错过了什么?

  • 谢谢-谢谢
yizd12fk

yizd12fk1#

这似乎是一个bug,现在已在2.6.14中修复。
Justed对它进行了测试,它可以与documentation的配置配合使用:

ActorSystem.create(Behaviors.setup(ctx -> someStaticBehavior(ctx)), "system", ConfigFactory.load("application.conf"), MailboxSelector.fromConfig("my-app.my-special-mailbox"));

相关问题