Akka:UnboundedPriorityMailbox-是否可以通过复杂类型确定消息的优先级?
UnboundedPriorityMailbox可以选择按类型(例如INT String等)确定消息的优先级。是否可以按类型的类属性将消息确定优先级?
我的意思是,我知道该选项可用:
case x: Int => 1
// String Messages
case x: String => 0
// Long messages
case x: Long => 2
// other messages
case _ => 3
消息的顺序(优先级)将是:
myPriorityActor ! “Hello”
myPriorityActor ! “I am priority actor”
myPriorityActor ! “I process string messages first,then integer, long and others”
myPriorityActor ! 5
myPriorityActor ! 3
myPriorityActor ! 1
myPriorityActor ! 6.0
myPriorityActor ! 5.0
我想知道我是否可以按类型的类属性来优先考虑队列。
例如:
case x: Student.name=> 1
// String Messages
case x: Student.salary=> 0
// Long messages
case x: Student.School=> 2
有可能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从根本上讲,这与Akka无关。您要做的就是返回一个部分功能,该功能具有接受任何一个并返回int的接口。是的,幕后功能将用于确定消息的优先级,但是从Akka的角度来看,它对您的功能一无所知。邮箱只是调用您提供的功能,以确定收到的每个消息的相对优先级。
关键是这确实是一个Scala问题,您最好只用一个Scala标签询问您的问题:您想编写一个部分函数,该函数查看“类属性”并返回int。
我认为您的问题的答案是否定的,但我不是100%确定我会理解您的问题。我认为答案的关键是了解我刚刚说的是:这里没有魔术。您只是提供a 部分功能接受Any并返回INT(或ISN) 'T适用)。 Scala模式匹配是进行一些类型的自动检测和类型的转换,但是您只需在语句中使用Bunch
.getClass
呼叫,您就可以(在更多代码行中)进行相同的操作(在更多的代码中)。因此,您要回答的唯一真正的问题是“我可以写一个if语句吗?”
因此,如果我的功能收到“托马斯·杰斐逊”的输入,您可以编写是否将其转换为与其消息优先级相对应的int值的语句。本能地,我认为您不能。因为我希望persity.name和student.school都是字符串类型,并且您很难区分它们。因此,我没有陈述可以写的话,可以在“托马斯·杰斐逊”和“托马斯·杰斐逊”学校之间说出。但是,另一方面,这完全取决于您如何定义类型。也许您是学校的弦线。在这种情况下,您可以查看类型并说明差异。
但是,最重要的是,这只是一个将任何函数转换为INT的函数。如果您可以编写这样做的功能,则Akka将利用该功能来确定消息的优先级。
尽管我还断言这个问题在现实世界中有些意义。优先的邮箱在现实世界应用中很少见,如果您拥有它们,则可能不会发送简单的类型。大多数消息可能是某种信封,不会是简单的类型。您可以轻松地分辨出通过信封的“ HighPriorityCancel”消息和“ GetGrade”消息之间的区别。
Fundamentally, this has nothing to do with Akka. All you are doing is returning a partial function that has the interface of accepting an Any and returning an Int. Yes, behind the scenes that function will be used to determine the priority of a message, but from Akka's perspective it doesn't know anything about your function. The Mailbox just calls the function you provide to determine a relative priority for each message it receives.
The point being this is really a Scala question and you might be better off asking your question with just a Scala tag: you want to write a partial function function that looks at "class property" and returns an Int.
I think the answer to your question is no, but I'm not 100% sure I'm understanding your question. I think the key to the answer is understanding what I just said above though: there is no magic here. You are just providing a partial function that accepts an Any and returns an Int (or isn't applicable). The Scala pattern matching is doing some automatic detection of types and conversion of types, but you could do the same (in more lines of code) just by doing a bunch
.getClass
calls within if statements yourself.So the only real question you have to answer is "could I write an if statement that does this?"
Thus, if my function receives the input of "Thomas Jefferson" could you write if statements that convert that into Int value corresponding to its message priority. Instinctively, I don't think you could. Because I would expect that both Student.name and Student.School are both String types and you would have a hard time distinguishing between them. Thus there's no if statement I could write that could tell between "Thomas Jefferson" the person and "Thomas Jefferson" the school. But, on the other hand, it all depends on how you are defining your types. Maybe you've subclassed String for school. In which case you could look at the types and tell the difference.
But, again, the bottom line is that this is just a function that converts Anys into Ints. If you can write function that does that, Akka will utilize that function to determine the priority of the message.
Although I'd also assert that this problem is somewhat moot in the real world. Prioritized mailboxes are pretty rare in real world applications and in cases where you have them you are probably not sending simple types. Most messages are likely some kind of envelope and won't be a simple type. You'll easily be able to tell the difference between a "HighPriorityCancel" message and a "GetGrade" message via the envelope.