I have two class, First class(ClassServletA.java) is the HttpServlet which stores the ipaddress and access time in the HashMap, I want to back up the HashMap in DB on daily basis, So I am Scheduling the task and storing the static HashMap object in DB and then reinitialize HashMap (after storing in DB) .
Is it possible to Lock the static Object Globally ?
public class ClassServletA {
public static Map<String,String> myMap = new HashMap<String, String>();
void doGet(HttpServeltRequest request , HttpServletResponse response){
myMap.put("ipaddress", "accessTime");
}
}
Second Class is the scheduler:
public class MyDailyTask implements Job {
void executeMethod(){
//Writing the map object to file or database login here
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(ClassServletA.myMap);
out.flush();
out.close();
// Reinitialize the hashmap
ClassServletA.myMap=new HashMap<String,String> ();
}
}
Is it possible to lock OR avoid the modification of the ClassServletA.myMap Map object globally within scheduling time period when the scheduler ( MyDailyTask.java) executes the executeMethod().