views:

34

answers:

2

Hello SO!

Even though I've been in Java SE for quite some time now, I started EE & web w/ Java only about a month ago, so pardon if the question seems a bit noobish...

So here's the situation: I'm trying to write a JS based multi-player game with real-time interaction (let's say chess in this example, though it really doesn't matter what particular game it is, could be tennis or w/ever). The clients would interact with the server through JS calls, sending the move etc. Now, while I could just receive the move from one client & pass it straight on to the other player, not maintaining the game state on the server would mean putting a huge sign out saying "user JS scripts welcome" (and that's out of experience -- "hacked" a crapload of that kind myself). This brings me to my problem -- how do I share a stateful object between several sessions? One idea that came to mind was a singleton storing a Hashmap of stateful beans & then each session could retrieve the bean by it's hash, but I've no idea how right that is (and it seems rather complex for a fairly common thing like that). Tieing it to application scope seems overkill as well...

P.S. I do understand that the object would need concurrency managing etc, I just can't seem to put my finger on how to get it shared...

EDIT: I'm sorry I didn't mention it before -- using Glassfish, EE6.

A: 

You have a business process scenario which is defined according to Seam framework documentation as follows

The business process spans multiple interactions with multiple users, so this state is shared between multiple users, but in a well-defined manner. The current task determines the current business process instance, and the lifecycle of the business process is defined externally using a process definition language, so there are no special annotations for business process demarcation.

Here you can see a Seam business process management Tutorial

Notice Seam uses JBoss BPM behind the scenes to handle its business process context. If you just want to use plain JBoss BPM capabilities, you can see here how to integrate with JBoss

See also JBoss BPM User guide

Arthur Ronald F D Garcia
TC1
@TC1 I see. One approach could be store your Stateful object in the application context (because session context is *per user*) with an unique id assigned to identity the game
Arthur Ronald F D Garcia
A: 

Solved. Shared it via ServletContext, which I initially thought wouldn't work 'cause FacesServlet is a separate one, thought it has smthn like a different container either.

TC1