- 浏览: 2552019 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
Update Site will come soon
2021-06-02 04:10 1678I am still keep notes my tech n ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 294Hadoop Docker 2019 Version 3.2. ... -
Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON
2019-12-03 04:17 449Nginx and Proxy 2019(1)Nginx En ... -
Data Solution 2019(13)Docker Zeppelin Notebook and Memory Configuration
2019-11-09 07:15 294Data Solution 2019(13)Docker Ze ... -
Data Solution 2019(10)Spark Cluster Solution with Zeppelin
2019-10-29 08:37 248Data Solution 2019(10)Spark Clu ... -
AMAZON Kinesis Firehose 2019(1)Firehose Buffer to S3
2019-10-01 10:15 322AMAZON Kinesis Firehose 2019(1) ... -
Rancher and k8s 2019(3)Clean Installation on CentOS7
2019-09-19 23:25 313Rancher and k8s 2019(3)Clean In ... -
Pacemaker 2019(1)Introduction and Installation on CentOS7
2019-09-11 05:48 343Pacemaker 2019(1)Introduction a ... -
Crontab-UI installation and Introduction
2019-08-30 05:54 455Crontab-UI installation and Int ... -
Spiderkeeper 2019(1)Installation and Introduction
2019-08-29 06:49 510Spiderkeeper 2019(1)Installatio ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ... -
Supervisor 2019(1)CentOS 7
2019-08-19 09:33 331Supervisor 2019(1)CentOS 7 Ins ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 373Redis Cluster 2019(3)Redis Clus ... -
Amazon Lambda and Version Limit
2019-08-02 01:42 438Amazon Lambda and Version Limit ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 530MySQL HA Solution 2019(1)Master ... -
RabbitMQ Cluster 2019(2)Cluster HA and Proxy
2019-07-11 12:41 464RabbitMQ Cluster 2019(2)Cluster ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:35 323Running Zeppelin with Nginx Aut ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:34 324Running Zeppelin with Nginx Aut ... -
ElasticSearch(3)Version Upgrade and Cluster
2019-05-20 05:00 329ElasticSearch(3)Version Upgrade ... -
Jetty Server and Cookie Domain Name
2019-04-28 23:59 404Jetty Server and Cookie Domain ...
相关推荐
赠送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; 包含...
赠送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; ...
赠送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; 包含...
赠送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; ...
赠送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; 包含...
赠送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....
赠送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-persistence-sql-async, 一个用于akka持久性的日志和快照存储 akka-persistence-sql-async 的日志和快照存储插件( akka持久化插件。 Akka-persistence-sql-async执行由 scalikejdbc异步查询,它提供非阻塞api来...
赠送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; 包含...
赠送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....
赠送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; ...
赠送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; ...
赠送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序列化 akka-kryo-serialization-- Scala 和Akka基于kryo的序列化程序这个库为 Scala 和Akka提供定制的基于kryo的序列化程序。 它可以用于更高效的akka远程处理。它还可以...
akka-actor_2.11 jar包
p-unit.zip,一个基于junit和groovy ssh dsl的groovy库,用于验证puppet模块的结果;一个基于junit和sshoogr(groovy ssh dsl)的groovy库,用于验证配置脚本(puppet、chef、ansible等)的结果。
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 Release Candidate 2版本。 Akka的核心组件之一就是Actor系统,这是一个设计模式,用于处理并发和并行计算。在Akka...
赠送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; 包含...