如何使用多个有状态bean实例?
我在学校项目中遇到以下问题。
- 许多汽车都将其位置数据发送到无状态 Web 服务,该服务将其转储到 Java 消息队列中。
- 该队列由消息驱动 bean 处理,它计算一些内容并将其发送到缓存。
- 该缓存/缓冲区需要为每辆车保留一组较小的缓冲区对象,并通过将该车的内容发送到另一个系统来定期清空每个缓冲区。
我在第 3 步中遇到了很多困难。我的第一个想法是为每辆车创建一个有状态的 bean 实例,以便系统中的每台机器都可以容纳多个这样的实例。单例 bean 将跟踪所有汽车的缓冲区。这有望使事情保持可扩展性。
尽管这看起来并不是事情的完成方式,因为一切都指向注入以及客户端和有状态 bean 之间的 1-1 关系,而我似乎需要 1-多关系。
经过一番研究,我发现有关此类场景的在线信息充其量是模糊的,最坏的情况是无关紧要的。我的书“Begginning Java EE 6 Platform with Glassfish 3”似乎根本没有解决这个问题。我已经联系了我的技术人员,但他还没有回复,我怀疑他是否真的了解很多。
我不愿意使用数据库,因为事情实际上不需要(也不应该!)存储的时间超过绝对必要的时间。
也许我错过了一些基本概念,或者也许我只是想得太多,需要一种完全不同的方法。我的问题是,在这种环境下,您将如何处理此类问题?我正在使用 Glassfish 3.0.1。
I have the following problem in a school project.
- Numerous cars all send their position data to a stateless webservice, which dumps it into a java message queue.
- This queue is handled by a message driven bean, which calculates some stuff and sends it to the cache.
- This cache/buffer needs to keep a set of smaller buffer objects for every car and empty every buffer periodically by sending the stuff for that car to another system.
I'm having a lot of difficulty with step 3. My first thought would be to make a stateful bean instance for every car, so that each machine in the system could hold a number of those. A singleton bean would keep track of the buffers for all cars. This would hopefully keep things scalable.
Though that is not the way things are done it seems, as everything points to injection and a 1-1 relation between clients and stateful beans, while I seem to need a 1-many relation.
After some research I discovered that the online information on these kinds of scenarios is vague at best, irrelevant at worst. My book 'Begginning Java EE 6 Platform with Glassfish 3' doesn't seem to address it at all. I've contacted my techer but he hasn't responded yet, and I'm doubtful he actually knows much about it.
I'm reluctant to use a database because things don't really need (and shouldn't!) be stored longer than absolutely neccesary.
Maybe I'm missing some fundamental concept, or maybe I'm just overthinking things and need a totally different approach. My question is how would you handle this type of problem in this environment? I'm using Glassfish 3.0.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么使用有状态会话 bean(必须这样做)?我会编写另一个 StatelessSessionBean,它可以从缓存中返回值,然后用另一个 Web 服务包装它。
Why use stateful session beans (do you have to)? I would write another StatelessSessionBean that can return values out of your cache then wrap that with another webservice.
我认为您错过了缓存实现的一些内容。您可以使用简单的 HashSet 来缓存数据。
或者您可以使用现有的缓存框架,如 memcache(谷歌缓存),它的性能非常好。请参阅 Memcache
如果你想定期清空缓存,你可以使用 java.util
班级计时器。
i think you missed something for the cache implementation. You could use a simple HashSet in order to cache your data.
Or you could use an existing cache framework like memcache (google cache) which is very performant. see Memcache
If you want to empty periodically the cache you can use java.util
Class Timer.