编辑:为了澄清,就好像编译器和运行时不同意哪个版本的 Akka 在类路径上。除了编译器看到新方法但运行时引发这种情况之外,NoSuchMethodError
当我稍后尝试调用时,我得到了同样的错误ActorContext.children
(编译器看到它但 JVM 引发了NoSuchMethod
)。这个问题可能比 Akka 更普遍。
我已经完成mvn clean
并检查了我的 Scala REPL 版本很多次。
原始问题:
2.2.1 版(我正在使用的版本)的Akka 文档和API说带有构造函数参数的 Actor 应该像这样构建:
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"))
^
编译器中的警告。文档和迁移指南(2.1.x 到 2.2.x)都确认了这种弃用,说闭包技术(我正在使用的那个)导致不可序列化的 Actors。
我不想使用不推荐使用的东西,特别是因为我刚刚开始使用这个库。我不明白关于Props.withDispatcher
和朋友的评论,因为我只想使用默认调度程序,至少现在是这样。有没有这种工作的例子?
编辑:我的 pom.xml 如下所示。我已经注释掉了所有的测试,因为没有scalatest
适用于 Scala 2.10.2 的版本。我已经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>