• Akka 入门


      <!-- 定义一下常量 -->
        <properties>
            <encoding>UTF-8</encoding>
            <scala.version>2.11.8</scala.version>
            <scala.compat.version>2.11</scala.compat.version>
            <akka.version>2.4.17</akka.version>
        </properties>
    
        <dependencies>
            <!-- 添加scala的依赖 -->
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>${scala.version}</version>
            </dependency>
    
            <!-- 添加akka的actor依赖 -->
            <dependency>
                <groupId>com.typesafe.akka</groupId>
                <artifactId>akka-actor_${scala.compat.version}</artifactId>
                <version>${akka.version}</version>
            </dependency>
    
            <!-- 多进程之间的Actor通信 -->
            <dependency>
                <groupId>com.typesafe.akka</groupId>
                <artifactId>akka-remote_${scala.compat.version}</artifactId>
                <version>${akka.version}</version>
            </dependency>
        </dependencies>
    View Code

    依赖:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>Seascala</artifactId>
            <groupId>com.sea</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>AkkaDemo</artifactId>
    
    
        <dependencies>
            <!--scala -->
            <!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library -->
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>2.13.1</version>
            </dependency>
            <!-- 配置akka actor 依赖-->
            <dependency>
                <groupId>com.typesafe.akka</groupId>
                <artifactId>akka-actor_2.13</artifactId>
                <version>2.6.3</version>
            </dependency>
            <!-- 多进程之间的通信akka actor 依赖-->
            <dependency>
                <groupId>com.typesafe.akka</groupId>
                <artifactId>akka-remote_2.13</artifactId>
                <version>2.6.3</version>
            </dependency>
        </dependencies>
        <build>
            <!--指定源码位置-->
            <sourceDirectory>src/main/scala</sourceDirectory>
            <testSourceDirectory>src/test/scala</testSourceDirectory>
        </build>
    
    
    </project>

    Demo:

    package com.sea.f01
    
    import akka.actor.{Actor, ActorRef, ActorSystem, Props}
    
    class SayHelloActor extends Actor {
      override def receive: Receive = {
    
        case "hello" => println("您好!")
        case "xf" => println("xf  hahhahah!")
        case _ => println("您是?")
    
      }
    }
    
    
    object SayHelloActorDemo {
      //创建一个ActorSystem  ,专门用于创建actor
      private val actoryFactory = ActorSystem("actoryFactory")
     //创建一个Actor的同时,返回一个actor 的引用actorref
      //说明:
    //Props[SayHelloActor]  使用反射创建了一个SayHelloActor 实例
    //  "sayHelloActor" 给 actor 取名
    //  sayHelloActorActorRef 就是 Props[SayHelloActor]  的ActorRef
      private val sayHelloActorRef: ActorRef = actoryFactory.actorOf(Props[SayHelloActor], "sayHelloActor")
    
    
    
      //test
      def main(args: Array[String]): Unit = {
        //给SayHelloActor 发消息
        sayHelloActorRef ! "xf"
        print("over")
      }
    
    
    }

     Demo2

    import akka.actor.{Actor, ActorRef, ActorSystem, Props}
    
    class Aactor(actorRef:ActorRef)  extends  Actor{
      override def receive: Receive = {
        case "start" =>{
              println("Aactior start ")
          actorRef ! "我打"
        }
        case "我打" =>{
           .sleep(1000)
          println("Aactior  我也被打了 我要开始反击了 XXX")
          sender() ! "我打"
        }
    
      }
    }
    
    
    class Bactor extends  Actor {
      override def receive: Receive ={
    
        case "我打"=>{
          Thread.sleep(1000)
          println("Bactior 被打了 , 要开始反击了 jj")
           sender() ! "我打"
        }
    
      }
    }
    
    
    
    
    
    
    object  testABActor {
       val actorFacory: ActorSystem = ActorSystem("actorFacory")
       val bactorRef: ActorRef = actorFacory.actorOf(Props[Bactor], "bactor")
       val actorRef: ActorRef = actorFacory.actorOf(Props(new Aactor(bactorRef)), "actor")
    
      def main(args: Array[String]): Unit = {
        actorRef ! "start"
      }
    }
  • 相关阅读:
    Word中如何让两个图片垂直居中对齐
    Visio对象插入Word后周围空白过大
    Matlab绘图时横坐标重叠怎么办
    Word中设置所有西文字体为新罗马
    最小二乘法为什么使用误差平方和
    python 网络编程——客户端
    padding
    Ubuntu apache
    网上流行的各开源框架与技术
    Ubuntu下使用SVN
  • 原文地址:https://www.cnblogs.com/lshan/p/12587104.html
Copyright © 2020-2023  润新知