简体   繁体   English

Spring,MVC,Java,每5秒制作一个Singleton bean动作一次?

[英]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. 我有一个Spring MVC应用程序,在这里我使用的是存储大型哈希图对象的单例bean。 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. 我正在使用NeoDats对象数据库将其持久化,因为它是一种搜索引擎应用程序,并且我们正在使用它,以便我们可以搜索hashmap对象(它比boolean关键字搜索引擎更复杂),该hashmap对象经常更新为来自用户的数据(它们添加到分类数据的标签)目前,我的问题是我一直在根据用户操作将哈希图对象持久保存到NeoDatis DB,但由于保存大型对象可能需要花费几秒钟的时间,如果用户保存得太快,有时会遇到问题。

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. 我想做的是删除用户操作和保存此hashmap对象之间的连接,而是使它自动发生,例如每5秒发生一次。 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). 我不太确定如何解决这个问题,我认为线程不会工作,因为它是一个单例bean实例,所以我不知道我是否可以通过线程访问它(因为hashmap对象中的数据会改变线程启动后)。 Any advice on how to approach this / resources is greatly appreciated 任何有关如何处理此资源的建议都将受到赞赏

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

我会考虑研究石英

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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