gpt4 book ai didi

scala - 为什么 sbt 不能解析 akka-remote?

转载 作者:行者123 更新时间:2023-12-04 17:59:56 27 4
gpt4 key购买 nike

当我在 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教程。

最佳答案

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"

关于scala - 为什么 sbt 不能解析 akka-remote?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24801510/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com