如何设置一个锁,如果没有收到保持活动信号,该锁将自动超时?
我想限制对某个资源的访问。基本上,我使用的是会话级锁。然而,编写涵盖窗口关闭所有可能方式的 JavaScript 变得很痛苦。
一旦用户离开该页面,我想解锁资源。
我的基本想法是使用某种服务器端超时来解锁资源。基本上,如果我无法解锁资源,我希望有一个计时器启动并解锁资源。
例如,现在从客户端更新 30 秒后,解锁资源。
我的基本问题是,我可以使用什么样的技巧来做到这一点?据我了解,我不能只在 JSF 中创建线程,因为它是非托管的。
我确信其他人也这样做,那么正确的使用方法是什么?
谢谢, 格雷
I have a certain resouce I want to limit access to. Basically, I am using a session level lock. However, it is getting to be a pain writing JavaScript that covers every possible way a window can close.
Once the user leaves that page I would like to unlock the resouce.
My basic idea is to use some sort of server side timeout, to unlock the resouce. Basically, if I fail to unlock the resource, I want a timer to kick in and unlock the resouce.
For example, after 30 seconds with now update from the clientside, unlock the resouce.
My basic question, is what sort of side trick can I use to do this? It is my understanding, that I can't just create a thread in JSF, because it would be unmanaged.
I am sure other people do this kind of thing, what is the correct thing to use?
Thanks,
Grae
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 BalusC 所问的那样,最大的问题是您希望以什么粒度级别进行锁定?每个登录用户,对于所有用户,或者也许您可以摆脱每个请求的锁定?
或者,这将是一个更困难的问题,是单个页面请求获取锁,然后该特定页面旨在在请求之间保持锁定的想法吗?例如作为一种预订。我正在浏览酒店页面,当我仅查看某个房间时,我已在系统中为该房间进行了隐式预订,因此在我查看该房间时不会发生其他人真正预订该房间的情况吗?
在后一种情况下,也许以下方案可行:
这只是一般想法。在我构建的 Java EE 应用程序中,我使用了类似的东西(尽管不完全相同),并且效果很好。
或者,您可以使用石英作业来定期删除过时的条目。另一种替代方案是用 JBoss Cache 或 Infinispan 实例替换全局并发映射。这些允许您为其条目定义逐出策略,这样您就不必自己编写代码了。如果您从未使用过这些缓存,那么学习如何设置它们并正确配置它们可能比自己构建一个简单的石英作业更麻烦。
As BalusC right fully asked, the big question is at what level of granularity would you like to do this locking? Per logged-in user, for all users, or perhaps you could get away with locking per request?
Or, and this will be a tougher one, is the idea that a single page request grabs the lock and then that specific page is intended to keep the lock between requests? E.g. as a kind of reservation. I'm browsing a hotel page, and when I merely look at a room I have made an implicit reservation in the system for that room so it can't happen that somebody else reserves the room for real while I'm looking at it?
In the latter case, maybe the following scheme would work:
This just the general idea. In a Java EE application that I have build I have used something similar (though not exactly the same) and it worked quite well.
Alternatively you could use a quartz job anyway that periodically removed the stale entries. Yet another alternative for that is replacing the global concurrent map with e.g. a JBoss Cache or Infinispan instance. These allow you to define an eviction policy for their entries, which saves you from having to code this yourself. If you have never used those caches though, learning how to set them up and configuring them correctly can be more trouble than just building a simple quartz job yourself.