alwaysLearner
alwaysLearner

Reputation: 1

How to resume Java command line tool execution upon restart

I am working on a Java command line tool that works like below:

  1. Downloads a file (which contains some key/value pairs of data)
  2. Processes the entire file and stores key/value pairs in an in-memory TreeMap (to sort by key).
  3. Iterates through the TreeMap and passes each key/value Map entry to an external library. So, basically its job is to sort the key/value entries by 'key' and then pass them to an external method.
Sample file content (key-value pairs in JSON):
[{   
      "key" : 2
      "value" : "version2Content"
 },
 {   
      "key" : 1
      "value" : "version1Content"
 }, 
 {   
      "key" : 3
      "value" : "version3Content"
 }]

Psuedo code in the CLI tool:

List<VersionPair> pairs = getVersionPairsFromJSON(input); 

TreeMap<Long, String> orderedEntries = new TreeMap<>();

for (VersionPair pair : pairs) {
    orderedEntries.put(pair.getKey(), pair.getVersionContent());
}

for (Map.Entry<Long, String> entry : orderedEntries) {
    externalMethod.sendVersion(entry.getKey(), entry.getValue())
        .whenComplete((metadata, error) -> {
            if (error != null) {
                System.err.println("Failed to send Version with key: " + entry.getKey() + ", with content: " + entry.getKey());
            }
        });
}

// POJO for VersionPair
public class VersionPair {
    Long key;
    String versionContent;
    ...
    // constructor, getters and setters
}
public CompletableFuture<Metadata> externalMethod(Long key, String versionContent);

In here, the order of calls to externalMethod should strictly be based on the sorted order of keys in the JSON file (due to the nature of my application). Eg:

externalMethod.sendVersion(1, "version1Content");
externalMethod.sendVersion(2, "version2Content");
externalMethod.sendVersion(3, "version3Content");

I would like to know if its possible to resume execution of this tool from where it left off, in case the execution is terminated for some reason. (Say, a user hit Ctrl+C). Assume that the file content remains unchanged.

My use-case also requires that the external library method cannot be called more than once, for a given key/value pair. So in case the program is stopped after calling the external library for some key/value pairs, I need to resume from where the program stopped, instead of starting all over again.

Is it possible to achieve this behavior with Java Command line tools ? If not, may I know what other options do we have that can achieve this ?

Thanks in advance!

Upvotes: 0

Views: 103

Answers (1)

Chun
Chun

Reputation: 519

My guess your use case is you want to build a command line application for end user to submit data into external system.

In order to achieve "resume" process, this requires "persistence" storage to remember the point in time where its "stop".

Therefore, you may need to include a "file-based" database, such as H2, HSQL etc. or using a simple text file, to remember which records has been processed.

Let say your command line is like this: java -jar app.jar data1.json, then in your program may create a data1-inprogress.dat file immediately. Then write down each record that you have successfully process your externalMethod.

So if the application is interrupted, and user rerun the same command, your program can determine whatever data1.json has data1-inprogress.dat to indicate is not finished. then you can start writing all the condition statement between data1.json and dat1-inprogress.dat to make sure your program does not reprocess the old data.

Upvotes: 1

Related Questions