Akka concurrent Programming--The seventh section: Actor model (vi) __ Concurrent

Source: Internet
Author: User

Main content:
1. Typed actor Definition
2. Typed actor Created
3. Message sent 1. Typed actor Definition

The typed actor in Akka is the implementation of the active objects design pattern, and the active objects mode will decouple the execution of the method and the invocation of the method, thus introducing concurrency into the program. The Typed actor is composed of a common interface and a corresponding implementation, and its underlying implementation uses the proxy pattern, which is implemented by using dynamic proxies in the JDK, which are automatically distributed to the object that implements the interface when the method of the interface is invoked. The definition of Typed actor [] is shown below.

Trait squarer {
    //fire-and-forget message
    def squaredontcare (i:int): Unit
    //non-blocking send-request-reply message
    def Square (I:int): Future[int]
    //blocking send-request-reply message
    def squarenowplease (i:int): Option[int]
    // Blocking send-request-reply message
    def squarenow (i:int): Int
  }

  class Squarerimpl (Val name:string) extends Squarer  {
    def this () = This ("Squarerimpl")

    def squaredontcare (i:int): unit = i * I
    def square (i:int): Future[int] = Promise.successful (i * i). Future
    def squarenowplease (i:int): option[int] = Some (i * i)
    def squarenow (I:int) : Int = i * I
  }

There are 4 methods defined in trait Squarer:
(1) def squaredontcare (i:int): Unit method: The return value type is unit, similar to the fire-and-forget message sending model in untyped actor, that is. And the Tell method call.
(2) def Square (I:int): Future[int]: The return value type is Future[int], which resembles the actor message sending model in untyped send-request-reply, that is, the? and ask method calls, This invocation is non-blocking.
(3) def squarenowplease (i:int): Option[int]: The return value type is Option[int] (Option class can be scala.option[_) or Akka.japi.Option 2. Create typed Actor

Create an typed actor instance with the following code.

Create typed Actor
val mysquarer:squarer =typedactor (System) by default constructors directly. Typedactorof (Typedprops[squarerimpl) ())
//Create typed actor directly through the default constructor and specify typed actor name
val mysquarer:squarer =typedactor (System). Typedactorof ( Typedprops[squarerimpl] (), "Mysquarer")
//creates typed actor with a Non-default constructor and specifies typed actor name
val othersquarer:squarer = Typedactor (System). Typedactorof (Typedprops (classof[squarer],new squarerimpl ("Squarerimpl")), "Othersquarer"

The above code demonstrates the use of constructors and Non-default constructors to create typed Actor, where squarer is the type of the proxy, squarerimpl the type of the implementation. 3. Message Sending

Fire-forget message Send
  mysquarer.squaredontcare (a)

  //send-request-reply message Send
  val osquare = Mysquarer.squarenowplease (a)

  val isquare = Mysquarer.squarenow (a)

  //request-reply-with-future message sent
  Val fsquare = Mysquarer.square (a)
  val result = Await.result (Fsquare, 5 second)

Code Mysquarer.squaredontcare (10) is a one-way message sent, and the method executes asynchronously on another thread; Val osquare = Mysquarer.squarenowplease (10), Val isquare = Mysquarer.squarenow (10) is sent for request-reply messages, executed in a blocking manner for a specified period of time, for the. Squarenowplease (10) method returns none if the result is not returned within the corresponding time. Otherwise, the return value is the Option[int] type, and the Squarenow (10) method throws an exception java.util.concurrent.TimeoutException if there is no return value within the corresponding time, otherwise the INT type value is returned; Val Fsquare = Mysquarer.square (10) is sent as a request-reply-with-future message, executed in a non-blocking manner, through Val result = Await.result (Fsquare, 5 Second) to obtain execution results. The complete code is shown below.

* * Typed Actor */Object example_01 extends App {import akka.event.Logging import scala.concurrent.{ Promise, Future} import akka.actor. {typedactor, typedprops} import scala.concurrent.duration._ trait squarer {//fire-and-forget message def squared
    Ontcare (i:int): unit//non-blocking send-request-reply message def Square (i:int): Future[int]//blocking send-request-reply message def squarenowplease (I:int): Option[int]//blocking send-request-reply message def squarenow (i:int): Int} class Squar Erimpl (Val name:string) extends Squarer {def this () = This ("Squarerimpl") def squaredontcare (i:int): unit = i * I def Square (i:int): future[int] = promise.successful (i * i). Future def squarenowplease (i:int): option[int] = Some (i * i) def squarenow (i:int): Int = i * i} val system = Actorsystem ("Typedactorsystem") val log = Logging (System, This.getclass)//Use default constructor to create typed Actor val mysquarer:squarer = Typedactor (System). Typedactorof (TypeDprops[squarerimpl] (), "mysquarer")//Create typed Actor val othersquarer:squarer = Typedactor (System) by using a Non-default constructor. ty Pedactorof (Typedprops (classof[squarer), New Squarerimpl ("Squarerimpl")), "Othersquarer")//fire-forget message send M Ysquarer.squaredontcare (a)//send-request-reply message send val osquare = Mysquarer.squarenowplease (a) log.info ("OSquare
  = "+osquare) val isquare = Mysquarer.squarenow (a) log.info (" isquare= "+isquare)//request-reply-with-future message forwarding Val Fsquare = mysquarer.square val result = Await.result (Fsquare, 5 second) log.info ("fsquare=" +result) system . Shutdown ()}

The results of the code run are as follows:
[INFO] [03/21/2016 21:15:50.592] [main] [Example12_9 (Akka://typedactorsystem)]osquare=some (100) [ info][03/21/201621:15:50.649][main][example129 (Akka://typedactorsystem)] osquare=some [INFO] [03/21/2016 21:15:50.649] [main] [Example12_9 (Akka://typedactorsystem)] isquare=100
[INFO] [03/21/2016 21:15:50.649] [main] [ example12_9$ (Akka://typedactorsystem)] fsquare=100

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.