我刚刚开始学习AKKA,并从学习AKKA这本书中编写了一个示例程序,但该程序没有运行,也没有为进一步调试提供任何有用的信息。我正在粘贴下面的代码文件,请查看并提供
SetRequest.java
package com.akkademy.messages;
public class SetRequest {
private String key;
private String value;
public SetRequest(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
AkkademyDB.java
package com.akkademy;
import akka.actor.AbstractActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
import com.akkademy.messages.SetRequest;
import java.util.HashMap;
import java.util.Map;
public class AkkademyDB extends AbstractActor{
protected final LoggingAdapter log = Logging.getLogger(context().system(),this);
protected final Map<String,Object> map = new HashMap<>();
private AkkademyDB(){
receive(ReceiveBuilder.match(SetRequest.class,message->{
log.info("Received set request-key:{} value: {}",message.getKey(),message.getValue());
}).matchAny(o->log.info("received unknown message {}",o)).build()
);
}
}
AkkademyDBTest.java
package com.akkademy;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.testkit.TestActorRef;
import com.akkademy.messages.SetRequest;
import akka.actor.ActorRef;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class AkkademyDBTest {
ActorSystem system = ActorSystem.create();
@Test
public void itShouldPlaceKeyValueFromSetMessageIntoMap() {
TestActorRef<AkkademyDB> actorRef = TestActorRef.create(system, Props.create(AkkademyDB.class));
actorRef.tell(new SetRequest("key","value"),ActorRef.noSender());
AkkademyDB akkademyDB = actorRef.underlyingActor();
assertEquals(akkademyDB.map.get("key"),"value");
}
}
build.sbt
name := """akkademy-db"""
version := "1.0"
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
"com.typesafe.akka" % "akka-actor_2.11" % "2.3.6",
"com.typesafe.akka" % "akka-testkit_2.11" % "2.5.6" % "test",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test")
运行测试文件时收到错误消息
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/tools.jar:/Users/madhurjain/akkademy-db/target/scala-2.11/test-classes:/Users/madhurjain/akkademy-db/target/scala-2.11/classes:/Users/madhurjain/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.2.jar:/Users/madhurjain/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.3.6.jar:/Users/madhurjain/.ivy2/cache/com.typesafe/config/bundles/config-1.2.1.jar:/Users/madhurjain/.ivy2/cache/org.scala-tools.testing/test-interface/jars/test-interface-0.5.jar:/Users/madhurjain/.ivy2/cache/junit/junit-dep/jars/junit-dep-4.10.jar:/Users/madhurjain/.ivy2/cache/com.novocode/junit-interface/jars/junit-interface-0.10.jar:/Users/madhurjain/.ivy2/cache/com.typesafe/config/bundles/config-1.3.1.jar:/Users/madhurjain/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.5.6.jar:/Users/madhurjain/.ivy2/cache/com.typesafe.akka/akka-testkit_2.11/jars/akka-testkit_2.11-2.5.6.jar:/Users/madhurjain/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.11.jar:/Users/madhurjain/.ivy2/cache/org.scala-lang.modules/scala-java8-compat_2.11/bundles/scala-java8-compat_2.11-0.7.0.jar:/Users/madhurjain/.ivy2/cache/junit/junit/jars/junit-4.12.jar:/Users/madhurjain/.ivy2/cache/org.hamcrest/hamcrest-core/jars/hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.akkademy.AkkademyDBTest
[INFO] [10/25/2017 17:59:59.509] [main] [akka://default/user/$$a] Received set request-key:key value: value
junit.framework.AssertionFailedError:
Expected :null
Actual :value
<Click to see difference>
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.Assert.failNotEquals(Assert.java:287)
at junit.framework.Assert.assertEquals(Assert.java:67)
at junit.framework.Assert.assertEquals(Assert.java:74)
at com.akkademy.AkkademyDBTest.itShouldPlaceKeyValueFromSetMessageIntoMap(AkkademyDBTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
答案 0 :(得分:1)
当AcademyDB
actor收到SetRequest
消息时,它不会将消息的键和值放在其内部映射中,而只是记录键和值。您的测试失败,因为它检查了actor的键值对的映射并且什么也没找到。
更改您的actor以将SetRequest
消息的键值对放在其地图中:
public class AkkademyDB extends AbstractActor {
protected final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
protected final Map<String, Object> map = new HashMap<>();
@Override
public Receive createReceive() {
return receiveBuilder()
.match(SetRequest.class, message -> {
log.info("Received set request-key:{} value: {}", message.getKey(), message.getValue());
map.put(message.getKey(), message.getValue()); // <--- put in map
})
.matchAny(o -> log.info("received unknown message {}", o))
.build();
}
}
此外,调整您的build.sbt
以始终使用Akka罐子的2.5.6版本:
name := "akkademy-db"
version := "1.0"
scalaVersion := "2.11.11"
val akkaVersion = "2.5.6"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion,
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test")