我需要一些帮助来序列化一个实体以通过 Akka Remote 发送它。
这是序列化程序类:
@Override
public void toBinary(Object o, ByteBuffer buf) {
byte[] bytes = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
bytes = bos.toByteArray();
}
catch(Exception e){
//System.out.println(e.getStackTrace());
e.printStackTrace();
}
buf.put(bytes);
}
@Override
public Object fromBinary(ByteBuffer buf, String manifest) {
Object obj = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(buf.array());
ois = new ObjectInputStream(bis);
obj = ois.readObject();
}
catch(Exception e){
//System.out.println(e.getStackTrace());
e.printStackTrace();
}
return obj;
}
我在第 5 行得到以下异常
java.lang.UnsupportedOperationException
at java.nio.ByteBuffer.array(ByteBuffer.java:994)
at serializers.ExampleByteBufSerializer.fromBinary(ExampleByteBufSerializer.java:67)
at akka.serialization.Serialization.deserializeByteBuffer(Serialization.scala:190)
at akka.remote.MessageSerializer$.deserializeForArtery(MessageSerializer.scala:91)
at akka.remote.artery.Deserializer$$anon$3.onPush(Codecs.scala:620)
at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:499)
at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:401)
at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:571)
at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:457)
at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:546)
at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:725)
at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:740)
at akka.actor.Actor$class.aroundReceive(Actor.scala:513)
at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:650)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:527)
at akka.actor.ActorCell.invoke(ActorCell.scala:496)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
at akka.dispatch.Mailbox.run(Mailbox.scala:224)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
这是我发送给远程参与者的消息:
public class Message2Serialize implements Serializable {
String nombre;
public Message2Serialize(String nombre) {
this.nombre = nombre;
}
public Message2Serialize() {
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
奇怪的是它以一种方式工作,如果我们使用它发送消息,它在接收器中工作正常:
ActorSelection selection = getContext().system().actorSelection("akka://applicationremote@localhost:25521/user/actors.MessageReceiverActor");
selection.tell(message2Serialize, getSelf());
但是当我们使用 then 重播给发送者 actor 时, getSender().tell(m, getSelf());
我们得到了异常。
我们正在使用 Java 1.8 和 akka-remote_2.11:2.5.3
提前致谢!罗德里