第96讲:Akka第一个案例动手实战main方法实现中ActorSystem等代码详解学习笔记

来源:互联网 发布:中原g7 知乎 编辑:程序博客网 时间:2024/06/05 09:02

96讲:Akka第一个案例动手实战main方法实现中ActorSystem等代码详解学习笔记

本讲讲解main方法的具体实现。

1.ActorSystem : 通过ActorSystem.create(name: String)创建ActorSystem

2.main入口代码实现详解

 

ActorSystem可以看作为container级别(容器型)的内容。其中有很多Actor实例。

 

/**

 * An actor system is a hierarchical group of actors which share common

 * configuration, e.g. dispatchers, deployments, remote capabilities and 

 * addresses. It is also the entry point for createing or looking up actors.

 * 在几百台机器的分布式集群上找到其他机器上的actor时,

 * 就需要用查找的方式,这就需要ActorSystem.

 */

abstract class ActorSystem extends ActorRefFactory {

  import ActorSystem._

  def name: String

  def settings: Settings

  def logConfiguration(): Unit

def create(): ActorSystem = apply()

 

/**

  * Creates a new ActorSystem with the specified name , 

  * obtains the current ClassLoader by first inspecting the current threads' getContextClassLoader,

  * then tries to walk the stack to find the callers class Loader, then falls back to the ClassLoader

  * associated with the ActorSystem class.

  * Then it loads the default reference configurationg using the ClassLoader.

  */

def create(name: String): ActorSystem = apply(name)

 

/**

  * Creates a new ActorSystem with the name "default", and the specified Config, then

  * obtains the current ClassLoader by first inspecting the current threads' getContextClassLoader,

  * then tries to walk the stack to find the callers class Loader, then falls back to the ClassLoader

  * associated with the ActorSystem class.

  */

def create(name: String, config: Config): ActorSystem = apply(name, config)

 

/**

 * Creates a new ActorSystem with the name "default", the specified Config, and specified ClassLoader

 */

def create(name: String, config: Config. classLoader: ClassLoader):ActorSystem = apply(name,config,classLoader)

 

public class HelloAkka {

  public static void main(String[] args) throws Exception {

    ActorSystem _system =  ActorSystem.create("HelloAkka");

    ActorRef master = _system.actorOf(new Props(MasterActor.class),"master");

    master.tell("Hi! My name is Rocky. I'm so so so so happy to ne here. ");

    master.tell("Today, I'm going to read a news article for you. ");

    master.tell("I hope I hope you'll like it.")

    //

    Thread.sleep(500);    //现在暂不考虑500mm是否足够。

    master.tell(new Result());    //

    Thread.sleep(500);

    _system.shutdown();  //关闭ActorSystem,这是销毁和资源回收的部分。

  }

}

 

 

通过new Props(MasterActor.class)创建masterActor

通过tell的方式给masterActor发信息。

masterActoronReceive中就会收到信息。

masterActor处理信息的方式是把信息发给mapActor

消息发送完后睡眠一段时间后再发消息给masterActor

MasterActor再把消息发给AggregateActor

 

AggregateActor收到信息时会打印finalReduceMap

也可以把结果发回给masterActor

 

这是从java角度编写akka代码的完整过程。

下一讲从scala角度看如何用akka框架。

 

 

以上内容是从王家林老师DT大数据课程第96讲的学习笔记。
DT大数据微信公众账号:DT_Spark  

王家林老师QQ:1740415547 

王家林老师微信号:18610086859

第94讲视频网站地址:

17173视频:http://v.17173.com/v_102_608/MjgxNDIwMTc.html

6视频:http://v.ku6.com/show/bVwNIlcmDvkr4j3xantZHw...html

56视频:http://www.56.com/u89/v_MTM4NzE2NTkw.html

DT大数据梦工厂1至101集scala的所有视频、PPT和代码在百度云盘的链接:http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group


0 0
原创粉丝点击