首页  

akka actor demo     所属分类 akka 浏览量 663
官网例子用的 sbt 
sbt 不太好用 ,改成 maven 


import akka.actor.{Actor, ActorSystem, Props}

class HelloActor extends Actor {
  def receive = {
    // case "hello"  => println("hello back to you.")
    case msg: String => println("msg=" + msg)
    case _ => println("what?")
  }
}

object Hello extends App {
  // actor need an ActorSystem
  val system = ActorSystem("HelloSystem")
  // create and start the actor
  val helloActor = system.actorOf(Props[HelloActor], name = "helloActor")
  // send two messages
  helloActor ! "hello"
  helloActor ! "what"
  helloActor ! "java"
  helloActor ! "scala"

  // shutdown the actor system
  system.terminate()
}


可以不继承 App scala.App object Hello2 { def main(args: Array[String]): Unit = { // actor need an ActorSystem val system = ActorSystem("HelloSystem") // create and start the actor val helloActor = system.actorOf(Props[Hello2Actor], name = "helloActor") // send two messages helloActor ! "hello" helloActor ! "java" helloActor ! "scala" helloActor ! "cpp" // shutdown the actor system system.terminate() } }
完整代码 https://gitee.com/dyyx/hellocode/blob/master/demo/scala/akka/akka-quickstart-scala-maven/src/main/java/com/example/dyyx/hello/Hello.scala

上一篇     下一篇
scala 函数

scala case class

How is Akka used in Play

akka http rest api demo

c语言 hello world 高逼格版本

IDEA 源码阅读技巧