[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 ...
随机推荐
- c# 实现点击下载功能
转自百度知道 private void DownLoad(string strName, string strPath) { string fileName = strName;//客户端保存的文件名 ...
- c# 如何筛选datatable
对DataTable数据各种筛选 筛选一个DataTable的数据,赋值给另外一个DataTable 假设: 有2个DataTable:DataTable A.DataTable B. 要求: 筛选D ...
- C# System.Threading.Timer 定时器
前提: 需要引入 System.Threading: 描述: 在很多时间我们都需要进行延迟执行,或是定时执行一些指定业务,这个时候使用 Timer 是最合适的,而且 Timer 是Cpu 级别处理对 ...
- c# 锁的使用
1 互斥锁lock(基于Monitor实现) 定义: private static readonly object Lock = new object(); 使用: lock (Lock) { //t ...
- angular 引入第三方库
第一步 --save:自动写入package.json 第二步: 第三部: 为了让typescript识别$ 第四步:
- mysql 新建数据库与表
- C# 高性能对象映射(表达式树实现)
前言 上篇简单实现了对象映射,针对数组,集合,嵌套类并没有给出实现,这一篇继续完善细节. 开源对象映射类库映射分析 1.AutoMapper 实现原理:主要通过表达式树Api 实现对象映射 优点: . ...
- asp.net站点阻止某个文件夹或者文件被浏览器访问
一个站点根目录下面有一个Config文件夹,这个文件夹里面都是一些json格式的txt文本,文本是一种静态资源,如果知道这个文本的地址,就可以在浏览器中输入地址打开这个文本,别人就可以看到站点的配置, ...
- php代码审计4审计代码执行漏洞
代码执行漏洞代码执行漏洞是指应用程序本身过滤不严,用户可以通过请求将代码注入到应用中执行,当应用在调用一些能将字符串转化成代码的函数(如php中的eval)时,没有考虑到用户是否能控制这个字符串,造成 ...
- 【bzoj3930】选数 容斥原理+暴力
Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公 ...