3

If a mail is send to my inbox, I recieve a message, and I'm inserting the contents into DB. I have a org.springframework.integration.core.Message something like follows:

public void receive(Message<?> message)
{
    //I am inserting message contents into DB
}

Now in the event of failure, I wanted to have fail safe recovery mechanism, what I am thinking is to serialize the Message object into a file and later deserialize and update to DB.

Question 1. In this situation how to serialize the Message object? 2. Other than serialization any other mechanism that can be used?

EDIT I have not done Serialization before, I heard like the class should implements Serializable in order to use ObjectOutputStream, in this case I don't want to create a subclass of Message, So how to serialize Message to File?

Mahendran
  • 2,191
  • 5
  • 24
  • 40
  • You can write the message content to a file and later on read it. – me_digvijay Jan 03 '13 at 07:25
  • How to do that without Subclassing Message and implements Serializable? – Mahendran Jan 03 '13 at 07:40
  • For that you have to manually implement serialization. For example if your message obj has a property called sender: then you can write the in the text file as $sender: "name" And while reading back you create a new message object, read the saved file, parse its $sender and assign it to the message object you have created. – me_digvijay Jan 03 '13 at 07:43
  • 1
    But yes parsing will be another headache. – me_digvijay Jan 03 '13 at 07:47

3 Answers3

7

Sure, there are many serialization mechanisms apart from the jvm one.

  • XML
  • JSON
  • BSON
  • MessagePack
  • protobufs
  • ...

Some of them are text-based, some are binary. All have drawbacks and pluses. Text-based ones are human-readable, binary ones are faster and take up less space.

There are java libraries that handle all the above formats: JAXB (XML), Jackson (JSON), etc.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

In this situation how to serialize the Message object? Other than serialization any other mechanism that can be used?

Extract all the data you need from the Message and save it. You can do this in any manner you choose.

You can deserialize it by populating a new Message with the data you saved.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
-1

I don't know if I probably understood it al right.. but assuming Message is not much more than lots of strings and some integers you can just use directly an ObjectOutputStream and write it to a file (binary) and then readin later. Why not?

Message e = new Message();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("whatever");
oos.writeObject(message);

// read in
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("whatever");
Message e = (Message) ois.readObject();
Stefan
  • 2,603
  • 2
  • 33
  • 62