Java 第一次调用方法,然后在数据发生任何更改时再次调用该方法
解决这个问题的最佳方法是什么?我想在第一次运行应用程序时调用一个特定的方法,但第二次应该只在有任何更改时调用它。如果没有更改,那么我不想调用该方法 下面是将从我的主应用程序调用的方法。在下面的方法中,我从数据库中检索数据。这是第一次,在从数据库检索数据之后,map将有如下内容-Java 第一次调用方法,然后在数据发生任何更改时再次调用该方法,java,database,multithreading,nosql,linkedhashmap,Java,Database,Multithreading,Nosql,Linkedhashmap,解决这个问题的最佳方法是什么?我想在第一次运行应用程序时调用一个特定的方法,但第二次应该只在有任何更改时调用它。如果没有更改,那么我不想调用该方法 下面是将从我的主应用程序调用的方法。在下面的方法中,我从数据库中检索数据。这是第一次,在从数据库检索数据之后,map将有如下内容- BundleFramework 1.0.0 Bundle-A 1.0.0 Bundle-B 1.0.0 现在,我想分别打印BundleFramework及其版本。然后调用proce
BundleFramework 1.0.0
Bundle-A 1.0.0
Bundle-B 1.0.0
现在,我想分别打印BundleFramework及其版本。然后调用process方法,该方法将包括Bundle-A和Bundle-B及其在map中的版本,这是第一次
现在,我的后台线程每两秒钟运行一次,这也将调用getBundlesInformation
方法。现在,如果Bundle-A和Bundle-B的版本没有更改,那么我不想调用process method,但是如果Bundle-A或Bundle-B的任何版本有任何更改,那么只将该更改传递到process method
在我下面的代码中,我几乎做到了这一点,但目前正在发生的是——如果Bundle-A和Bundle-B版本的数据库中没有任何更改,那么它也会使用Bundle-A和Bundle-B信息调用process方法
我想要的是——第一次(从主应用程序运行时),它应该使用Bundle-A和Bundle-B调用process方法,下一次它应该只在该Bundle的版本发生任何更改时调用process方法
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;
}
publicstaticmap frameworkInfo=newlinkedhashmap();
public static Map bundleList=新建LinkedHashMap();
public static Map newBundleList=newlinkedhashmap();
私有静态映射oldBundleList=新LinkedHashMap();
私有静态void getAttributesFromDatabase(){
Map bundleInformation=新建LinkedHashMap();
bundleInformation=getFromDatabase();
如果(!bundleInformation.get(“BundleFramework”).equals(frameworkInfo.get(“BundleFramework”)){
frameworkInfo.put(“BundleFramework”,bundleInformation.get(“BundleFramework”);
String version=frameworkInfo.get(“BundleFramework”);
printFrameworkBundle(“BundleFramework”,版本);
}
bundleInformation.删除(“BundleFramework”);
如果(!bundleInformation.isEmpty()){
oldBundleList=捆绑列表;
bundleList=bundleInformation;
}
final Map EntriesDifferenting=Maps.differenting(旧bundleList,bundleList).EntriesDifferenting();
如果(!EntriesDifferenting.isEmpty()){
for(字符串键:EntriesDifferenting.keySet()){
newBundleList.put(key,bundleList.get(key));
System.out.println(“{”+key+“=”+bundleList.get(key)+“}”);
}
流程(newBundleList);
}
流程(捆绑列表);
}
私有静态映射getFromDatabase(){
映射数据=新建LinkedHashMap();
字符串version0=“1.0.0”;
字符串version1=“1.0.0”;
字符串version2=“1.0.0”;
data.put(“BundleFramework”,版本0);
数据。put(“Bundle-A”,版本1);
数据。put(“Bundle-B”,版本2);
返回数据;
}
有人能帮我吗?我使用的是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();
}
}
公共类应用程序{
public static Map frameworkInfo=新建LinkedHashMap();
public static Map bundleList=新建LinkedHashMap();
public static Map newBundleList=newlinkedhashmap();
私有静态映射oldBundleList=新LinkedHashMap();
公共静态void main(字符串[]args){
getAttributesFromDatabase();
LoggingAftereveryx毫秒();
}
私有静态void makeCall(映射测试){
系统输出打印LN(测试);
}
私有静态void getAttributesFromDatabase(){
Map bundleInformation=新建LinkedHashMap();
bundleInformation=getFromDatabase();
如果(!bundleInformation.get(“Framework”).equals(frameworkInfo.get(“Framework”)){
frameworkInfo.put(“Framework”),bundleInformation.get(“Framework”);
String version=frameworkInfo.get(“Framework”);
printFrameworkBundle(“框架”,版本);
}
捆绑信息。删除(“框架”);
如果(!bundleInformation.isEmpty()){
oldBundleList=捆绑列表;
bundleList=bundleInformation;
}
if(oldBundleList!=null){
final Map EntriesDifferenting=Maps.differenting(旧bundleList,bundleList).EntriesDifferenting();
如果(!EntriesDifferenting.isEmpty()){
for(字符串键:EntriesDifferenting.keySet()){
newBundleList.put(key,bundleList.get(key));
System.out.println(“{”+key+“=”+bundleList.get(key)+“}”);
}
makeCall(newBundleList);
}
}否则{
makeCall(bundleList);
}
}
私有静态void printFrameworkBundle(字符串、字符串版本){
System.out.println(字符串+“-”+版本);
}
私有静态映射getFromDatabase(){
Map hello=新建LinkedHashMap();
字符串version0=“1.0.0”;
字符串version1=“1.0.0”;
字符串version2=“1.0.0”;
hello.put(“框架”,版本0);
hello.put(“BundleA”,version1);
hello.put(“BundleB”,version2);
回复你好;
}
/**
*这是一个简单的函数,它将在每次
*X毫秒
*/
私有静态无效日志Aftereveryx毫秒(){
新线程(){
公开募捐{
while(true){
试一试{
睡眠(1000);
}捕获(中断异常例外){
}
getAttributesFromDatab
if(!(BundleFrameworkInfo.get("BundleFramework") != null &&
BundleFrameworkInfo.get("BundleFramework").equals(bundleList.get("BundleFramework"))))
if(!bundleList.isEmpty()) {
oldBundleList = bundleList;
bundleList = bundleList; // what would self-assigning achieve?
}
if(!bundleInformation.isEmpty()) {
oldBundleList = bundleList;
bundleList = bundleInformation;
}
if(!oldBundleList.isEmpty()) {
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);
}
} else {
process(bundleList);
}
// redundant
Map<String, String> bundleInformation = new LinkedHashMap<String, String>();
bundleInformation = getFromDatabase();
// initialize directly
Map<String, String> bundleInformation = getFromDatabase();