1

I’m writing a server/client game, a typical scenario looks like this: one client (clientA) send a message to the server, there is a MessageDrivenBean in server to handle such messages. After the MDB finished its job, it sends the result message back to another client (clientB).

In my opinion I only need two queues for such communication, one for input the other for output. Creating new queue for each connection is not a good idea, right? The Input queue is relative clear, if more clients are sending message at the same time, the messages are just waiting in the queue, while there are more MDB instances in server, that should not a big performance issue.

But on the other side I am not quite clear about the output queue, should I use a topic instead of a queue? Every client is listening the output queue, one of them gets the new message and checks the property to determine if the message is to it, if not, it rollback the transaction, the message goes back to queue and be ready for other client … It should work but must be very slow. If I use topic instead, every client gets a copy of the message, if it’s not to it, just ignores the message. It should be better, right?

I’m new about message system. Is there any suggestion about my implementation? Thanks!

cn1h
  • 1,188
  • 4
  • 16
  • 24

2 Answers2

1

To begin with, choosing JMS as a gaming platform is, well, unusual — businesses use JMS brokers for delivery reliability and transaction support. Do you really need this heavy lifiting in a game? Shouldn't you resort to your own HTTP-based protocol, for example?

That said, two queues are a standard pattern for point-to-point communication. Creating a queue for a new connection is definitely not OK — message-driven beans are attached to queues at deployment time, so you won't be able to respond to queue creation events. Besides, queues are not meant to be created and destroyed in short cycles, they're rather designed to be long-living entities. If you need to deliver a message to one precise client, have the client listen on the server response queue with a message selector set to filter only the messages intended for this client (see javax.jms.Message API).

With topics it's exactly as you noted — each connected client will get a copy of the message — so again, it's not a good pattern to send to n clients a message that has to be discarded by n-1 clients.

MaDa
  • 10,511
  • 9
  • 46
  • 84
  • thanks for the answer. now i'm more clear about JMS. btw. I use JMS because the frontend is wroten with flash and I use the blazeds message system to communicate with java backend. http is absolutly an option, but the blazeds message system has a server data push feature, that means instead of querying server response every second, the client gets the response immediatly when there is one. So i decided to use jms. – cn1h Oct 24 '11 at 13:50
  • @cn1h With server pushing the data to the client it makes sense. It's good that you have such technology available, and thanks for explanation. – MaDa Oct 24 '11 at 14:05
1

MaDa;

You could stick one output queue (or topic) and simply tag the message with a header that identifies the intended client. Then, clients can listen on the queue/topic using a selector. Hopefully your JMS implementation has efficient server-side listener evaluation.

Nicholas
  • 15,916
  • 4
  • 42
  • 66