0

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 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

2
  • Hi, Why are you trying to do it this way? Is it absolutely necessary that they both share the same session object? A session is meant to be for a single user afaik.
    – arg20
    Jun 14, 2011 at 17:29
  • You could, however, have each session object point to a persistant "Game" object. Although, I completely lack your requirements so this suggestion is only a guess.
    – arg20
    Jun 14, 2011 at 17:31

2 Answers 2

0

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.

0
0

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).

2
  • For a large scale site a clustered caching solution is a good idea, but this seems to be a student. Perhaps a simple hashmap (with some concurrency control) would be more appropriate? Apr 4, 2011 at 1:56
  • Hard to say, but you're right. If traffic is minimal, then some sort of a shared hashmap makes sense.
    – squawknull
    Apr 4, 2011 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.