BindQueue Method

Binds a queue to an exchange.

Syntax

public void bindQueue(String channelName, String queueName, String exchangeName, String routingKey, boolean noWait);

Remarks

This method is used to bind the queue named QueueName to the exchange named ExchangeName. Exchanges use bindings to determine which queues to route messages to.

Multiple bindings between the same queue and exchange with different RoutingKeys and/or arguments are allowed; requests that would create a duplicate binding are ignored. No queue will ever receive duplicate copies of any message, regardless of what bindings are present on the server.

Note that all AMQP 0.9.1 servers automatically bind all queues to their default exchange (which is always a direct exchange with no name) using each queue's name as the binding's routing key. This makes it easy to send a message to a specific queue without having to declare bindings; just call PublishMessage, pass empty string for ExchangeName, and the name of the desired queue for RoutingKey.

ChannelName controls what channel the class will send the request over. While any channel can technically be used, keep in mind that the server will close it if a channel error occurs. For this reason, it is good practice to make requests such as this one using a channel that is not involved in message publishing or consumption.

QueueName must be a non-empty string consisting only of letters, digits, hyphens, underscores, periods, and colons. It must be no longer than 255 characters.

The server's default exchange may be specified by passing empty string for ExchangeName. Otherwise, ExchangeName must be a non-empty string consisting only of letters, digits, hyphens, underscores, periods, and colons. It must be no longer than 255 characters.

The RoutingKey parameter specifies the binding's routing key. Exchanges that use routing-key-based logic make some sort of comparison between the routing keys of incoming messages and this value to decide which messages should be forwarded to the specified queue. Examples of exchange types which use routing keys include:

  • direct exchanges, which compare (for equality) the routing keys of incoming messages to the routing keys of each queue bound to them.
  • topic exchanges, which match the routing keys of incoming messages against the routing pattern of each queue bound to them.

Not all exchange types make use of routing keys, in which case empty string can be passed for the RoutingKey parameter. Examples of exchange types which don't use routing keys include:

  • fanout exchanges simply forward incoming messages to all queues bound to them, unconditionally.
  • header exchanges only forward messages that include certain headers. When binding a queue to a header exchange, add items to the Arguments collection to describe the headers that eligible messages must have, and whether they must have any or all of those headers.

Note that the format of the RoutingKey parameter and/or the content of the Arguments collection may differ slightly between server implementations. Refer to your server's documentation to determine what it expects to receive for each exchange type that it supports.

The NoWait parameter, if True, will cause the server to execute the request asynchronously. For asynchronous request handling, the server only sends back a response in case of an error.

An exception is thrown if no channel with the given ChannelName exists, or if the server returns an error because:

  • No queue with the given QueueName exists.
  • No exchange with the given ExchangeName exists.

Note that in AMQP, server errors are grouped into "connection errors" and "channel errors", and both are fatal. That is, if the server returns a channel error, it will then close the channel which caused the error; and if it returns a connection error, it will then close the connection. The AMQPClassic class's Error Codes page includes AMQP's various connection and channel errors.

Binding a Queue to an Exchange

// Bind a queue to an exchange. Messages will only be delivered to the queue if their routing key is "MyRoutingKey".
amqpc1.BindQueue("channel", "MyQueue", "MyExchange", "MyRoutingKey", false);

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks IoT 2020 Java Edition - Version 20.0 [Build 8265]