简体   繁体   中英

Sharing cached resources between java classes

I have three classes, testContainer.java, recurringTest.java and databaseHelper.java

testContainer class runs only once and stays alive while recurringTest runs again and again.

public class testContainerA extends someExternalClass {
    public void initialize() {
        databaseHelper.getResoucefromDB(A)
        runTestContinuously(new recurringTestA());
    }
}
public class recurringTestA {
    // some load tests that accesses resourceMap.get(A)

}

public class testContainerB extends someExternalClass {
    public void initialize() {
        databaseHelper.getResoucefromDB(B)
        runTestContinuously(new recurringTestB());
    }
}
public class recurringTestB {
    // some different load tests that accesses resourceMap.get(B)
}

public class databaseHelper{
    public static HashMap<String, List<resource>> resourceMap;
    public void getResoucefromDB(String resourceType) {
        resourceList.put(resourceType, callDatabase(resourceType));
    }
}

Requirements:

  1. recurringTest constructor cannot have any parameters.
  2. cannot use any files to save the resource list.
  3. recurringTestA and recurringTestB are set up such that they run simultaenously

I tried saving the resources in a file and it works. I do not want to use any file though. How do I cache/save the resources so that it can be accessed inside the test without calling the database more than once? How do I save resources in resourceList so that the recurring tests can access it without having to call DB again and again.

可能这将是一个糟糕的答案,但您可以尝试为此使用静态变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM