If your database is an RDBMS, you could put the logic to detect a change there, i.e. use a trigger to update a last modified column that your application would poll and call your process
method when it changes.
How to make sure that I am calling a certain method only whenever there is any change in the database?
-
25-06-2022 - |
Pergunta
What's the best way to accomplish this problem? I want to call a particular method for the first time when I am running my application but second time it should be called only whenever there is any change. If there is no change then I don't want to call that method.
I want to call process
method for the first time when I am running my application and it should print out like this- I don't want to use TestingFramework
entry in process method
call.
{Answer-A=1.0.0, Answer-B=1.0.0}
But after that I have a background thread running which will call getAttributesFromDatabase
method again so now I want to print out only the information that got changed but if there is no change then I don't want to call process
method again.
Suppose any value got changed for either Animal-A
or Animal-B
, then it should print out only the change information only..
Let's take an example- Suppose second time when my background thread is running, and map entry is like this without any change-
TestingFramework 1.0.0
Answer-A 1.0.0
Asnwer-B 1.0.0
then I don't want to call process method again as there was no change. But somehow supposed the value entry got changed for Answer-A
or Answer-B
, then at that time, I want to call process method with the entry that got changed.
I hope the question is clear enough.
public static Map<String, String> frameworkInfo = new LinkedHashMap<String, String>();
public static Map<String, String> bundleList = new LinkedHashMap<String, String>();
public static Map<String, String> newBundleList = new LinkedHashMap<String, String>();
private static Map<String, String> oldBundleList = new LinkedHashMap<String, String>();
public static void main(String[] args) {
getAttributesFromDatabase();
loggingAfterEveryXMilliseconds();
}
private static void getAttributesFromDatabase() {
Map<String, String> bundleInformation = new LinkedHashMap<String, String>();
bundleInformation = getFromDatabase();
if(TestingFrameworkInfo.get("TestingFramework") != (bundleInformation.get("TestingFramework"))) {
TestingFrameworkInfo.put("TestingFramework", bundleInformation.get("TestingFramework"));
String version = TestingFrameworkInfo.get("TestingFramework");
printTestingFrameworkBundle("TestingFramework", version);
}
bundleInformation.remove("TestingFramework");
if(!bundleInformation.isEmpty()) {
oldBundleList = bundleList;
bundleList = bundleInformation;
}
final Map<String, MapDifference.ValueDifference<String>> entriesDiffering = Maps.difference(oldBundleList, bundleList).entriesDiffering();
if (!entriesDiffering.isEmpty()) {
for (String key : entriesDiffering.keySet()) {
newBundleList.put(key, bundleList.get(key));
System.out.println("{" + key + "=" + bundleList.get(key) + "}");
}
process(newBundleList);
}
process(bundleList);
}
private static void process(final Map<String, String> test) {
System.out.println(test);
}
private static Map<String, String> getFromDatabase() {
Map<String, String> hello = new LinkedHashMap<String, String>();
String version0 = "1.0.0";
String version1 = "1.0.0";
String version2 = "1.0.0";
hello.put("TestingFramework", version0);
hello.put("Answer-A", version1);
hello.put("Answer-B", version2);
return hello;
}
private static void loggingAfterEveryXMilliseconds() {
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
getAttributesFromDatabase();
}
}
}.start();
}
With the below code, I have, it will call the process method for the first time and second time when there is no change then again it calls the process method which I don't want at all. Can anybody help me with this?
I am pretty much sure, I am missing one key thing here, and then it will start working I guess.
Updated:-
I am still working on this. Can anybody help me with this?
Solução