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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access