`
sillycat
  • 浏览: 2552019 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Akka(2)Start with first Project - First Part

 
阅读更多
Akka(2)Start with first Project - First Part
Use actor to calculate the value of Pi.

Create the project
Install the Typesafe akka-scala-sbt project template first.

Try to get the scala akka project template:
>g8 typesafehub/akka-scala-sbt
Akka 2.0 Project Using Scala and sbt
organization [org.example]: com.sillycat.easyakka
name [Akka Project In Scala]: easyakka
akka_version [2.0.1]:
version [0.1-SNAPSHOT]:
Applied typesafehub/akka-scala-sbt.g8 in easyakka

Try to get the scala akka demo source code:
>g8 typesafehub/akka-first-tutorial-scala
Akka 2.0 project using Scala and sbt
organization [org.example]: com.sillycat.easyakka
package [org.example]: com.sillycat.easyakka
name [Akka Pi Calculation Tutorial In Scala]: easyakka
akka_version [2.0.3]:
version [0.1-SNAPSHOT]:
artifact_id [akka-pi-scala]: easyakka
Skipping existing file: easyakka/README
Applied typesafehub/akka-first-tutorial-scala.g8 in easyakka

>cd easyakka
>sbt update
>sbt eclipse

Import the project to my eclipse.

Run the project with simple build tool
>sbt compile
>sbt run

Everything works fine here.

Run it from eclipse:
I just open the file Pi.scala and right click on that and run scala application.

Error Message:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;

Solution:
That is mostly related to using different versions of Scala.
I change my low version of eclipse and scala to 2.9.2. That works fine. I do not want to speed time to solve the version problem.
I just want to understand how the actor works in akka. So let's move on.

Try to understand the Design and Implementation
Import the package:
import akka.actor._
import akka.routing.RoundRobinRouter
import akka.util.Duration
import akka.util.duration._

Design
Master ----> Worker(N)
       <----
Master ----> Listener

Messages:
Calculate - sent to the Master actor to start the calculation
Work - send from the Master actor to the Worker actors
Result - send from the Worker actors to the Master actor
PiApproximation - send from the Master actor to the Listener actor

Messages sent to actors should always be immutable to avoid sharing mutable state. 'case class' make excellent messages.

  sealed trait PiMessage
  case object Calculate extends PiMessage
  case class Work(start: Int, nrOfElements: Int) extends PiMessage
  case class Result(value: Double) extends PiMessage
  case class PiApproximation(pi: Double, duration: Duration)

A sealed trait can be extended only in the same file than its declaration.

Creating the worker
  class Worker extends Actor {
//get the start and number
    def calculatePiFor(start: Int, nrOfElements: Int): Double = {
      var acc = 0.0
      for (i <- start until (start + nrOfElements))
        acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1)
      acc   //return the latest line
    }

    //receive the task data from message, sender back once we are done
    def receive = {
      case Work(start, nrOfElements) =>
        sender ! Result(calculatePiFor(start, nrOfElements)) // perform the work
    }
  }

We send the message back to the sender using the sender reference.

In Akka the sender reference is implicitly passed along with the message so that the receiver can always reply or store away the sender reference for future use.

Creating the master
The master actor is a little bit more involved.

  class Master(nrOfWorkers: Int,
    nrOfMessages: Int,
    nrOfElements: Int,
    listener: ActorRef) extends Actor {  // listener is a ActorRef we can send message

    var pi: Double = _
    var nrOfResults: Int = _
    val start: Long = System.currentTimeMillis //get the start time

    //define the number of workers and using round-robin router
    val workerRouter = context.actorOf(
      Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")

    def receive = {
      case Calculate =>
        for (i <- 0 until nrOfMessages)
          workerRouter ! Work(i * nrOfElements, nrOfElements) // divide the tasks, send all tasks to workerRouter
      case Result(value) =>       // case it is the sender message about result from the worker
        pi += value
        nrOfResults += 1
        if (nrOfResults == nrOfMessages) { //get all the messages back
          listener ! PiApproximation(pi, duration = (System.currentTimeMillis - start).millis)
          context.stop(self) // stop the master actor
        }
    }
  }

Our master have 3 parameters.
nrOfWorkers      how many workers we should start up
nrOfMessages     how many chunks to send out to the workers
nrOfElements     how big the number chunks send to each worker

The ActorRef is used to report the final result to the outside world.


References:
http://doc.akka.io/docs/akka/2.1.0/
http://doc.akka.io/docs/akka/2.1.0/general/index.html

https://github.com/typesafehub
http://typesafe.com/resources/tutorials/getting-started-with-akka-scala.html#getting-started-with-akka-scala

http://alvinalexander.com/scala/java.lang.nosuchmethoderror-scala.predef.augmentstring-error

http://stackoverflow.com/questions/11203268/what-is-a-sealed-trait



分享到:
评论

相关推荐

    akka-actor-2.11-2.5.19-API文档-中文版.zip

    赠送jar包:akka-actor_2.11-2.5.19.jar; 赠送原API文档:akka-actor_2.11-2.5.19-javadoc.jar; 赠送源代码:akka-actor_2.11-2.5.19-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.5.19.pom; 包含...

    akka-stream_2.11-2.5.21-API文档-中文版.zip

    赠送jar包:akka-stream_2.11-2.5.21.jar; 赠送原API文档:akka-stream_2.11-2.5.21-javadoc.jar; 赠送源代码:akka-stream_2.11-2.5.21-sources.jar; 赠送Maven依赖信息文件:akka-stream_2.11-2.5.21.pom; ...

    akka-actor_2.11-2.5.21-API文档-中文版.zip

    赠送jar包:akka-actor_2.11-2.5.21.jar; 赠送原API文档:akka-actor_2.11-2.5.21-javadoc.jar; 赠送源代码:akka-actor_2.11-2.5.21-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.5.21.pom; 包含...

    akka-stream_2.11-2.5.21-API文档-中英对照版.zip

    赠送jar包:akka-stream_2.11-2.5.21.jar; 赠送原API文档:akka-stream_2.11-2.5.21-javadoc.jar; 赠送源代码:akka-stream_2.11-2.5.21-sources.jar; 赠送Maven依赖信息文件:akka-stream_2.11-2.5.21.pom; ...

    akka-actor_2.11-2.4.20-API文档-中英对照版.zip

    赠送jar包:akka-actor_2.11-2.4.20.jar; 赠送原API文档:akka-actor_2.11-2.4.20-javadoc.jar; 赠送源代码:akka-actor_2.11-2.4.20-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.4.20.pom; 包含...

    akka-protobuf_2.11-2.4.20-API文档-中英对照版.zip

    赠送jar包:akka-protobuf_2.11-2.4.20.jar; 赠送原API文档:akka-protobuf_2.11-2.4.20-javadoc.jar; 赠送源代码:akka-protobuf_2.11-2.4.20-sources.jar; 赠送Maven依赖信息文件:akka-protobuf_2.11-2.4.20....

    akka-actor_2.11-2.5.19-API文档-中英对照版.zip

    赠送jar包:akka-actor_2.11-2.5.19.jar; 赠送原API文档:akka-actor_2.11-2.5.19-javadoc.jar; 赠送源代码:akka-actor_2.11-2.5.19-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.5.19.pom; 包含...

    akka--------------介绍

    akka接触信息

    akka-persistence-sql-async, 一个用于akka持久性的日志和快照存储.zip

    akka-persistence-sql-async, 一个用于akka持久性的日志和快照存储 akka-persistence-sql-async 的日志和快照存储插件( akka持久化插件。 Akka-persistence-sql-async执行由 scalikejdbc异步查询,它提供非阻塞api来...

    akka-actor_2.11-2.4.20-API文档-中文版.zip

    赠送jar包:akka-actor_2.11-2.4.20.jar; 赠送原API文档:akka-actor_2.11-2.4.20-javadoc.jar; 赠送源代码:akka-actor_2.11-2.4.20-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.4.20.pom; 包含...

    akka-protobuf_2.11-2.4.20-API文档-中文版.zip

    赠送jar包:akka-protobuf_2.11-2.4.20.jar; 赠送原API文档:akka-protobuf_2.11-2.4.20-javadoc.jar; 赠送源代码:akka-protobuf_2.11-2.4.20-sources.jar; 赠送Maven依赖信息文件:akka-protobuf_2.11-2.4.20....

    akka-stream_2.11-2.4.20-API文档-中文版.zip

    赠送jar包:akka-stream_2.11-2.4.20.jar; 赠送原API文档:akka-stream_2.11-2.4.20-javadoc.jar; 赠送源代码:akka-stream_2.11-2.4.20-sources.jar; 赠送Maven依赖信息文件:akka-stream_2.11-2.4.20.pom; ...

    akka-stream_2.11-2.4.20-API文档-中英对照版.zip

    赠送jar包:akka-stream_2.11-2.4.20.jar; 赠送原API文档:akka-stream_2.11-2.4.20-javadoc.jar; 赠送源代码:akka-stream_2.11-2.4.20-sources.jar; 赠送Maven依赖信息文件:akka-stream_2.11-2.4.20.pom; ...

    akka-actor_2.11-2.5.21-API文档-中英对照版.zip

    赠送jar包:akka-actor_2.11-2.5.21.jar; 赠送原API文档:akka-actor_2.11-2.5.21-javadoc.jar; 赠送源代码:akka-actor_2.11-2.5.21-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.5.21.pom; 包含...

    akka-kryo-serialization, 基于Kryo的Akka序列化.zip

    akka-kryo-serialization, 基于Kryo的Akka序列化 akka-kryo-serialization-- Scala 和Akka基于kryo的序列化程序这个库为 Scala 和Akka提供定制的基于kryo的序列化程序。 它可以用于更高效的akka远程处理。它还可以...

    akka-actor_2.11 jar包

    akka-actor_2.11 jar包

    akka-remote_2.10-2.2.3-shaded-protobuf.zip

    p-unit.zip,一个基于junit和groovy ssh dsl的groovy库,用于验证puppet模块的结果;一个基于junit和sshoogr(groovy ssh dsl)的groovy库,用于验证配置脚本(puppet、chef、ansible等)的结果。

    akka-http-oauth2-provider:使用Akka HTTP中的scala-oauth2-provider启用此库

    akka-http-oauth2-provider 使用Akka HTTP中的启用该库。设置将“ akka-http-oauth2-provider”添加到项目的库依赖项。 libraryDependencies ++ = Seq ( " com.nulab-inc " %% " scala-oauth2-core " % " 1.5.0 " , ...

    akka-actor-1.0-RC2.jar.zip

    在本次讨论中,我们将聚焦于`akka-actor-1.0-RC2.jar.zip`这个压缩包,它包含了Akka Actor库的1.0 Release Candidate 2版本。 Akka的核心组件之一就是Actor系统,这是一个设计模式,用于处理并发和并行计算。在Akka...

    akka-slf4j_2.11-2.5.21-API文档-中文版.zip

    赠送jar包:akka-slf4j_2.11-2.5.21.jar; 赠送原API文档:akka-slf4j_2.11-2.5.21-javadoc.jar; 赠送源代码:akka-slf4j_2.11-2.5.21-sources.jar; 赠送Maven依赖信息文件:akka-slf4j_2.11-2.5.21.pom; 包含...

Global site tag (gtag.js) - Google Analytics