简体   繁体   English

akka Serialization ByteBuffer - java.lang.UnsupportedOperationException

[英]akka Serialization ByteBuffer - java.lang.UnsupportedOperationException

I need some help to serialize an Entity to send it by Akka Remote. 我需要一些帮助来序列化实体以通过Akka Remote发送它。

This is the serializer class: 这是序列化程序类:

@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;
    }

Im getting the following exception in line 5 我在第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)

This is the message Im sending to the remote actor: 这是我发送给远程actor的消息:

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;
    }
}

The weird thing is that it works in one way, if we send the message using this it works fine in the receptor: 奇怪的是,它以一种方式工作,如果我们使用它发送消息,它在接收器中工作正常:

ActorSelection selection = getContext().system().actorSelection("akka://applicationremote@localhost:25521/user/actors.MessageReceiverActor");
selection.tell(message2Serialize, getSelf());

But when we replay to the sender actor using getSender().tell(m, getSelf()); 但是当我们使用getSender().tell(m, getSelf());重放发送者actor时, getSender().tell(m, getSelf()); then we got the exception. 然后我们得到了例外。

We are using Java 1.8 and akka-remote_2.11:2.5.3 我们使用的是Java 1.8和akka-remote_2.11:2.5.3

Thanks in advance! 提前致谢! Rodri Rodri

javadoc extract javadoc提取物

@throws UnsupportedOperationException. @throws UnsupportedOperationException。 If this buffer is not backed by an accessible array 如果此缓冲区未由可访问的数组支持

the ByteBuffer seems not to be completely initialized... furthermore the javadoc also says what to do 似乎没有完全初始化ByteBuffer ......此外,javadoc还说要做什么

Invoke the {@link #hasArray hasArray} method before invoking this method in order to ensure that this buffer has an accessible backing array. 在调用此方法之前调用{@link #hasArray hasArray}方法,以确保此缓冲区具有可访问的后备阵列。

By changing this line: bis = newByteArrayInputStream(arr); 通过改变这一行: bis = newByteArrayInputStream(arr); to

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

It works, but Im not sure why. 它有效,但我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM