以下基于SimpleAmqpClient源码实现,源码路径:https://github.com/alanxz/SimpleAmqpClient
创建消费者步骤:
步骤1:创建channel,类似于创建一个tcp连接
static ptr_t Create(const std::string &host = “127.0.0.1”, int port = 5672,
const std::string &username = “guest”,
const std::string &password = “guest”,
const std::string &vhost = “/”,
int frame_max = 131072)
参数说明:
host:The hostname or IP address of the AMQP broker
port:The port to connect to the AMQP broker on
username:The username used to authenticate with the AMQP broker
password:The password corresponding to the username used to authenticate with the AMQP brokervhost:The virtual host on the AMQP we should connect to
frame_max:Request that the server limit the maximum size of any frame to this value
其中,
broker:就是接收和分发消息的应用,也就是说RabbitMQ Server就是Message Broker。
vhost:虚拟主机,一个Broker里可以开有多个VirtualHost,它的作用是用作不同用户的权限分离。
步骤1示例:
AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Create(host, port, username, password, vhost, frame_max);
步骤2:在步骤1channel上声明一个queue,包括设置queue的属性
std::string DeclareQueue(const std::string &queue_name,
bool passive = false,
bool durable = false,
bool exclusive = true,
bool auto_delete = true);
参数说明:
queueName:the desired name of the queue. If this is a zero-length string the broker will generate a queue name and it will be returned as a result from this method
passive:Indicated how the broker should react if the queue does not exist. If passive is true and the queue does not exist the borker will respond with an error and not create the queue, the queue is created otherwise. Defaults to false(queue is created if it does not already exist)
durable:Indicates whether the exchange is durable - e.g., will it survive a broker restart. Defaults to false.
exclusive:Indicates that only client can use the queue. Defaults to true. An exclusive queue is deleted when the connection is closed.
auto_delete:the queue will be deleted after at least one exchange has been bound to it, then has been unbound.
步骤2示例(接步骤1):
ret_queueName = channel->DeclareQueue(queueName, passive, durable, exclusive, auto_delete);
ret_queueName为broker创建的queue name
步骤3:声明使用的交换机及交换机类型
void DeclareExchange( const std::string &exchange_name,
const std::string &exchange_type = Channel::EXCHANGE_TYPE_DIRECT,
bool passive = false,
bool durable = false,
bool auto_delete = false);
参数说明:
exchange_name:the name of the exchange
exchange_type:the type of exchange to be declared. Defaults to direct other types that could be used: fanout and topic
passive:同上
durable:同上
auto_delete: Indicates whether the exchange will automatically be removed when no queues are bound to it. Defaults to false.
步骤3示例(接步骤2):
channel->DeclareExchange(exchange_name, mqpClient::Channel::EXCHANGE_TYPE_FANOUT);
步骤4:绑定queue与交换机。交换机可以有很多个
void BindQueue(const std::string &queue_name,
const std::string &exchange_name,
const std::string &routing_key = “”);
参数说明:
queue_name: the name of the queue to bind
exchange_name: the name of the exchange to bind
routing_key: only messages sent to the exchange with this routing
步骤4示例(接步骤3):
channel->BindQueue(ret_queueName, exchange_name);
步骤5:以消费者身份订阅队列,准备消费消息
std::string BasicConsume(const std::string &queue,
const std::string &consumer_tag = “”,
bool no_local = true, bool no_ack = true,
bool exclusive = true,
boost::uint16_t message_prefetch_count = 1);
参数说明:
queue:the name of the queue to subscribe to
consumer_tag:the name of the consumer. This is used to do operations with a consumer
no_local:Defaults to trueno_ack: If true, ack’ing the message is automatically done when the message is delivered. Defaults to true (message does not have to be ack’ed)
exclusive: means only this consumer can access the queue. Defaults to true
message_prefetch_count: number of unacked messages the broker will deliver. Setting this to more than 1 will allow the broker to deliver messages while a current message is being processed for example. A value of 0 means no limit. This option is ignored if no_ack = true
步骤5示例(接步骤4):
std::string consumer = channel->BasicConsume(ret_queueName.c_str());
步骤6:接收消息
bool BasicConsumeMessage(const std::string &consumer_tag,
Envelope::ptr_t &envelope,
int timeout = -1);
参数说明:
consumer_tag:同上
envelope: the message object to save it to. Is ok to be an empty pointer
timeout:the timeout for the first part of the message to be delivered in ms
注意:
1、此函数需要在BasicConsume调用之后被调用
2、此函数出错会抛出MessageReturnedException异常,因此需要做异常捕获
步骤6示例(接步骤5):
AmqpClient::Envelope::ptr_t delivered;channel->BasicConsumeMessage(consumer, delivered, receiveTimeOut)
创建生成者步骤:
步骤1:创建channel,类似于创建一个tcp连接
参数同上,示例1:
AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Create(host, port, username, password, vhost, frame_max);
步骤2:声明交换机,消息将向此交换机发送
参数同上,示例2(接步骤1示例):
channel->DeclareExchange(exchangeName, AmqpClient::Channel::EXCHANGE_TYPE_FANOUT);
步骤3:发送消息
void BasicPublish(const std::string &exchange_name,
const std::string &routing_key,
const BasicMessage::ptr_t message,
bool mandatory = false,
bool immediate = false);
参数说明:
exchange_name:The name of the exchange to publish the message to.
routing_key:The routing key to publish with, this is used to route to the correct queue
message:the BasicMessage object to publish to the queue.
mandatory: requires the message to be delivered to a queue. A MessageReturnedException is thrown if the message cannot be routed to a queue. Defaults to false.
immediate:requires the message to be both routed to a queue, and immediately delivered via a consumer if the message is not routed, or a consumer cannot immediately deliver the message a MessageReturnedException is thrown. Defaults to false.
示例3(接步骤2示例):
channel->BasicPublish(exchangeName, routeKey, msg);
以上步骤只是fanout的一种实现方式,根据不同的需求,实现步骤也会有差异。可以通过源码中测试示例代码做进一步了解。
以上API参数说明可以进一步参考源码中的Channel.h文件