AlenEviLL
AlenEviLL

Reputation: 56

How to use message driven beans between two clients

I am new to EJB and JavaEE in general and I am currently working on a little project that basically implements a game where two players PlayerA and PlayerB can play via web browser.

I currently have a set up for several JSP pages like this: login -> playerprofile -> acceptGame -> playGame -> outcome

There are two login pages for player A and B they link to same playerprofile page that shows the username and score for the chosen player, info is stored in the database. In that profile page players can click on link that sends them to acceptGame page where player has to click on accept button and if both players have accepted they can proceed to playGame. It makes sense in this scenario to use message beans since one player may have accepted whilst another has not, so the player who has accepted should wait for the other player to accept.

Unfortunately, every tutorial for message beans I have come across online just passes some TextMessage object and it gets printed on the server, but there is no example to scenarios similar to this.

Upvotes: 0

Views: 137

Answers (1)

ytg
ytg

Reputation: 1857

Message-driven beans are used to process asynchronous calls, when you as a caller don't want to wait for the result of the processing. In your case, I think using a message-driven bean is not optimal.

You can implement your logic with it too: playerA accept the game offer, the server sends a message to a MDB, playerA proceeds to a waiting state, the MDB stores that playerA accepted the game offer, playerB also accepts the game offer, etc., and when they're both in the waiting state and they both determine that the other has the acceptance stored, they proceed to playGame.

This seems overly complicated to me, the message-driven bean doesn't improve this process in any way. I think the same results could be accomplished with a stateful bean more comfortably, because the MDB can only eliminate wait-times from the technical point of view (eg. I want send out an e-mail, but I don't care when it is actually sent as long as it is sent), but not from the business point of view. If your business logic requires you to wait, then you'll have to wait.

Upvotes: 0

Related Questions