完成此問題的最佳方法是什麼?我想在第一次運行我的應用程序時調用特定的方法,但第二次應該只在有任何更改時才調用它。如果沒有改變,那麼我不想調用該方法。第一次調用方法,然後每當數據發生變化時再調用一次方法
下面是將從我的主應用程序調用的方法。在下面的方法中,我正在從數據庫中檢索數據。這是第一次,從檢索數據庫中的數據後,地圖將有類似如下─
BundleFramework 1.0.0
Bundle-A 1.0.0
Bundle-B 1.0.0
現在,我想單獨打印出來BundleFramework及其版本。然後調用第一次包含Bundle-A和Bundle-B及其版本的流程方法。
現在我的後臺線程每兩秒運行一次,這也會調用getBundlesInformation
方法。現在,如果Bundle-A and Bundle-B
的版本沒有變化,那麼我不想調用處理方法,但是如果Bundle-A or Bundle-B
的版本中的任何版本有任何更改,則只將該更改傳遞給process method
。
我幾乎在我的下面的代碼,但目前發生了什麼 - 如果數據庫沒有變化的Bundle-A和Bundle-B版本,那麼它也調用與Bundle-A和Bundle-B的處理方法信息。
我想要的是 - 第一次(從主應用程序運行時),它應該使用Bundle-A和Bundle-B調用過程方法,並且下一次它應該只在調用過程方法時發生任何更改該捆綁包的版本。
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>();
private static void getAttributesFromDatabase() {
Map<String, String> bundleInformation = new LinkedHashMap<String, String>();
bundleInformation = getFromDatabase();
if(!bundleInformation.get("BundleFramework").equals(frameworkInfo.get("BundleFramework"))) {
frameworkInfo.put("BundleFramework", bundleInformation.get("BundleFramework"));
String version = frameworkInfo.get("BundleFramework");
printFrameworkBundle("BundleFramework", version);
}
bundleInformation.remove("BundleFramework");
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 Map<String, String> getFromDatabase() {
Map<String, String> data= new LinkedHashMap<String, String>();
String version0 = "1.0.0";
String version1 = "1.0.0";
String version2 = "1.0.0";
data.put("BundleFramework", version0);
data.put("Bundle-A", version1);
data.put("Bundle-B", version2);
return data;
}
有人能幫助我嗎?我正在使用Nosql數據庫,所以我不能有觸發器功能。
更新的代碼: -
下面是我的全部代碼 -
public class App {
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 makeCall(Map<String, String> test) {
System.out.println(test);
}
private static void getAttributesFromDatabase() {
Map<String, String> bundleInformation = new LinkedHashMap<String, String>();
bundleInformation = getFromDatabase();
if(!bundleInformation.get("Framework").equals(frameworkInfo.get("Framework"))) {
frameworkInfo.put("Framework", bundleInformation.get("Framework"));
String version = frameworkInfo.get("Framework");
printFrameworkBundle("Framework", version);
}
bundleInformation.remove("Framework");
if(!bundleInformation.isEmpty()) {
oldBundleList = bundleList;
bundleList = bundleInformation;
}
if(oldBundleList != null) {
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) + "}");
}
makeCall(newBundleList);
}
} else {
makeCall(bundleList);
}
}
private static void printFrameworkBundle(String string, String version) {
System.out.println(string+" - "+version);
}
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("Framework", version0);
hello.put("BundleA", version1);
hello.put("BundleB", version2);
return hello;
}
/**
* This is a simple which will call the logHistogramInfo method after every
* X Milliseconds
*/
private static void loggingAfterEveryXMilliseconds() {
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
getAttributesFromDatabase();
}
}
}.start();
}
}
:好的。我補充說,但如果這些軟件包的版本沒有變化,它仍然會第二次調用過程方法。如果你願意,你也可以嘗試運行它。 – ferhan
堅持..我搞砸了莫名其妙..讓我編輯代碼馬上爲你.. – ferhan
我剛糾正我的代碼..讓我知道如果仍然不清楚.. – ferhan