Spring、MVC、Java,让单例 bean 每 5 秒执行一次操作?

发布于 2024-10-31 12:37:35 字数 520 浏览 6 评论 0原文

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

我想要做的是删除用户操作和保存此哈希图对象之间的连接,并使其自动发生,例如每 5 秒一次。我可能必须尽快执行此操作,因为我们可能会扩展应用程序以在多个服务器上运行,因此会存在一个有点复杂的系统来在系统之间同步这些哈希图对象,并且让用户执行保存数据库的操作会使其变得太不可靠,我认为。

我不太确定如何解决这个问题,我认为线程不起作用,因为它是一个单例 bean 实例,所以我不知道是否可以通过线程访问它(因为 hashmap 对象中的数据会改变线程启动后)。非常感谢任何有关如何处理此问题/资源的建议

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-11-07 12:37:35

我推荐使用 Spring 中内置的 ExecutorService 或 Quartz 计时器服务。

I'd recommend either an ExecutorService or the Quartz timer service that's built into Spring.

护你周全 2024-11-07 12:37:35

我会考虑研究 Quartz

I would consider looking into Quartz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文