如何维护有状态会话 Bean 列表?
鉴于要开发一个Java EE应用程序,其目的是聊天服务器,我遇到了一些问题。
客户端应该能够使用不同的连接器(例如 HTTP、SOAP 或 AMF)进行连接。传入的请求需要转换为统一的内部消息。接下来,消息驱动 Bean 调用业务逻辑并将请求结果返回到特定连接器。
Q1:这听起来合理吗?
我的问题是如何存储每个客户端的会话信息。这将包括连接数据、时间戳、需要传递的命令等。 有状态会话 Bean 应该可以满足此目的,因为只要客户端连接,我就需要它们。然而,问题是维护所有 bean 的列表,以便能够为新请求选择正确的会话。 我无法将 SFSB 引用链接到 HttpSession(如其他地方推荐的那样),因为存在不同的连接器,而不仅仅是 HTTP 客户端。
Q2:正确的做法是什么?会话管理器,执行 JNDI 查找以创建新的 SFSB 并将其添加到内部列表中?
Q3:在集群环境下如何工作?我看到 SFSB 可以复制到不同的节点,但是如何同步会话管理器列表?
问题 4:除了负载均衡器之外,您还会推荐会话关联性吗?
谢谢。
Given the fact that a Java EE application is to develop, which has the purpose of a chat server, I encountered some problems.
Clients should be able to connect using different connectors like HTTP, SOAP or AMF. Incoming requests need to be converted to unified, internal messages. Next, message driven beans call the business logic and return the result of the request to the specific connector.
Q1: Does that sound reasonable?
My question is how do I store the session information for each client. That would include connection data, timestamps, commands which need to be delivered etc.
Stateful Session Beans should be fine for this, because I need them just as long as a client is connected. However, the problem is to maintain a list of all beans to be able to pick the correct session for a new request.
I cannot link the SFSB reference to a HttpSession (as recommended elsewhere) because there are different connectors, not only HTTP clients.
Q2: What is the correct approach for that? A session manager, which performs a JNDI lookup to create a new SFSB and adds it to an internal list?
Q3: How would it work in a clustered environment? I see that SFSBs can be replicated to different nodes, but how to sync the session manager list?
Q4: Would you recommend session affinity in addition to a load balancer?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问1:我猜。这取决于您的要求。只是不要让事情变得过于复杂。简单点。请注意,使用消息驱动 bean 之类的东西意味着异步处理/响应,并且消息可能会失败或进入系统异常/死信队列。
Q2:通常您希望通过 JNDI 查找 Java EE beans。在我看来,让生活更轻松。也许您需要一个 servlet 或无状态会话 bean 或前面的东西作为“控制器”?然后,您可以在需要数据时从数据库加载有状态 Bean 的数据,和/或在不再需要该 Bean 时将数据写入数据库。
Q3:取决于您如何配置集群以及您使用的应用程序服务器。每个服务器可能会以不同的方式实现集群/故障转移/同步等。我建议阅读您的应用程序服务器文档以找出答案。和/或发布您正在使用的应用程序服务器,以便该论坛上的其他人可以推荐一些东西。例如,我们使用 IBM WebSphere Application Server 版本 7.0.0.7,网络部署版。因此,在我们的例子中,我们可以在集群节点之间设置 bean/对象的复制。
问题 4:如果您希望最终用户的每个后续事务都保持与同一集群节点的连接,我相信答案是肯定的。 (至少从通过 HTTP 服务器进入的角度来看)。我猜如果您没有打开会话亲和性,那么您可以切换到不同的集群节点,并且定位数据可能会更困难(特别是如果您不在集群之间复制它)。这是可以做到的,只是可能会影响性能。所以我猜这里的答案是“是”。
Q1: I guess. It depends on your requirements. Just don't overcomplicate things. Go simple. And be aware that using things like message driven beans means asynchronous processing/responses and the chance for messages to fail or go to a system exception/dead letter queue.
Q2: Generally you want to look up Java EE beans via JNDI. Makes life easier IMO. Perhaps you need a servlet or a stateless session bean or something in front as a "controller"? Then you can load the data for the stateful beans from the database when you need the data and/or write the data to the database when you do not need the bean anymore.
Q3: Depends on how you configure your cluster and also on which application server you are using. Each server may possibly implement clustering/fail over/sync'ing, etc. differently. I would recommend reading up on your application server documentation to figure that one out. And/or post which application server you are using so others on this forum can recommend something. For example, we use IBM WebSphere Application Server, version 7.0.0.7, Network Deployment Edition. So in our case, we can set up replication of beans/objects between cluster nodes.
Q4: I believe the answer is yes if you wish to remain connected to the same cluster node for each subsequent transaction of an end user. (At least from the standpoint of coming in through an HTTP server). I'm guessing if you do not have session affinity turned on, then you could switch to a different cluster node and it may be trickier to locate your data (especially if you do not replicate it between clusters). It can be done, it just might be a performance hit. So I'm guessing the answer here is "yes".
关于你的问题:
我通过序列化/反序列化它们的句柄来存储对有状态会话 Bean (SFSB) 的引用。它在集群环境中运行良好,但是当集群中的节点在 SFSB 调用之间启动/停止时,句柄会在此类调用后被修改。一旦句柄在 SFSB 调用后被修改,就需要再次序列化它以供进一步使用。
我认为您可以考虑我的序列化/反序列化 SFSB 句柄的方法。
Regarding your question:
I store references to Stateful Session Beans (SFSB) by serializing/deserializing their Handles. It runs fine in a clustered environment but when nodes are started/stopped in the cluster between your SFSB invocations then Handles are modified after such invocations. And once a Handle gets modified after a SFSB invocation then it needs to be serialized once again for a further use.
I think that you may consider my approach of serializing/deserializing SFSB Handles.