akka序列化ByteBuffer-java.lang.UnsupportedOperationException

akka序列化ByteBuffer-java.lang.UnsupportedOperationException,java,akka,akka-remote-actor,akka-remoting,Java,Akka,Akka Remote Actor,Akka Remoting,我需要一些帮助来序列化一个实体,通过Akka Remote发送它 这是序列化程序类: @Override public void toBinary(Object o, ByteBuffer buf) { byte[] bytes = null; ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStrea

我需要一些帮助来序列化一个实体,通过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)
这是Im发送给远程参与者的消息:

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());
但是当我们使用
getSender().tell(m,getSelf())重播给发送方参与者时然后我们得到了异常

我们正在使用Java1.8和akka-remote_2.11:2.5.3

提前谢谢! Rodri

javadoc摘录

@抛出UnsupportedOperationException。如果此缓冲区未由可访问阵列支持

ByteBuffer似乎没有完全初始化。。。 此外,javadoc还说明了该做什么

在调用此函数之前,请调用{@link#hasArray hasArray}方法 方法,以确保此缓冲区具有可访问的备份 数组

javadoc摘录

@抛出UnsupportedOperationException。如果此缓冲区未由可访问阵列支持

ByteBuffer似乎没有完全初始化。。。 此外,javadoc还说明了该做什么

在调用此函数之前,请调用{@link#hasArray hasArray}方法 方法,以确保此缓冲区具有可访问的备份 数组

通过更改此行:
bis=newByteArrayInputStream(arr)

byte[] arr = new byte[buf.remaining()];
buf.get(arr);
bis = new ByteArrayInputStream(arr);
它可以工作,但我不知道为什么。

通过更改此行:
bis=newByteArrayInputStream(arr)

byte[] arr = new byte[buf.remaining()];
buf.get(arr);
bis = new ByteArrayInputStream(arr);

它可以工作,但我不知道为什么。

您是否尝试在不定义自定义序列化程序的情况下执行此操作?是的,它与默认java序列化程序完美配合,这正是您当时使用自定义序列化程序的原因所在?好吧,默认的和你做的一样。这是因为性能。Akka开发人员建议不要在生产中使用Java序列化程序。我知道,我只是认为默认的序列化程序也使用字节数组进行序列化。在这种情况下,将不会有任何改善与您的自定义之一。但是我找不到关于默认服务器现在正在做什么的信息。仅供参考,我通常使用akka Kryo。您是否尝试在不定义自定义序列化程序的情况下执行此操作?是的,它与默认java序列化程序完美配合?您当时使用自定义序列化程序的原因是什么?好吧,默认的和你做的一样。这是因为性能。Akka开发人员建议不要在生产中使用Java序列化程序。我知道,我只是认为默认的序列化程序也使用字节数组进行序列化。在这种情况下,将不会有任何改善与您的自定义之一。但是我找不到关于默认服务器现在正在做什么的信息。仅供参考,我通常使用akka Kryothat是正确的,问题是它没有初始化。通过使用hasArray,我没有得到异常,但是我没有得到任何要反序列化的数据。其清空测试结果正确,问题在于其未初始化。通过使用hasArray,我没有得到异常,但是我没有得到任何要反序列化的数据。它是空的