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. java jeesite.properties配置文件属性提取

    package com.thinkgem.jeesite.common.config; import java.io.UnsupportedEncodingException; import java ...

  2. docker run hello-world失败

    提示镜像拉取失败,解决方案 到 https://cr.console.aliyun.com/   注册一个账户 列表中就有 加速器 启动  Docker 端  右键  选择配置 在Docker Dae ...

  3. const限定符、constexpr和常量表达式------c++ primer

    编译器将在编译过程中把用到const变量的地方都替换成对应的值,为了执行这种替换,编译器必须知道变量的初始值.如果程序包含多个文件,则那个用了const对象的文件都必须能访问到它的初始值才行.要做到这 ...

  4. R:魔兽世界终极版

    描述 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部之间是依次排列的若干城市,城市从西向东依次编号为1,2,3 .... N ( N <= 20 ).红魔军的司令部算作编号为0 ...

  5. Codeforces Round #551 (Div. 2)B. Serval and Toy Bricks

    B. Serval and Toy Bricks time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. 5. 常见C语言字符串库函数的使用及实现

    1. strncat 函数: [函数原型]#include <string.h> char *strncat( char *str1, const char *str2, size_t c ...

  7. HTML的相关路径与绝对路径的问题---通过网络搜索整理

    问题描述:    在webroot中有个index.jsp 在index.jsp中写个表单. 现在在webroot中有个sub文件夹,sub文件夹中有个submit.jsp想得到index.jsp表单 ...

  8. 降维之主成分分析法(PCA)

    一.主成分分析法的思想 我们在研究某些问题时,需要处理带有很多变量的数据,比如研究房价的影响因素,需要考虑的变量有物价水平.土地价格.利率.就业率.城市化率等.变量和数据很多,但是可能存在噪音和冗余, ...

  9. 前端CSS的基本素养

    前端开发的三驾马车——html.css.js,先谈谈CSS CSS 前期:解决布局.特效.兼容问题 中级:网站风格的制定.色调.模块.布局方式.交互方式.逻辑设计等 高级:模块命名.类的命名.文件的组 ...

  10. PHP将数据库数据批量生成word文档

    <?php    class word{         function start(){            ob_start();            echo '<html x ...