英文:
How to resume Java command line tool execution upon restart
问题
我正在开发一个类似以下方式工作的Java命令行工具:
- 下载一个文件(其中包含一些键/值对的数据)。
- 处理整个文件并将键/值对存储在内存中的TreeMap中(以键排序)。
- 遍历TreeMap并将每个键/值Map条目传递给外部库。基本上,它的任务是按'键'对键/值条目进行排序,然后将它们传递给外部方法。
以下是CLI工具中的伪代码:
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);
在这里,对externalMethod
的调用顺序应严格基于JSON文件中的键的排序顺序(由于我的应用程序的性质)。例如:
externalMethod.sendVersion(1, "version1Content");
externalMethod.sendVersion(2, "version2Content");
externalMethod.sendVersion(3, "version3Content");
我想知道是否可能在某种情况下从工具中断的地方恢复执行,例如因为某种原因终止了执行(比如,用户按下Ctrl+C)。假设文件内容保持不变。
我的用例还要求对于给定的键/值对,外部库方法不能被调用多次。因此,在为一些键/值对调用外部库后,如果程序停止,我需要从程序停止的地方继续,而不是从头开始。
是否可以通过Java命令行工具实现此行为?如果不能,我想知道还有哪些其他选项可以实现这一点?
提前感谢您的回答!
英文:
I am working on a Java command line tool that works like below:
- Downloads a file (which contains some key/value pairs of data)
- Processes the entire file and stores key/value pairs in an in-memory TreeMap (to sort by key).
- 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!
答案1
得分: 1
我猜想您的使用情况是希望构建一个命令行应用程序,供最终用户将数据提交到外部系统中。
为了实现“恢复”过程,这需要“持久性”存储来记住其“停止”的时间点。
因此,您可能需要包括一个“基于文件”的数据库,比如 H2、HSQL 等,或者使用一个简单的文本文件,来记住已经处理过的记录。
假设您的命令行是这样的:java -jar app.jar data1.json,那么在您的程序中可以立即创建一个 data1-inprogress.dat 文件。然后将您成功处理的每条记录都写入您的 externalMethod。
因此,如果应用程序被中断,用户重新运行相同的命令,您的程序可以确定无论 data1.json 是否有 data1-inprogress.dat,以指示尚未完成。然后,您可以开始编写所有条件语句,将 data1.json 和 dat1-inprogress.dat 之间的情况进行对比,以确保您的程序不会重新处理旧数据。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论