[Activator- HelloAkka] Define our Messages
An Actor does not have a public API in terms of methods that you can invoke. Instead its public API is defined through messages that the actor handles.
Actor沒有一個你可以調用的公用API方法的術語, 而是它的公用API是被定義通過Actor處理信息
Messages can be of arbitrary type (any subtype of Object in Java or Any in Scala). This means that we can send boxed primitive values (such as String, Integer, Boolean etc.) as messages or plain data structures like arrays and collection types. However, since the messages are the Actor's public API, you should define messages with good names and rich semantic and domain specific meaning, even if it's just wrapping your data type. This will make it easier to use, understand and debug actor-based systems.
Now we want to define three different messages;
WhoToGreetredefines the new greetingGreetasks the Actor for latest greetingGreetingreturns the latest greeting
Let's start by defining the messages in Java (we are putting them inside an outer HelloAkkaJava class, containing our full sample).
It is very important that the messages we create are immutable (meaning that they cannot be changed), if not we run the risk of accidentally sharing state between two different Actors which will violate the Actor Model.
我們創建message是不變量是非常重要的(代表它們不能被改變), 如果不是我們執行時偶然在兩個不同的Actor共享狀態將會有違反Actor Model的風險
In this sample we will not use any remoting, but it is a good practice to always mark your messages as Serializable since then you will not run in to any runtime issues if you decide to scale out (on to multiple nodes) with Akka but forget to go back and reimplement your messages.
// Java code
public static class Greet implements Serializable {}
public static class WhoToGreet implements Serializable {
public final String who;
public WhoToGreet(String who) {
this.who = who;
}
}
public static class Greeting implements Serializable {
public final String message;
public Greeting(String message) {
this.message = message;
}
}
This is the way we would define the messages in Scala. In Scala case classes and case objects make excellent messages since they are immutable and have support for pattern matching, something we will take advantage of in the Actor when matching on the messages it has received. Another advantage case classes has is that they are marked as serializable by default.
// Scala code case object Greet
case class WhoToGreet(who: String)
case class Greeting(message: String)
[Activator- HelloAkka] Define our Messages的更多相关文章
- Fedora 24中的日志管理
Introduction Log files are files that contain messages about the system, including the kernel, servi ...
- The template engine
Play has an efficient templating system which allows to dynamically generate HTML, XML, JSON or any ...
- WM (Constants)
Create page WM (Constants) Summary WM_* Constants and their definitions or descriptions and what c ...
- Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide
Preface Validating data is a common task that occurs throughout all application layers, from the pre ...
- jms版本
Java消息服务是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914). 2001年6月25日,Java消息服务发布JMS 1.0.2b,2002年3月18日Java消息服务发布 1 ...
- 窗口过程 Wndproc
操作系统向应用程序发送一系列消息,如左键按下和左键抬起,应用程序将通过GetMessage等方法 Wndproc应用例子最终将消息提交到窗口过程(WndProc)指向一个应用程序定义的窗口过程的指针. ...
- C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入
原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...
- qt动态库实现无边框窗体的消息处理 nativeEvent的使用
需求: 在动态库中创建一个窗口句柄,可以给外部调用,库的调用者,通过这个句柄发送消息到底层库,库里面可以实现对消息的处理 m_FHandle=AllocateHWnd(WndProcDllMsg); ...
- Trapping Messages Sent to an Application
http://www.delphicorner.f9.co.uk/articles/apps7.htm Trapping Messages Sent to an Application I wrote ...
随机推荐
- 制作百度地图离线JavaScript API加载本地瓦片地图
全面介绍,请看下列介绍地址,改写目前最新版本的百度V2.0地图,已全面实现离线操作,能到达在线功能的95%以上 http://api.jjszd.com:8081/apituiguang/gistg. ...
- 21天学通C++学习笔记(三):变量和常量
1. 简述 内存是一种临时存储器,也被称为随机存取存储器(RAM),所有的计算机.智能手机及其他可编程设备都包含微处理器和一定数量的内存,用地址来定位不同的存储区域,像编号一样. 硬盘可以永久的存储数 ...
- Timer(定时器)
默认情况下,在每个采样器之前没有任何延时,这样不能很好的模拟现实生活中人们访问网页,因为现实生活中人们点击一个请求后,会有一定的时间,然后再点击下一个请求,JMeter提供了定时器来模拟这种行为. 定 ...
- kali linux之netcat
网络工具中的瑞士军刀----体积小,功能强大 侦听模式/传输模式 telnet/获取banner信息 传输文本信息,文件,目录 加密传输文件,远程控制/木马,加密所有流量(来做远程控制是非常理想的选择 ...
- C#/ASP.NET对URL中的中文乱码处理
前言:UTF-8中,一个汉字对应三个字节,GB2312中一个汉字占用两个字节. 不论何种编码,字母数字都不编码,特殊符号编码后占用一个字节. 1.直接在C#后台编码URL参数 引用类库:System. ...
- SSM整合dubbo 进行分页查询
1.先书写Mapper和sql语句 public interface ActEntityMapper { int deleteByPrimaryKey(String actId); int inser ...
- Navicat 连接Sqlite数据库的方法和步骤
1.打开Navicat,进行如下操作. 2.在弹出的新建连接对话框中输入正确的信息,点击“确定”按钮. 3.可见如下
- pscp 命令---windows和linux之间互相拷贝文件的工具
pscp -r d:\cc root@10.0.0.8:/root/test copy d:\cc content recursively into the /root/test, contains ...
- Loj 6432. 「PKUSC2018」真实排名 (组合数)
题面 Loj 题解 枚举每一个点 分两种情况 翻倍or不翻倍 \(1.\)如果这个点\(i\)翻倍, 要保持排名不变,哪些必须翻倍,哪些可以翻倍? 必须翻倍: \(a[i] \leq a[x] < ...
- 洛谷 P2048 BZOJ 2006 [NOI2010]超级钢琴
题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中A ...