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进行简要的说明.引用资料 ...
随机推荐
- poj2823 单调队列初步
什么是单调队列:头元素一直是队列当中的最大值,队列中的值按照递减顺序排列,可以从末尾插入一个元素,或从两段删除元素 1.插入元素,为了保证队列的单调性(这里假设为递减性),在插入元素v时要将对位的元素 ...
- 性能测试六:jmeter进阶之Cookie与header管理器
一.http cookie管理器 可以在浏览器中抓取到cookie信息,然后通过http cookie管理器为http请求添加cookie信息 添加cookie管理器后,Jmeter可以自动处理coo ...
- python接口自动化测试二十六:使用pymysql模块链接数据库
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/5/28 18:51# @Author : StalloneYang# ...
- Android Strings.xml To CSV / Excel互转
Android Strings.xml To CSV/Excel互转https://blog.csdn.net/hzxpyjq/article/details/50686983https://blog ...
- poj 1789 每个字符串不同的字母数代表两个结点间的权值 (MST)
题目大意是就是给出n个长度为7的字符串,每个字符串代表一个车,定义车的距离是两个字符串间不同字母的个数,题目要求的数不同的车的距离的最小值,即所求的就是最小生成树 Sample Input 4aaaa ...
- mongo 复制集命令
1.登录primary2.use admin >rs.add("new_node:port") 或 rs.add({"_id":4,"host& ...
- hdu 2825
题解: ac自动机+dp的题目 差不多都一个套路 记录枚举了i位,匹配到自动机上的x位,然后对于匹配了哪些单词状态压缩一下就可以了 代码: #include <bits/stdc++.h> ...
- Kubernetes学习系列
这段时间项目组内想要引入Kubernetes,作为第二代容器调度引擎,故最近在系统的学习Kubernetes.整理了一些学习笔记,心得,放到博客中,一来记录自己的学习经过,二来看能否帮到有需要的同学. ...
- 练习题|MySQL
MySQL主要内容: 1.数据库介绍.类型.特性2.MySQL数据库安装.连接.启动.停止3.表字段类型介绍.主键约束.表创建语句4.常用增删改查语句.分组.聚合5.外键管理.unique字段.表结构 ...
- pyrhon SQLite数据库
pyrhon SQLite数据库 目录 介绍 导入模块 创建数据库/打开数据库 创建表 在表中插入行 查询/修改 删除表中的行 删除表 介绍 Python SQLITE数据库是一款非常小巧的嵌入式开源 ...