国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 服务器 > Akka并发编程——第七节:Actor模型(六)

Akka并发编程——第七节:Actor模型(六)

来源:程序员人生   发布时间:2016-07-04 16:08:26 阅读次数:2546次

主要内容:
1. Typed Actor定义
2. Typed Actor创建
3. 消息发送

1. Typed Actor定义

Akka中的Typed Actor是Active Objects设计模式的实现,Active Objects模式将方法的履行和方法的调用进行解耦合,从而为程序引入并发性。Typed Actor由公用的接口和对应实现两部份构成,其后面深层次的实现使用的是代理模式,即通过使用JDK中的动态代理来实现,在调用接口的方法时自动分发到实现接口的对象上。Typed Actor的定义[ ]以下所示。

trait Squarer { //fire-and-forget消息 def squareDontCare(i: Int): Unit //非阻塞send-request-reply消息 def square(i: Int): Future[Int] //阻塞式的send-request-reply消息 def squareNowPlease(i: Int): Option[Int] //阻塞式的send-request-reply消息 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 }

trait Squarer中定义了4个方法:
(1)def squareDontCare(i: Int): Unit方法:返回值类型为Unit,它类似于Untyped Actor中的fire-and-forget消息发送模型,即!和tell方法调用。
(2)def square(i: Int): Future[Int]:返回值类型为Future[Int],它类似于Untyped Actor中的send-request-reply消息发送模型,即?和ask方法调用,此种调用是非阻塞的。
(3)def squareNowPlease(i: Int): Option[Int]:返回值类型为Option[Int](Option类可以是scala.Option[_]也能够是akka.japi.Option

2. 创建Typed Actor

通过以下代码创建Typed Actor实例。

//直接通过默许的构造函数创建Typed Actor val mySquarer: Squarer =TypedActor(system).typedActorOf(TypedProps[SquarerImpl]()) //直接通过默许的构造函数创建Typed Actor并指定Typed Actor名称 val mySquarer: Squarer =TypedActor(system).typedActorOf(TypedProps[SquarerImpl](),"mySquarer") //通过非默许的构造函数创建Typed Actor并指定Typed Actor名称 val otherSquarer: Squarer = TypedActor(system).typedActorOf(TypedProps(classOf[Squarer],new SquarerImpl("SquarerImpl")), "otherSquarer")

上面代码演示的是使用构造函数和非默许构造函数创建Typed Actor,其中Squarer为代理的类型,SquarerImpl为具体实现的类型。

3. 消息发送

//fire-forget消息发送 mySquarer.squareDontCare(10) //send-request-reply消息发送 val oSquare = mySquarer.squareNowPlease(10) val iSquare = mySquarer.squareNow(10) //Request-reply-with-future 消息发送 val fSquare = mySquarer.square(10) val result = Await.result(fSquare, 5 second)

代码mySquarer.squareDontCare(10)是单向消息发送,方法将在另外1个线程上异步地履行;val oSquare = mySquarer.squareNowPlease(10)、val iSquare = mySquarer.squareNow(10)为Request-reply消息发送,在特定时间内以阻塞的方式履行,对.squareNowPlease(10)方法如果在对应时间内没有返回结果则返回值为None,否则返回值为Option[Int]类型,对squareNow(10)方法如果在对应时间内无返回值则会抛出异常java.util.concurrent.TimeoutException,否则返回Int类型值;val fSquare = mySquarer.square(10)为Request-reply-with-future式的消息发送,以非阻塞的方式履行,可以通过val result = Await.result(fSquare, 5 second)获得履行结果。完全代码以下所示。

/* * 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消息 def squareDontCare(i: Int): Unit //非阻塞send-request-reply消息 def square(i: Int): Future[Int] //阻塞式的send-request-reply消息 def squareNowPlease(i: Int): Option[Int] //阻塞式的send-request-reply消息 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 } val system = ActorSystem("TypedActorSystem") val log = Logging(system, this.getClass) //使用默许构造函数创建Typed Actor val mySquarer: Squarer = TypedActor(system).typedActorOf(TypedProps[SquarerImpl](),"mySquarer") //使用非默许构造函数创建Typed Actor val otherSquarer: Squarer = TypedActor(system).typedActorOf(TypedProps(classOf[Squarer], new SquarerImpl("SquarerImpl")), "otherSquarer") //fire-forget消息发送 mySquarer.squareDontCare(10) //send-request-reply消息发送 val oSquare = mySquarer.squareNowPlease(10) log.info("oSquare="+oSquare) val iSquare = mySquarer.squareNow(10) log.info("iSquare="+iSquare) //Request-reply-with-future 消息发送 val fSquare = mySquarer.square(10) val result = Await.result(fSquare, 5 second) log.info("fSquare="+result) system.shutdown() }

代码运行结果以下:
[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)] iSquare=100
[INFO] [03/21/2016 21:15:50.649] [main] [Example12_9$(akka://TypedActorSystem)] fSquare=100

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生