-
A
Connection
represents a real TCP connection to the message broker, whereas aChannel
is a virtual connection (AMQP connection) inside it. This way you can use as many (virtual) connections as you want inside your application without overloading the broker with TCP connections. -
You can use one
Channel
for everything. However, if you have multiple threads, it’s suggested to use a differentChannel
for each thread.Channel thread-safety in Java Client API Guide:
Channel instances are safe for use by multiple threads. Requests into
a Channel are serialized, with only one thread being able to run a
command on the Channel at a time. Even so, applications should prefer
using a Channel per thread instead of sharing the same Channel across
multiple threads.There is no direct relation between
Channel
andQueue
. AChannel
is used to send AMQP commands to the broker. This can be the creation of a queue or similar, but these concepts are not tied together. -
Each
Consumer
runs in its own thread allocated from the consumer thread pool. If multiple Consumers are subscribed to the same Queue, the broker uses round-robin to distribute the messages between them equally. See Tutorial two: “Work Queues”.It is also possible to attach the same
Consumer
to multiple Queues.
You can understand Consumers as callbacks. These are called everytime a message arrives on a Queue the Consumer is bound to. For the case of the Java Client, each Consumers has a methodhandleDelivery(...)
, which represents the callback method. What you typically do is, subclassDefaultConsumer
and overridehandleDelivery(...)
. Note: If you attach the same Consumer instance to multiple queues, this method will be called by different threads. So take care of synchronization if necessary.