Akka Quick Start

Source: Internet
Author: User

Akka has many advantages, such as high performance, high reliability, high concurrency, distributed, fault tolerant, extensible, event-driven, and not described. Different versions of the API vary widely, and this article runs on Scala 2.10.3 and Akka 2.3.2.

<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactid>akka-actor_2.10 </artifactId>
  <version>2.3.2</version>
</dependency>
<dependency>
  <groupId>org.scala-lang</groupId>
  <artifactId>scala-library</artifactId>
  <version>2.10.3</version>
</dependency>

definition

Defining the actor is simple, inheriting akka.actor.Actor, and implementing the Receive method.

Class Hello extends Actor {
  def receive = {case
    msg:string = println ("Hello" + msg) Case _ = =
    pri NTLN ("unexpected message.")
  }
}
Start

Creating an actor instance needs to pass Actorsystem.

Val system = Actorsystem ("Hellosystem")
val hello = system.actorof (Props[hello], name = "Hello")
val hello1 = Syst Em.actorof (Props[hello])
val Hello2 = system.actorof (Props (New Hello ()))

If you want to continue creating the child actor in the actor, you need to use the built-in Actorcontext object.

Context.actorof (Props[children], name = "Children")

If you want to create a remote Actor, you need to pass the Actorselection method, the original Actorfor method is no longer used.

Context.actorselection ("Akka.tcp://helloremotesystem@127.0.0.1:5150/user/remoteactor")
Send Message

Giant simple, that's one! You can send any type of message that is asynchronous.

Hello! "Bruce"
Hello! 10086

The sending of synchronous messages requires the use of future objects.

Implicit val timeout = timeout (5 seconds)
val future = hello? "Sha"
val result = Await.result (future, Timeout.duration). asinstanceof[string]
Stop

There are two ways to stop an actor.

One is through the internal actorcontext.stop () method, the method will be children actor layer after kill, and then Ziwen.

def receive = {case
    ' stop ' = = Context.stop (self) ...
  }

Another is the external feed poison, through the Actorref.tell () method to achieve. The latter parameter is to who reply, here obviously does not need, pass empty.

Hello.tell (Poisonpill.getinstance, Actorref.nosender);
Hum Example

Heng is a two-Buddhist temple of God is commonly known as a king of the Lord. Ming Dynasty novel "Seal God Romance" author Chen Zhonlin accordingly Emily just two members of God will, image mighty ferocious. A Zhenglen, can nose hum white air to make enemy; a Chen Qi, can the mouth ha Huang Qi to seize.

Object Hengha extends App {
  val system = Actorsystem ("Henghasystem")
  val ha = System.actorof (props[ha], name = "Ha ")
  val heng = system.actorof (Props (New Heng (ha)), name =" Heng ")

  Heng!" Start "
}
Class Heng (ha:actorref) extends Actor {
  def receive = {case
    "start" = ha!) "Heng" Case
    "ha" = 
      println ("Ha")
      ha! "Heng" case
    _ = = println ("Heng What?")
  }
}
Class Ha extends Actor {
  def receive = {case
    "heng" = 
      println ("hum")
      sender! "Ha" Case
    _ = = println ("Ha What?")
  }
}

Run up, the result:

Hum and Hum
...
Remote Example Local Project

Application.conf

Akka {
  loglevel = "DEBUG"
  actor {
    Provider = "Akka.remote.RemoteActorRefProvider"
  }
  Remote {
    enabled-transports = ["Akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 5155
    }
 }
}

Object Local extends App {
  val system = Actorsystem ("LocalSystem")
  val localactor = system.actorof (props[ Localactor], name = "Localactor")//The local actor
  Localactor! "Start"//Start the action
}
Class Localactor extends Actor {
  //Create the remote actor
  val remote = context.actorselection ("Akka.tcp://hello Remotesystem@127.0.0.1:5150/user/remoteactor ")
  var counter = 0

  def receive = {case
    " START "
      + = Remote! "Hello from the Localactor" case
    msg:string =
      println (S "localactor received message: ' $msg '")

      if ( Counter < 5) {
        sender! "Hello Back to You"
        counter + = 1}}

Remote Engineering

Application.conf

Akka {
  loglevel = "DEBUG"
  actor {
    Provider = "Akka.remote.RemoteActorRefProvider"
  }
  Remote {
    enabled-transports = ["Akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 5150
    }
 }
}

Object Helloremote extends App {
  val system = Actorsystem ("Helloremotesystem")
  val remoteactor = System.actorof ( Props[remoteactor], name = "Remoteactor")

  Remoteactor! "The Remoteactor is Alive"
}
Class Remoteactor extends Actor {
  def receive = {case
    msg:string =
      println (S "Remoteactor received Messag E ' $msg ')
      sender! "Hello from the Remoteactor"
  }
}

Original link: http://ibruce.info/2014/05/20/hello-akka/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.