gpt4 book ai didi

java - 使用 Play 2 和远程 Akka 集群实现序列化

转载 作者:行者123 更新时间:2023-11-30 11:40:29 26 4
gpt4 key购买 nike

我有一个连接到远程 Akka 集群系统的 Play Framework 2,两个系统都在 java 中。我有一个包装在类中的数据结构,我已经实现了可序列化在包装类上。我想使用数据结构进行远程计算,但每当我尝试将包装类发送到远程 Akka 系统时,我都会收到错误消息。

下面是代码片段。

这是包装类

package controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

public class Coordinates implements Serializable{


String className = null;
Map <String,ArrayList<Object>> jMap = null;
boolean train = false;

public Coordinates() { train = true;}

public Coordinates (String className)
{
this.className = className;
}

public Coordinates (Map <String,ArrayList<Object>> jMap , boolean b)
{

this.jsonMap = jMap;
train = b;
}
}

这是发送对象的Actor类

package controllers;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;

public class LocalActor extends UntypedActor {

ActorRef masterActor;
@Override
public void onReceive(Object arg) throws Exception {
if(arg instanceof Coordinates)
{ Coordinates msg = (Coordinates) arg ;
masterActor.tell(msg , getSelf());
}
}


public void preStart()
{
masterActor = getContext().actorFor("akka://MasterNode@127.0.0.1:2552/user/masterActor");
}

}

这是接收类的 Akka actor

package Rubine_Cluster;

import java.io.Serializable;

import akka.actor.*;


public class MasterActor extends UntypedActor implements Serializable{

public MasterActor(){System.out.println(" the masteractor has been started ");}

@Override
public void onReceive(Object message) throws Exception {
System.out.print(" this is from before me to you "+message.toString());

if(message instanceof Coordinates)
{ Coordinates msg = (Coordinates) message;

try { System.out.print(" this is from message " + msg.jsonMap.toString());
getSender().tell( 1234 , getSelf());
} catch (Exception e) {
getSender().tell(new akka.actor.Status.Failure(e), getSelf());
throw e;
}

}
else{ unhandled(message);}
}
}

这是我的 eclipse IDE 的错误信息

[INFO] [10/02/2012 02:58:35.606] [main] [ActorSystem(MasterNode)] REMOTE: RemoteServerStarted@akka://MasterNode@127.0.0.1:2552
Master Node is called
the masteractor has been started
[INFO] [10/02/2012 02:59:28.223] [MasterNode-7] [ActorSystem(MasterNode)] REMOTE: RemoteClientStarted@akka://LocalNode@127.0.0.1:63305
[ERROR] [10/02/2012 02:59:28.334] [MasterNode-7] [ActorSystem(MasterNode)] REMOTE: RemoteServerError@akka://MasterNode@127.0.0.1:2552] Error[java.lang.ClassNotFoundException:controllers.Coordinates
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at akka.util.ClassLoaderObjectInputStream.resolveClass(ClassLoaderObjectInputStream.scala:12)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at akka.serialization.JavaSerializer$$anonfun$1.apply(Serializer.scala:121)
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
at akka.serialization.JavaSerializer.fromBinary(Serializer.scala:121)
at akka.serialization.Serialization.deserialize(Serialization.scala:73)
at akka.remote.MessageSerializer$.deserialize(MessageSerializer.scala:22)
at akka.remote.RemoteMessage.payload(RemoteTransport.scala:210)
at akka.remote.RemoteMarshallingOps$class.receiveMessage(RemoteTransport.scala:276)
at akka.remote.netty.NettyRemoteTransport.receiveMessage(NettyRemoteSupport.scala:30)
at akka.remote.netty.RemoteServerHandler.messageReceived(Server.scala:182)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)
at akka.remote.netty.RemoteServerHandler.handleUpstream(Server.scala:154)
at org.jboss.netty.channel.StaticChannelPipeline.sendUpstream(StaticChannelPipeline.java:366)
at org.jboss.netty.channel.StaticChannelPipeline$StaticChannelHandlerContext.sendUpstream(StaticChannelPipeline.java:528)
at org.jboss.netty.handler.execution.ChannelUpstreamEventRunnable.run(ChannelUpstreamEventRunnable.java:44)
at org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor$ChildExecutor.run(OrderedMemoryAwareThreadPoolExecutor.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
]

我也在 Remote Akka 系统中实现了 Coordinate 类。任何建议或帮助将不胜感激。谢谢

最佳答案

您不必两次实现 Coordinate 类。您应该有一个包含所有模型类(坐标等)的模块,并根据第一个创建其他两个 Akka 项目。

你应该有这样的项目/模块依赖:

module containing Coordinate class (Maven Project)
|
---- Local Actor (Play project)
|
---- Remote Actor (Maven Project)

关于java - 使用 Play 2 和远程 Akka 集群实现序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12683712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com