2012-08-07 49 views
0

我有一个问题,与我的akka​​ Java项目使用播放框架,实施rubine算法,我想与社区分享,希望得到一个解决方案。akka儿童演员给出了一个类抛出异常

该项目包含创建一个儿童演员的阿卡主演员。这个孩子的行为者然后初始化执行一些任务的Java类。该任务的数据由使用websocket和json对象的Web界面提供。 json对象包含一个double数据类型。播放框架应用程序类从Web界面获取数据并将其传递给主演员。主演员然后创建一个小孩演员并将数据传递给小孩actor.below是代码片段。相关部分中hightloghted

这是从应用程序类:

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) 

对任何建议都会很高兴。

回答

0

在我看来,这不是马上关于Akka或Play,它只是在您的JSON数据中有Integer值。

自从您声明该变量为ArrayList<Double>之后,您会期望不会发生类型安全的语言,但这对您而言就是Java。下面是重现你的问题的简化版本:

​​3210

 
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double 
     at x.main(x.java:20) 
相关问题