0

I have an object in my Spring application which I want to persist whenever some new data is written into it.

This is my object,

public class MessageBroker implements Serializable{


    private List<Message> backbone;

    private Map<String, List<Message>> messagesReceived;

    private Map<String, List<Message>> messagesSent;


    public MessageBroker(){

        backbone = new ArrayList<>();
        messagesReceived = new ConcurrentHashMap<>();
        messagesSent = new ConcurrentHashMap<>();
    }
..........

Now in the send method on the object, I have this,

public void send(Message message){

  .........
  FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("./data/store.ser");
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(this);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

This is my Message object,

public class Message implements Serializable{

    @Getter @Setter
    private String senderId;
    @Getter  @Setter
    private String receiverId;
    @Getter  @Setter
    private String mediaId;
}

I get the following error,

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.MessageBroker

How am I getting this since I've already implemented Serializable.

2 Answers2

0

Can you please post your complete MessageBroker class? And have you checked all fields inside your MessageBroker class?

I am asking this because if you for example have Logger object inside your class (a lot of people is using loggers :) ) which is not declared as static it can produce this error.

Nik.exe
  • 391
  • 3
  • 5
-1

Please make sure also Message and all its's fields implements Serializable. If this is not the solution please provide addition information on the used objects.

Repi
  • 68
  • 4
  • Message is also Serializable. What additional information would help? –  Nov 11 '16 at 23:05
  • I've edited my answer to update the Message class. –  Nov 11 '16 at 23:07
  • So the a lest one referenced Object within your MessageBroker or the Message is not serializable. It can also be that a message which is in the map of the broker, holds a reference to an object with is not serializable. – Repi Nov 11 '16 at 23:08
  • @LarsRepenning exception would point out what member if any is not serializable. – Antoniossss Nov 11 '16 at 23:13