Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing state in Java

Broad discussion question. Are there any libraries already which allow me to store the state of execution of my application in Java?

E.g I have an application which processes files, now the application may be forced to shutdown suddenly at some point.I want to store the information on what all files have been processed and what all have not been, and what stage the processing was on for the ongoing processes.

Are there already any libraries which abstract this functionality or I would have to implement it from scratch?

like image 967
Neeraj Avatar asked Feb 24 '12 07:02

Neeraj


People also ask

How do you save an object state in Java?

Saving object in java can be accomplished in a number of ways, the most prominent of which is to serialize the object - saving it to a file and reading the object using an ObjectOutputStream and ObjectInputStream, respectively. Serialization is a fantastic advantage to using java.

How is data stored in Java?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In JAVA , when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.

Can you store object as a value in Java?

Yes, since objects are also considered as datatypes (reference) in Java, you can create an array of the type of a particular class and, populate it with instances of that class.

What is object in Java with example?

Java Classes/Objects Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.


1 Answers

It seems like what you are looking for is serialization which can be performed with the Java Serialization API.

You can write even less code if you decide to use known libraries such as Apache Commons Lang, and its SerializationUtils class which itself is built on top the Java Serialization API.

Using the latest, serializing/deserializing your application state into a file is done in a few lines.

The only thing you have to do is create a class holding your application state, let's call it... ApplicationState :-) It can look like that:

class ApplicationState {

 enum ProcessState {
  READ_DONE,
  PROCESSING_STARTED,
  PROCESSING_ENDED,
  ANOTHER_STATE;
 }

 private List<String> filesDone, filesToDo;
 private String currentlyProcessingFile;
 private ProcessState currentProcessState;
}

With such a structure, and using SerializationUtils, serializing is done the following way:

try {
      ApplicationState state = new ApplicationState();
      ...
      // File to serialize object to
      String fileName = "applicationState.ser";

      // New file output stream for the file
      FileOutputStream fos = new FileOutputStream(fileName);

      // Serialize String
      SerializationUtils.serialize(state, fos);
      fos.close();

      // Open FileInputStream to the file
      FileInputStream fis = new FileInputStream(fileName);

      // Deserialize and cast into String
      String ser = (String) SerializationUtils.deserialize(fis);
      System.out.println(ser);
      fis.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
like image 86
Jalayn Avatar answered Sep 30 '22 21:09

Jalayn