简体   繁体   中英

EJB3 client callback?

For learning purposes as I'm new to EJB3, I'm implementing the board game Risk as a client-server app with EJB3 on the server side. The intention is that multiple users would log in and play a game together. For the moment, I'm thinking of using JSF for the frontend.

There are multiple instances in which an action by some client 'A' needs to result in some other client 'B' being notified. Eg if A attacks a country owned by B, B needs to know about it, eg to consider how many dice to roll and if they want to play a card.

I have a @Stateless session bean caled PlayBean with methods called attack() and defend() , exposed either by a @WebService or @Remote interface. If attack() is called by player A, I need to notify the attacked player B so that B can decide what parameters to call defend() with. In concrete terms, this amounts to the managed bean for A making a call to the PlayBean, and then PlayBean making a call back to B's managed bean. How do I perform this session bean to managed bean callback? (Once I can 'get to' the managed bean, from there I can look at JSF's server-push features to go right up to the user)

My instinct is to have the page beans provide a callback object as a parameter to the session bean... but does this work this way with EJB3?

@Stateless
public class PlayBean implements Play {
    @Override
    public void attack(OccupiedTerritory from, OccupiedTerritory to, int battalions, Set<Leader> leader) {
    int attackDice = battalions;
    Die die = new Die(6); // TODO read number of sides from cfg
    Iterator<Leader> leaderIt = leader.iterator();
    int[] roll = new int[attackDice];
    for (int i = 0; i < attackDice; i++) {
        int bonus = leaderIt.hasNext() ? leaderIt.next().getAttackBonus() : 0;
        roll[i] = die.roll(bonus);
    }
    Arrays.sort(roll);
    Player victim = to.getOccupier(); // ...but how do I nudge this player (NOT the player that invoked this method) to act, i.e. to choose how many defence dice to use? I need to get a reference to "victim"'s JSF managed bean.
}

I think a lot of this depends on information you haven't supplied.

For example, if the client is a thick client (Java or otherwise), you could set up a messaging approach using JMS in the server. The client, when connecting, simply sets up a messaging client which listens for events. The server could initiate events using messages. If Java is used in the client, JMS could also be used in the client. But using Messaging leaves the client and the server components to be built using different languages if required.

If the client is a thin, web application, then you're really looking at what the web framework provides (and less EJB... although JMS could still be used in the server to trigger the events). JSF Frameworks like ICEfaces and Primefaces support server-push, which allows the server to push updated to the client. I think ZK also supports this, so it's not limited to JSF.

Either way, I think the answer to this isn't solely described by EJBs. Hope this helps.

May be you should rethink your view of a fully fledged EAR and the roles of the different components. If you are playing with EJBs, you should look at the application as a transaction system. A lot of problems can be transformed to transaction problems if you are willing to do so.

In your case you should look at the JSF managed bean as a controller extension, or backing bean for a page. There is no logic or state in there except for displaying information or tunnel actions to EJBs. This said there should be no need to calling them back from an EJB.

So where is your data model? (ie Who attacks whom.) For this example it might be sufficient to have a Singleton EJB holding this data. You can use your PlayBean then to modify the data of the Singleton Data Model Bean.

Now it's time to find a solution for your final question. How to inform a player about a change in the data. (That's the "you have been attacked" thing). If you use any html front end (like JSF) the problem is always the same. There is no such thing like real push for web pages. I see two choices:

  1. Polling (like meta-refresh) or manual refresh by the user

  2. Using something like Comet 2.

So your question is not about EJBs or JAF managed beans, it's about HTML Frontend push notification ;-)

Just my 2 cents..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM