0

我正在做一个项目,我需要以某种方式从数据库中打印出数据。举个例子,假设在我的数据库中,我只有以下条目-

Framework 1.0.0
BundleB 1.0.0
BundleC 1.0.0

然后我的 Java 方法将调用数据库,这将返回我上述数据的映射。

我的地图将包含以下数据-

Key as Framework, Value as 1.0.0
Key as BundleB, Value as 1.0.0
Key as BundleC, Value as 1.0.0

假设,我第一次启动我的程序,然后它会用下面的代码打印出来,这很好。

Framework - 1.0.0

然后我每 2 秒运行一次后台线程,它将调用数据库并再次从数据库中获取所有数据。每隔两秒,它就会打印出与下面相同的内容——(这不是我想要的)

Framework - 1.0.0

我想Framework - 1.0.0在运行程序时第一次打印出来,但在后台线程运行时第二次打印出来,它应该只在该框架的版本发生更改时打印出来,否则不打印任何东西。

意思是一段时间后,如果有人像这样更改数据库中的版本信息-

Framework 1.0.1
BundleB 1.0.0
BundleC 1.0.0

那么只有它应该像这样打印出来-

Framework - 1.0.1

我希望问题足够清楚。以下是我到目前为止的代码。

public class Test {

    public static Map<String, String> bundleList = 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(!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()) {
                bundleList.put(key, bundleList.get(key));
            } 
        }

        String version = bundleList.get("Framework");
        printOutZeroInformation("Framework", version);
    }

    private static void printOutZeroInformation(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;
    }

    private static void loggingAfterEveryXMilliseconds() {
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {

                    }
                    getAttributesFromDatabase();
                }
            }
        }.start();
    }
}

任何帮助将不胜感激。

4

1 回答 1

0

我没有运行它。我只是给你一个想法。尝试与打印相同的东西。如果你不明白,请告诉我。

private static void 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);

        //The following code will update bundleList only when it's different from hello
        if (!bundleList.isEmpty()) {
            if (bundleList.get("Framework") != hello.get("Framework"))
                bundleList.put("Framework", version0)
            if (bundleList.get("BundleA") != hello.get("BundleA"))
                bundleList.put("BundleA", version1)
            if (bundleList.get("BundleB") != hello.get("BundleB"))
                bundleList.put("BundleB", version2)
        }
        else { //if this is the first time
            bundleList.put("Framework", version0)
            bundleList.put("BundleA", version1)
            bundleList.put("BundleB", version2)
        }
    }
于 2013-08-20T08:06:09.643 回答