[英]akka child actor gives a class cast exception
我的akka java項目使用play framework來實現rubine算法時遇到問題,我想與社區分享以尋求解決方案。
該項目包含一個創建子演員的akka主演員。 然后,子角色會初始化一個執行某些任務的java類。 該任務的數據由使用websocket和json對象的Web界面提供。 json對象包含一個double數據類型。 播放框架應用程序類從Web界面獲取數據,並將其傳遞給主actor。 然后,主演員創建一個子演員,並將數據傳遞給該子演員。下面是代碼片段。 相關章節已公開
這來自應用程序類:
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null)); }
public static WebSocket<JsonNode> sockHandler() {
return new WebSocket<JsonNode>() {
// called when the websocket is established
public void onReady(final WebSocket.In<JsonNode> in, final WebSocket.Out<JsonNode> out) {
try{ RubineActor.connectMaster(in, out); }
catch(Exception e){e.printStackTrace();}
} }; } }
這是來自男主角:
public class RubineActor extends UntypedActor {
static ObjectMapper mapper;
Map<String,ArrayList<Double>> jsonMap;
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
ActorRef childActor = getContext().actorOf(new Props(ChildActor.class), "childactor");
static ActorRef masterActor = Akka.system().actorOf(new Props(RubineActor.class));
public static void connectMaster (final WebSocket.In<JsonNode> in,
final WebSocket.Out<JsonNode> out)
{ in.onMessage(new Callback<JsonNode>() {
public void invoke(JsonNode event) throws JsonParseException, JsonMappingException,
IOException {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String,ArrayList<Double>> jsonMap = mapper.readValue(event, Map.class);
masterActor.tell(new Coordinates(jsonMap));
("timeArray").toString(); } }); }
@Override
public void onReceive(Object message) throws Exception {
if(message instanceof Coordinates)
{Coordinates msg = (Coordinates) message;
childActor.tell(msg); }
else { unhandled(message);} }
public static class Coordinates {
Map <String,ArrayList<Double>> jsonMap = null;
public Coordinates (Map <String,ArrayList<Double>> jsonMap ) {
super();
this.jsonMap = jsonMap; } }}
this is from the child actor
public class ChildActor extends UntypedActor {
public ChildActor (){}
@Override
public void onReceive(Object msg) throws Exception
if(msg instanceof Coordinates) {
Coordinates cood = (Coordinates) msg;
new Gesture(cood.jsonMap);
}
else { unhandled(msg);} }}
這是來自普通的java類:
Map<String, ArrayList<Double>> jsonMap ;
Map <String,ArrayList<Long>> jsonMap2 ;
public Gesture(){}
public Gesture (Map<String, ArrayList<Double>> jsonMap)
{ this.jsonMap = jsonMap;
npoints = jsonMap.size();
intial_Theta(); }
public void intial_Theta() {
// this line produces a class cast exception error.the error is given below.
double dx = jsonMap.get("x").get(2) - jsonMap.get("x").get(0);}
這是錯誤消息:
[info] Compiling 4 Scala sources and 10 Java sources to C:\Users\FAISAL\workspac
e\Sketch_Server\target\scala-2.9.1\classes...
[info] play - Application started (Dev)
[info] play - Starting application default Akka system.
[ERROR] [08/07/2012 05:15:11.187] [application-akka.actor.default-dispatcher-3]
[akka://application/user/$a/childactor] java.lang.Integer cannot be cast to java
.lang.Double
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Doub
le
at model.Gesture.intial_Theta(Gesture.java:90)
at model.Gesture.<init>(Gesture.java:74)
at model.ChildActor.onReceive(ChildActor.java:19)
at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:1
54)
at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:1
53)
at akka.actor.Actor$class.apply(Actor.scala:318)
at akka.actor.UntypedActor.apply(UntypedActor.scala:93)
at akka.actor.ActorCell.invoke(ActorCell.scala:626)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197)
at akka.dispatch.Mailbox.run(Mailbox.scala:179)
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(
AbstractDispatcher.scala:516)
at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259)
at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479)
at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
任何建議都會很高興。
在我看來,這與Akka或Play無關,只是在JSON數據中包含Integer
值。
您可能希望不會以類型安全的語言發生這種情況,因為您已將變量聲明為ArrayList<Double>
,但這就是您的Java。 這是一個簡化的版本,可以重現您的問題:
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
public class x {
static void breakMap(Map x) {
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(new Integer(1));
x.put("foo", l);
};
public static void main(String[] args) {
Map<String, ArrayList<Double>> x = new HashMap<String, ArrayList<Double> >();
breakMap(x);
ArrayList<Double> y = x.get("foo");
Double z = y.get(0);
}
}
給
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double at x.main(x.java:20)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.