0
votes

I need to serialize ConcurrentLinkedQueue.

public class Service implements Serializable { 
    private static Queue<Message> messages = new ConcurrentLinkedQueue<>(); 

    public class Serialize {
        public static Queue<Message> getMessages() 
        { 
            return messages; 
        } 
    }
}

Trying:

FileOutputStream fos = new FileOutputStream("filename");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(Service.Serialize.getMessages());

Result:

java.io.NotSerializableException: server.Service$1 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:441) at java.util.concurrent.ConcurrentSkipListMap.writeObject(ConcurrentSkipListMap.java:1430) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)

How to???

2

2 Answers

0
votes

Somewhere in this class which you have omitted to post you are using and trying to serialize an anonymous inner class, as the exception clearly states.

-1
votes

Perhaps you should get rid of the inner class Serialize and put the method inside of it in Service..