我的 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)
任何建议都会很高兴。