akka actors默认邮箱介绍
1. UnboundedMailbox is the default unbounded MailboxType used by Akka Actors
”无界邮箱“ 是akka actors默认使用的邮箱,
UnboundedMailbox继承了MailboxType
/**
* MailboxType is a factory to create MessageQueues for an optionally
* provided ActorContext.
*
* <b>Possibly Important Notice</b>
*
* When implementing a custom mailbox type, be aware that there is special
* semantics attached to `system.actorOf()` in that sending to the returned
* ActorRef may—for a short period of time—enqueue the messages first in a
* dummy queue. Top-level actors are created in two steps, and only after the
* guardian actor has performed that second step will all previously sent
* messages be transferred from the dummy queue into the real mailbox.
*/
trait MailboxType {
def create(owner: Option[ActorRef], system: Option[ActorSystem]): MessageQueue
}
和ProducesMessageQueue
trait ProducesMessageQueue[T <: MessageQueue]
MailboxType特质的create方法接受两个参数owner和system,owner表示“消息队列”的所有者,system表示Actor系统
UnboundedMailbox的定义如下:
/**
* UnboundedMailbox is the default unbounded MailboxType used by Akka Actors.
*/
case class UnboundedMailbox() extends MailboxType with ProducesMessageQueue[UnboundedMailbox.MessageQueue] { // 使用this关键字来调用构造函数。
// 它演示了如何从其他构造函数调用构造函数。必须确保this必须放在构造函数中的第一个语句,同时调用其他构造函数this(),否则编译器会抛出错误。
// “=”等号右边调用的this()是指向的UnboundedMailBox()类
def this(settings: ActorSystem.Settings, config: Config) = this() // 重写MailboxType的create()方法,并创建UnboundedMailbox.MessgaeQueue对象
final override def create(owner: Option[ActorRef], system: Option[ActorSystem]): MessageQueue =
new UnboundedMailbox.MessageQueue
}
UnboundedMailbox中的create()方法中创建的UnboundedMailbox.MessgaeQueue对象是
object UnboundedMailbox {
class MessageQueue extends ConcurrentLinkedQueue[Envelope] with UnboundedQueueBasedMessageQueue {
final def queue: Queue[Envelope] = this
}
}
MessageQueue继承了ConCurrentLinkedQueue,并且ConCurrentLinkedQueue是存放的Envelope
final case class Envelope private (val message: Any, val sender: ActorRef)
(信封)对象,信封对象中包含一个message消息和发送者sender的ActorRef。
此外,MessageQueue还with了一个UnboundedQueueBasedMessageQueue
/**
* BoundedMessageQueueSemantics adds bounded semantics to a QueueBasedMessageQueue,
* i.e. blocking enqueue with timeout.
*/
trait BoundedMessageQueueSemantics {
def pushTimeOut: Duration
} trait UnboundedQueueBasedMessageQueue extends QueueBasedMessageQueue with UnboundedMessageQueueSemantics {
// 入队,这里是入队了一个信封,貌似receiver没有用到?不明白为什么要有这个receiver,可能是为了明显标记是发给那个Actor的吧
def enqueue(receiver: ActorRef, handle: Envelope): Unit = queue add handle
// 出队,出队就是从队列中取出一个元素(一封信)
def dequeue(): Envelope = queue.poll()
}
UnboundedQueueBasedMessageQueue中实现了从QueueBasedMessageQueue继承来的enqueue()和dequeue()方法。更确切的话是从MessageQueue继承来的,因为QueueBasedMessageQueue是继承了MessageQueue,代码如下:
/**
* This is a marker trait for message queues which support multiple consumers,
* as is required by the BalancingDispatcher.
*/
trait MultipleConsumerSemantics /**
* A QueueBasedMessageQueue is a MessageQueue backed by a java.util.Queue.
*/
trait QueueBasedMessageQueue extends MessageQueue with MultipleConsumerSemantics {
def queue: Queue[Envelope]
// 消息的数量 (队列的元素个数)
def numberOfMessages = queue.size
// 是否包含消息 (队列是否为空)
def hasMessages = !queue.isEmpty
// 清空消息 (清空队列)
def cleanUp(owner: ActorRef, deadLetters: MessageQueue): Unit = {
if (hasMessages) {
// 获取第一个消息
var envelope = dequeue
while (envelope ne null) {
// 如果消息不为空,则不断向deadLetters发送消息
// deadLetters也是一个消息队列actor.dispatcher.mailboxes.deadLetterMailbox.messageQueue
deadLetters.enqueue(owner, envelope)
envelope = dequeue
}
}
}
}
从QueueBasedMessageQueue的定义来看,其中包含了几个重要的方法。还定义了一个queue,回顾一下UnboundedMailbox的定义
object UnboundedMailbox {
class MessageQueue extends ConcurrentLinkedQueue[Envelope] with UnboundedQueueBasedMessageQueue {
final def queue: Queue[Envelope] = this
}
}
其中的queue是重写了QueueBasedMessageQueue中定义的queue,def queue: Queue[Envelope]。
3. 邮箱是akka里面是怎么使用的?
如果我们是使用的ActorRef去创建一个actor,例如:
val testActor: ActorRef = context.actorOf(
TestActor.props().withDispatcher("test-actor-dispatcher"),
TestActor.Name)
akka actors默认邮箱介绍的更多相关文章
- MySQL 中的默认数据库介绍
MySQL 中的默认数据库介绍:https://dataedo.com/kb/databases/mysql/default-databases-schemas 默认数据库 官方文档 informat ...
- 以Akka为示例,介绍Actor模型
许多开发者在创建和维护多线程应用程序时经历过各种各样的问题,他们希望能在一个更高层次的抽象上进行工作,以避免直接和线程与锁打交道.为了帮助这些开发者,Arun Manivannan编写了一系列的博客帖 ...
- 【cypress】6. cypress的默认文件结构介绍
通过之前的一些介绍,已经大概其明白cypress是个啥,但是具体使用的细节点还有很多,需要一步步的去学习. 在安装好cypress之后,会生成一个默认项目,这个项目结构里的各个文件夹是干嘛使的呢? 一 ...
- 支持SMTP邮箱介绍
126邮箱:POP:POP.126.comSMTP:SMTP.126.comhttp://mail.126.com/help/client_04.htm 163邮箱:POP:pop.163.comSM ...
- 翻译:AKKA笔记 - 介绍Actors
任何以前做过多线程的人都不会否认管理多线程程序是困难并且痛苦的. 我说管理是因为它开始很容易而且当你看到性能提升时会很兴奋.但是,当你看到你没法从子线程的错误中恢复 或者 这些僵尸bug很难重现 或者 ...
- 设置Office 365邮箱默认发送和接收邮件大小限制
Office 365默认的 35MB 的邮件大小限制.Office 365 最大是支持 150MB 的邮件体积的. 我们只需用 Windows Powershell 连接 Office 365 ,然后 ...
- Akka Typed 官方文档之随手记
️ 引言 近两年,一直在折腾用FP与OO共存的编程语言Scala,采取以函数式编程为主的方式,结合TDD和BDD的手段,采用Domain Driven Design的方法学,去构造DDDD应用(Dom ...
- Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark Streaming简介 1.1 概述 Spark Streaming 是Spa ...
- 分布式应用框架Akka快速入门
转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...
随机推荐
- Codeforces 999F Cards and Joy(二维DP)
题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...
- hdu1024线性dp
/* dp[i][j]表示取第i个数时分成了j块 要么是将第i个数加入j块中的最后一块,要么是自成一块,加上前面j-1块的和 状态转移方程: dp[i][j]=max(dp[i-1][j]+a[i], ...
- Highcharts实现图形报表(我主要实现javaweb开发的图形报表)
官网网址:https://www.hcharts.cn/ 中文版的(参考起来方便,你懂的.):http://www.mamicode.com/info-detail-446038.html 网上已经有 ...
- Flink(三)Flink开发IDEA环境搭建与测试
一.IDEA开发环境 1.pom文件设置 <properties> <maven.compiler.source>1.8</maven.compiler.source&g ...
- 【BZOJ3626】[LNOI2014]LCA
题解: 数据结构套路真多.. 思考了一下线段树分治和启发式合并发现复杂度并不怎么对.. 看了题解发现都是套路啊 考虑一下lca(x,y)的dep有什么性质 我们把1-x的路径+1,查询1-y的路径和 ...
- Error: The INF file contains Unicode characters that could not be converted correctly
昨天第一次为自己的windows mobile程序制作CAB安装包,但是在生成过程中,却出现了这样一个问题: 编译完成 -- 0 个错误,0 个警告time -> G:\WindowsMobil ...
- Educational Codeforces Round 26 E - Vasya's Function
数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我 ...
- k8s 关键字以及管理流程。
一.流程图如下 二.用户通过kubectl提交需要运行的docker container(pod). 三.api server把请求存储在etcd里面. 四.scheduler(调度)扫描,分配机器. ...
- BZOJ4972 八月月赛 Problem B 小Q的方格纸 二维前缀和
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4972 八月月赛Problem B 题目概括 一个矩阵,一坨询问,问矩阵中一个特定方向的等腰直角三角 ...
- this关键字的用法(包括this语句)
一:概述 this代表它所在函数所属对象的引用. 简单说:哪个对象在调用this所在的函数,this就代表哪个对象. 二:关于运用的程序 同龄人的比较. 三:关于this语句的使用 用于构造函数之间进 ...