当我在sbt上构建并运行此程序时,它有以下错误:
[info] Updating {file:/opt/ifkaar/akkaprojects/calculation/}calculation...
[info] Resolving com.typesafe.akka#akka-remote;2.3.4 ...
[warn] module not found: com.typesafe.akka#akka-remote;2.3.4
[warn] ==== local: tried
[warn] /home/sarawaheed/.ivy2/local/com.typesafe.akka/akka-remote/2.3.4/ivys/ivy.xml
[warn] ==== public: tried
[warn] http://repo1.maven.org/maven2/com/typesafe/akka/akka-remote/2.3.4/akka-remote-2.3.4.pom
[warn] ==== Typesafe Repository: tried
[warn] http://repo.typesafe.com/typesafe/releases/com/typesafe/akka/akka-remote/2.3.4/akka-remote-2.3.4.pom
[info] Resolving jline#jline;2.11 ...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: com.typesafe.akka#akka-remote;2.3.4: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[trace] Stack trace suppressed: run last *:update for the full output.
[error] (*:update) sbt.ResolveException: unresolved dependency: com.typesafe.akka#akka-remote;2.3.4: not found
[error] Total time: 2 s, completed Jul 17, 2014 3:38:42 PM
这是我的build.sbt文件
name := "calculation"
version := "1.0"
scalaVersion := "2.11.1"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.4,"com.typesafe.akka" %% "akka-remote" % "2.3.4""
这是我的PI.scala代码
import akka.actor._
import akka.routing.RoundRobinRouter
import akka.util.Duration
import akka.util.duration._
object Pi extends App {
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
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)
class Worker extends Actor {
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
}
def receive = {
case Work(start, nrOfElements) ⇒
sender ! Result(calculatePiFor(start, nrOfElements)) // perform the work
}
}
class Master(nrOfWorkers: Int, nrOfMessages: Int, nrOfElements: Int, listener: ActorRef)
extends Actor {
var pi: Double = _
var nrOfResults: Int = _
val start: Long = System.currentTimeMillis
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)
case Result(value) ⇒
pi += value
nrOfResults += 1
if (nrOfResults == nrOfMessages) {
// Send the result to the listener
listener ! PiApproximation(pi, duration = (System.currentTimeMillis - start).millis)
// Stops this actor and all its supervised children
context.stop(self)
}
}
}
class Listener extends Actor {
def receive = {
case PiApproximation(pi, duration) ⇒
println("\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s"
.format(pi, duration))
context.system.shutdown()
}
}
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
// Create an Akka system
val system = ActorSystem("PiSystem")
// create the result listener, which will print the result and shutdown the system
val listener = system.actorOf(Props[Listener], name = "listener")
// create the master
val master = system.actorOf(Props(new Master(
nrOfWorkers, nrOfMessages, nrOfElements, listener)),
name = "master")
// start the calculation
master ! Calculate
}
}
注意:我正在关注this教程。
答案 0 :(得分:7)
build.sbt文件中的libraryDependencies
条目应如下所示:
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.3.4",
"com.typesafe.akka" %% "akka-remote" % "2.3.4"
)
++=
运算符是一种列表连接运算符。它告诉sbt将这个新的依赖集合与现有的依赖集合合并,无论哪个都可以。
+=
运算符附加单个项目,而不是结束集合。例如:
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.4"
libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.3.4"
%%
运算符是显式指定应编译所需库的Scala版本的简写。每个Scala版本都有不同的库名称。例如,而不是写:
"com.typesafe.akka" % "akka-actor_2.11" % "2.3.4"
你写道:
"com.typesafe.akka" %% "akka-actor" % "2.3.4"
声明的scalaVersion
设置将用于"展开" %%
运算符。请注意,将仅使用X.Y.Z完整版中的X.Y数字。发生这种情况是因为Scala版本在Z更改时兼容,但在X或Y更改时则不兼容。
您最终可能最终得到以下内容以避免重复:
libraryDependencies ++= Seq("actor", "remote").map("akka-" + _).map("com.typesafe.akka" %% _ % "2.3.4")
由于akka-remote
取决于akka-actor
以下内容:
libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.3.4"