简体   繁体   中英

Spring, MVC, Java, making a singleton bean do an action every 5 seconds?

I have a Spring MVC app where I am using a singleton bean that stores a large hashmap object. I am using NeoDats object DB to persist this as it is a sort of search engine application and we are using it so that we can search through the hashmap object (its more complex than boolean keyword search engine), this hashmap object is frequently updated with data from the user (tags that they add to the categorize data) The issue I currently is that I had been persisting the hashmap object to the NeoDatis DB based on user actions, but since it can take a few seconds to save the large object, this runs into issues sometimes if the user saves too fast.

What I would like to do is remove the connection between user action and saving this hashmap object and rather make it something that occurs automatically, such as every 5 seconds. I may have to do this soon as we may be expanding the app to run on more than one server so there would be a somewhat complex system for synchronizing these hashmap objects between systems and having user actions for saving the DB would make it too unreliable, I think.

I am not quite sure how to approach this, I don't think threading will work because its a singleton bean instance so I don't know if I can gain access to that through the thread (as the data in the hashmap object would change after the thread was started). Any advice on how to approach this / resources is greatly appreciated

我建议使用ExecutorService或Spring内置的Quartz计时器服务。

我会考虑研究石英

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