Question

Alright everyone, I'm still in the process of self-teaching about writing server-side apps, and I'm working on a rough draft for an iOS app I have planned.

What I intend to do is have the app submit and request values to and from basically what amounts to be a HashMapof different keys and values, the exact behavior of which isn't necessary to explain.

Obviously, to maintain the stored server data across running instances of the app (i.e. restarts, adding more machines, etc.), I intend to use a FileWriter to save the HashMap to a file. Idea being, when the server application is first run, it pulls the saved HashMap into a instance for "working storage".

Do any of you have ideas on how to implement a setup that would periodically (every 1 to 10 seconds, suppose) save the file to make the "working example" and file synchronized (to prevent against loss of data due to power outage, program failure, act of God, etc.).

I looked up the reference for FileWriter and couldn't see anything listed. I'd definitely prefer to use a method more sophisticated than overwriting, destroying and reopening a file every second.

Word to the wary - I'm a bit new to Java. I can hold my own in the core language, but I don't have a massive inbuilt memory of all the different classes and interfaces that exist. A detailed description will make me even more happy.

Was it helpful?

Solution

Don't use FileWriter unless you are only writing text.

Without more information I would suggest using XMLEncoder. This has the advantage over binary formats that you can read and even modify the contents. If you don't like XML, I suggest using YAML e.g. snakeyaml and this has a much nicer format.

You would have to lock access to the HashMap to avoid corruption. I also suggest writing to a new file and only deleting the old file when you have written successfully. This avoids loss data in the event of a failure while writing.

OTHER TIPS

One thing you could do is have the autosaving mechanism live in a different thread that wakes up periodically and writes the HashMap using the FileWriter. One way you could do this is by creating an inner class that extends Runnable and has an instance variable that points to the HashMap that you want to write.

So maybe something like this:

class AutoSaver implements Runnable {
    private final HashMap<?, ?> mapToSave;
    private final FileWriter writer;
    public AutoSaver(HashMap<?, ?> mapToSave) {
        this.mapToSave = mapToSave;
        // initialize FileWriter
    }

    @Override
    public void run() {
        // write HashMap
        Thread.sleep(interval);
    }
}

And then to start it up:

new Thread(new AutoSaver(mapToSave)).start();

You do have to be careful with this though, because with multiple threads accessing the same object you'll now have to deal with concurrency issues. One way to fix this would be to use ConcurrentHashMap instead of plain HashMap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top