Search code examples
javafilecrashqueuemapdb

Looking for a crash-safe queue in java


I'm building a Java application that will run on a battery-powered, cellular-enabled device (not a mobile phone by the way), and needs to send commands to a server. These commands are in the form of JSON-objects, so they can easily be serialized and deserialized.

As internet connectivity may not be completely reliable, and the battery of the device may run out, I need a way of saving my commands to disk in case the battery runs out (which could, in some cases, cause power to switch off without warning).

The commands can be 'worth' a few euros a piece, so it's important that I take every precaution (within certain bounds of course) to make sure no commands are lost. Sending a command twice is not a problem, as every command is tagged with a GUID, and my server will make sure duplicates are ignored. The queue may contain up to a thousand commands, but most of the time it will be empty.

What I'm actually looking for is a Queue-like (FIFO) object with a backing file store that is made to survive an instant crash. I need to be able to peek at the next in line, and remove it after processing is finished.

Up to now, I've been working with MapDB 3.0, but the documentation is a bit confusing as to how to create a queue-like object. And besides, it seems to be a bit much for what I'm trying to achieve


Solution

  • You could have a directory of files. One file per message. The file name could be a timestamp or name which records the ordered. A directory with 1000 files should still perform ok.

    Once you close the file, it should be persisted to disk, although exact how safe any operation is will depend on the device and how it is implemented.