zoukankan      html  css  js  c++  java
  • [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;

    • 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)
  • 相关阅读:
    .net中实现运行时从字符串动态创建对象
    C# 用 VB.net 函數庫 實現全角與半角轉換
    實現.net 加載插件方式
    VS2008下載
    Lotus Notes Send EMail from VB or VBA
    用C#写vs插件中的一些Tip
    SQL2005中异常处理消息框可直接使用
    C#路径/文件/目录/I/O常见操作汇总
    利用.net反射动态调用指定程序集的中的方法
    说说今年的计划
  • 原文地址:https://www.cnblogs.com/YangMark/p/3765936.html
Copyright © 2011-2022 走看看