0

我的 akka java 项目使用 play framework 来实现 rubine 算法时遇到问题,我想与社区分享希望得到解决方案。

该项目包含一个创建子actor的akka​​主actor。然后子actor初始化一个执行某些任务的java类。任务数据由使用 websocket 和 json 对象的 Web 界面提供。json 对象包含双精度数据类型。播放框架应用程序类从 Web 界面获取数据并将其传递给主参与者。然后主actor创建一个子actor并将数据传递给子actor。下面是代码片段。相关部分是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)

任何建议都会很高兴。

4

1 回答 1

0

在我看来,这与 Akka 或 Play 无关,只是Integer您的 JSON 数据中有值。

由于您将变量声明为 ,因此您会期望在类型安全的语言中不会发生这种情况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);

    }   
}

线程“主”java.lang.ClassCastException 中的异常:java.lang.Integer 无法转换为 java.lang.Double
        在 x.main(x.java:20)
于 2012-08-07T08:47:02.673 回答