java - 第一次调用一个方法,当数据有变化时再次调用该方法

标签 java database multithreading nosql linkedhashmap

解决这个问题的最佳方法是什么?我想在运行应用程序时第一次调用特定方法,但第二次只有在发生任何更改时才应调用该方法。如果没有变化,那么我不想调用该方法。

下面是将从我的主应用程序调用的方法。在下面的方法中,我从数据库中检索数据。第一次,从数据库检索数据后, map 将具有如下内容-

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 方法,但如果 Bundle-A 和 Bundle-B 的任何版本有任何变化>Bundle-A 或 Bundle-B,然后仅将该更改传递给处理方法

我的下面的代码几乎就在那里,但当前发生的是 - 如果 Bundle-A 和 Bundle-B 版本的数据库没有更改,那么它也会使用 Bundle-A 和 Bundle-B 信息调用 process 方法.

我想要的是 - 第一次(从主应用程序运行时),它应该使用 Bundle-A 和 Bundle-B 调用 process 方法,下次它应该仅在有任何更改时调用 process 方法该 bundle 的版本。

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();
    }
}

最佳答案

使用String#equals()进行字符串比较。即使两个字符串在内容上相等,不等于 != 也很可能返回 true。因此,将您的 if 条件更改为

if(!(BundleFrameworkInfo.get("BundleFramework") != null &&
   BundleFrameworkInfo.get("BundleFramework").equals(bundleList.get("BundleFramework"))))

你的其余代码非常困惑。例如,

if(!bundleList.isEmpty()) {
    oldBundleList = bundleList;
    bundleList = bundleList; // what would self-assigning achieve?
}

此外,由于只要 bundleList 为空,您就将 oldBundleList 设置为 bundleList。因此,假设 Maps.difference() 按预期工作,它根本不应该返回任何差异。

编辑:(根据OP的更新代码)

getAttributesFromDatabase() 的最后一部分更改为

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);
}

编辑:(冗余代码)

您有很多冗余/空的Map创作。例如,

// redundant
Map<String, String> bundleInformation = new LinkedHashMap<String, String>();    
bundleInformation = getFromDatabase();

// initialize directly
Map<String, String> bundleInformation = getFromDatabase();

成员变量bundleListoldBundleList也是如此。

关于java - 第一次调用一个方法,当数据有变化时再次调用该方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18348435/

相关文章:

Java-ArrayIndexOutOfBoundsException 错误

database - 什么是跨数据中心主-主复制的好解决方案?

database - 使用索引在数据库中进行不区分大小写的搜索?

java - Tomcat 中的连接池 Scanario

java - 如何并行运行不同的方法

java - 每个 Action 监听器的单独类(class)?

java - 从监听器触发 AsyncTask - AndroidStudio 1.2.2 - java.lang.NoClassDefFound

Java - 分配替换的字符串,或打印它

c - pthread - 暂停/取消暂停所有线程

c - Linux 生产多线程程序中信号处理的最佳实践是什么?