user710818
user710818

Reputation: 24248

Is it better to write in file or serialize in Java?

I need to create an application that will be run periodically, by scheduler.

From run to run I need to remember result of previous actions - if they will finished successfully or not, email was sent or not etc.

It is possible only save counter and date when last action happened.

Pro:

Contra:

I think it possible use serialization - in this case I will receive already class, so if in future will be need keep some additional information I will need only re-make class, but not system of saving.

Is it possible to combine the pros of these two systems?

Upvotes: 0

Views: 171

Answers (5)

jeroen_de_schutter
jeroen_de_schutter

Reputation: 1883

If you can wrap the result of your previous action into one single object, I would suggest serializing it to XML (or JSON) by using XStream. It is by far the easiest way to serialize Java objects to flat files I have ever used. Especially if you use annotations : http://x-stream.github.io/annotations-tutorial.html

Upvotes: 1

Prateek
Prateek

Reputation: 551

depends on your requirement of result object:

  1. If you convert it to something like a file containing JSON or XML then you can share this object with non java based apps and can easily transfer them over network.

  2. If you serialize result object using java serialization then, its clients can only be java based apps but they will be easier to share and transmit and faster than first option.

    Definitely, both approached have their trade offs so you need to choose as per your app's requirement.

Upvotes: 1

Sammy
Sammy

Reputation: 1218

It is possible only save counter and date when last action happened.

It should be easily be possible to read these two from file and instantiate an object with them. If you serialize, you should use remember, that you cannot deserialize, if your class has changed. So you should then go with xml. You can also dump and read an json-file with these information, this is also readable an has less overhead.

I would look at the solution an use that one, I understand most, but I would not use normal Java-serialization.

Upvotes: 1

ejboy
ejboy

Reputation: 4181

Depending on the use-case, ORM can be an overkill if it is only used for the persistence of the scheduled task. I think that Java Serialization should be enough for your case when external scheduling is used. Just keep in mind the binary compatibility to allow future changes in the model,e.g. defining serialVersionUID etc. See recommendations

You can also try some simple XML serialization like XStream.

Upvotes: 1

zeller
zeller

Reputation: 4974

it requires additional efforts for converting to class structure.


This shouldn't add a real overhead. So I'd suggest this version if your system is small enough.
If it is really complex, use a database with ORM (or NoSQL or whatever you prefer). That, I think will combine the advantages of these two options.

Upvotes: 2

Related Questions