Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 4. Point-to-Point Messaging > Load Balancing Using Multiple Receivers

4.5. Load Balancing Using Multiple Receivers

A queue may have multiple receivers attached to it for the purpose of distributing the workload of message processing. The JMS specification states that this capability must be implemented by a JMS provider, although it does not define the rules for how the messages are distributed among consumers. A sender could use this feature to distribute messages to multiple instances of an application, each of which would provide its own receiver.

When multiple receivers are attached to a queue, each message in the queue is delivered to one receiver. The absolute order of messages cannot be guaranteed, since one receiver may process messages faster than another. From the receiver’s perspective, the messages it consumes should be in relative order; messages delivered to the queue earlier are consumed first. However, if a message needs to be redelivered due to an acknowledgment failure, it is possible that it could be delivered to another receiver. The other receiver may have already processed more recently delivered messages, which would place the redelivered message out of the original order.

If you would like to see multiple recipients in action, try starting two instances of the QLender class and one instance of the QBorrower class, each in a separate command window:

java ch04.p2p.QLender QueueCF LoanRequestQ
java ch04.p2p.QLender QueueCF LoanRequestQ
java ch04.p2p.QBorrower QueueCF LoanRequestQ LoanResponseQ         

Now, when entering a salary and loan amount in the command window, you will notice that the message is delivered to one or the other QLender application, but not both. The exact load balancing scheme will vary between JMS providers. Some may use a round-robin load balancing scheme, whereas others may use a first-available balancing scheme. You will need to consult your JMS provider documentation to determine the specific load balancing algorithm used.