编辑:为了澄清,似乎编译器和运行时不同意Akka在类路径上的版本。除了这种情况,编译器看到新方法但运行时引发NoSuchMethodError
,我后来尝试调用ActorContext.children
时会遇到同样的错误(编译器看到它但JVM引发了NoSuchMethod
)。这个问题可能比Akka更普遍。
我已经完成mvn clean
并多次检查我的Scala REPL版本。
原始问题:
版本2.2.1的Akka documentation和API(我正在使用的版本)说具有构造函数参数的Actors应该像这样构建:
import akka.actor.Actor
import akka.actor.Props
import akka.actor.ActorSystem
class MyActor(one: Int, two: Double, three: String) extends Actor {
def receive = {
case "test" => println("%d %g %s".format(one, two, three))
}
}
val system = ActorSystem("MyActorSystem")
val actor = system.actorOf(Props(classOf[MyActor], 1, 2.0, "three"))
虽然这会编译,但在尝试运行它时会引发java.lang.NoSuchMethodError: akka.actor.Props$.apply(Ljava/lang/Class;Lscala/collection/Seq;)Lakka/actor/Props;
异常。
在REPL上运行
<console>:12 error: type mismatch;
found : Class[MyActor](classOf[$MyActor])
required: () => akka.actor.Actor
val actor = system.actorOf(Props(classOf[MyActor], 1, 2.0, "three"))
^
如果我接受REPL建议的更改,
val actor = system.actorOf(Props(() => new MyActor(1, 2.0, "three")))
它可以在没有注释REPL和
的情况下工作[warn] test.scala:10 method apply in object Props is deprecated: use Props.withDispatcher and friends
val actor = system.actorOf(Props(classOf[MyActor], 1, 2.0, "three"))
^
编译器中的警告。文档和Migration Guide (2.1.x to 2.2.x)都肯定了这种弃用,并说封闭技术(我正在使用的那种)会产生不可序列化的Actors。
我不想使用不赞成的东西,特别是因为我刚刚开始使用这个库。我不理解有关Props.withDispatcher
和朋友的评论,因为我只想使用默认调度程序,至少目前是这样。有没有这方面的例子?
编辑:我的pom.xml如下所示。我已经注释掉了所有测试,因为没有适用于Scala 2.10.2的scalatest
版本。我已多次清理target
目录:其他地方没有任何其他Scala版本的提示(并且从未有过任何其他版本的Akka)。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>plotsmanship</name>
<!-- <description>TODO</description> -->
<inceptionYear>2013</inceptionYear>
<groupId>org.plotsmanship</groupId>
<artifactId>plotsmanship</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.tools.version>2.10</scala.tools.version>
<scala.version>2.10.2</scala.version>
</properties>
<dependencies>
<!-- Real dependencies for the jar -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7R4</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.10</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-remote_2.10</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.10</artifactId>
<version>2.2.1</version>
</dependency>
<!-- Dependencies for testing only (resolves to junit-4.11.jar hamcrest-core-1.3.jar scalatest_2.10-2.0.M6-SNAP8.jar) -->
<!-- <dependency> -->
<!-- <groupId>junit</groupId> -->
<!-- <artifactId>junit</artifactId> -->
<!-- <version>4.11</version> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.scalatest</groupId> -->
<!-- <artifactId>scalatest_${scala.tools.version}</artifactId> -->
<!-- <version>2.0.M6-SNAP8</version> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<!-- <testSourceDirectory>src/test/scala</testSourceDirectory> -->
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.3</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<!-- <goal>testCompile</goal> -->
</goals>
<configuration>
<args>
<arg>-deprecation</arg>
<arg>-feature</arg>
<!-- <arg>-make:transitive</arg> (is an unsupported option) -->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
<recompileMode>incremental</recompileMode>
<useZincServer>true</useZincServer>
</configuration>
</execution>
</executions>
</plugin>
<!-- <plugin> -->
<!-- <groupId>org.apache.maven.plugins</groupId> -->
<!-- <artifactId>maven-surefire-plugin</artifactId> -->
<!-- <version>2.13</version> -->
<!-- <configuration> -->
<!-- <useFile>false</useFile> -->
<!-- <disableXmlReport>true</disableXmlReport> -->
<!-- <includes> -->
<!-- <include>**/*Test.*</include> -->
<!-- <include>**/*Suite.*</include> -->
<!-- </includes> -->
<!-- </configuration> -->
<!-- </plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>org.plotsmanship.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>./lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
target/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:2)
如果你使用akka 2.2.x和scala 2.10.y你可能有一个问题:查看scala / lib目录,你会看到akka-actors.jar,这是akka的2.1.z版本(只是看看里面在MANIFEST或一些典型的课程)。因此,如果你像这样运行你的应用程序:
scala -cp $YourLibs:akka-actors-2.2.x ...
2.1 jar的akka将首先(自动)添加,并且不会被类路径中的2.2覆盖。
解决方法:强>
在没有scala包装器的情况下手动运行:
java -cp $ScalaHome/lib/scala-library.jar:$AkkaHome/$Akka_2.2_jars YourMainClass
P.S。我不明白为什么akka jar随scala一起发货
答案 1 :(得分:1)
尝试查看类路径中的库。即使指定了运行scala -cp的类路径,scala也会自动在scala主目录的lib目录中添加scala标准库和其他库。请检查here。
尝试在main中编写一段代码来打印类路径。它可以帮助您找到正在加载的内容。这个one在java中,但它可以提供帮助。
我复制并粘贴了您的第一个示例,就像API中指定的那样,并使用此build.sbt构建它:
name := "My Project"
version := "1.0"
scalaVersion := "2.10.2"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies ++= {
Seq(
"com.typesafe.akka" %% "akka-actor" % "2.2.1",
"com.typesafe.akka" %% "akka-remote" % "2.2.1",
"com.typesafe.akka" %% "akka-testkit" % "2.2.1"
)
}
它就像一个魅力:
$ sbt clean compile run
[info] Loading global plugins from ~/.sbt/plugins
...........
[success] Total time: 4 s, completed Sep 13, 2013 6:03:05 PM
[info] Running MyActor
1 2.00000 three