开发者

Java EE use a stateful session bean for 2 user sessions?

开发者 https://www.devze.com 2023-02-22 07:48 出处:网络
I\'m trying to build a Java web based game. I was wondering how one would use an instance of a stateful session bean for two users. My game will be a two-player game, and my professor said the easiest

I'm trying to build a Java web based game. I was wondering how one would use an instance of a stateful session bean for two users. My game will be a two-player game, and my professor said the easiest way 开发者_StackOverflow社区to accomplish this would be to give both players' HttpSession's a reference to the same stateful session bean, but after multiple google searches, I can't figure out how to find the same bean for two people instead of creating a new one.

Thanks for any input, -Duffy


I guess you could store the bean reference in application scope (e.g. getServletContext().setAttribute(...)) and the two Http sessions could use it by a common key. However, if it's a multiple server cluster, the application server must support attribute propagation across different servers.


You must somehow share the reference to the bean between the two HttpSessions. That is easier said than done, especially if you are running on multiple servers. If you were running on one server, you could theoretically put the reference into a static hashmap.

However, for what it sounds like you are trying to do, I would probably recommend using a shared clustered caching solution (such as memcached, JBoss Infinispan, Terracotta, etc.). This seems like it's getting out of the intended use case for stateful session beans, so you may be heading toward some dangerous territory, and might actually break the spec (I'm not sure about this). Try to use a technology that was intended for this sort of shared state across a cluster (such as the clustered caching solutions suggested above).

0

精彩评论

暂无评论...
验证码 换一张
取 消