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;

  • WhoToGreet redefines the new greeting
  • Greet asks the Actor for latest greeting
  • Greeting returns 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的更多相关文章

  1. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  2. The template engine

    Play has an efficient templating system which allows to dynamically generate HTML, XML, JSON or any ...

  3. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

  4. 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 ...

  5. jms版本

    Java消息服务是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914). 2001年6月25日,Java消息服务发布JMS 1.0.2b,2002年3月18日Java消息服务发布 1 ...

  6. 窗口过程 Wndproc

    操作系统向应用程序发送一系列消息,如左键按下和左键抬起,应用程序将通过GetMessage等方法 Wndproc应用例子最终将消息提交到窗口过程(WndProc)指向一个应用程序定义的窗口过程的指针. ...

  7. C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入

    原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...

  8. qt动态库实现无边框窗体的消息处理 nativeEvent的使用

    需求: 在动态库中创建一个窗口句柄,可以给外部调用,库的调用者,通过这个句柄发送消息到底层库,库里面可以实现对消息的处理 m_FHandle=AllocateHWnd(WndProcDllMsg); ...

  9. Trapping Messages Sent to an Application

    http://www.delphicorner.f9.co.uk/articles/apps7.htm Trapping Messages Sent to an Application I wrote ...

随机推荐

  1. mac 地址查询

    mac 地址由 6个字节(48bit)组成.前3个字节是厂商代码.输入厂商名称可查询相应的代码. http://standards.ieee.org/develop/regauth/oui/publi ...

  2. 宏(使用注意事项、主要用途)------c++程序设计原理与实践(进阶篇)

    使用宏的时候一定要小心:在c中没有真正有效的方法来避免使用宏,但宏带有严重的副作用,因为宏不遵守通常的c(或c++)作用域和类型规则——它只是一种文本替换.   宏的使用注意事项: 所以宏名全部大写. ...

  3. iOS开发-导出profile文件

    1.登陆苹果开发者网站 苹果开发者中心,点击 Certificates, Identifiers & Profiles 2.导出Provisioning Profiles 2.1 点击右上脚加 ...

  4. 【bzoj2500】幸福的道路 树形dp+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  5. Python3之os模块

    一:简介 os模块主要用于提供系统高级别的操作. 二:常用方法 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录 os.chflags( ...

  6. SDUT OJ 数据结构上机测试1:顺序表的应用

    数据结构上机测试1:顺序表的应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  7. Android--Apache HttpClient 的一些问题

    1,对于Android4.0之上的环境下,不能在主线程中访问网络    http://www.cnblogs.com/plokmju/p/Android_apacheHttpClient.html   ...

  8. js 有用信息集

    1.java.cookie.js 库:轻易操作cookie 2.jquery.form.js 库:通过ajaxForm,ajaxsubmit 两个函数,将form转为ajax提交方式:https:// ...

  9. Qt C++ libcaffe.so无法调用

    在用QT编写调用caffe的工程时,发现一直有个错误: error while loading shared libraries libcaffe.so.1.0.0-rc3: cannot open ...

  10. Linux下指定线程的名字

    为了能方便的区分一个进程中的每个线程,可以通过prctl()给每个线程取个名字.这样在会创建多个线程的程序执行过程中,就能知道一个pid或tid对应的是哪个线程,对调试程序有一定帮助. prctl是个 ...