auto evbase = event_base_new();LibEventHandlerMyError hndl(evbase);// Establish a connectionAMQP::TcpConnection connection(&hndl, AMQP::Address("amqp://admin:eyJrZXlJZC...@amqp-xxx.rabbitmq.ap-sh.public.tencenttdmq.com:5672/amqp-xxx|vhost-cpp"));// The service address is in the format of “amqp://username:password@host:port/vhost”.// Create a channelAMQP::TcpChannel channel(&connection);channel.onError([&evbase](const char *message) {std::cout << "Channel error: " << message << std::endl;event_base_loopbreak(evbase);});
Parameter | Description |
String | Cluster access address, which can be obtained in the console by clicking Access Address in the “Operation” column on the Cluster page. |
port | Port number in the cluster access address. |
name-server | |
password | |
vhost | Vhost name in the format of “cluster ID + | + vhost name”. |
// Declare an exchangechannel.declareExchange(exchange_name, AMQP::ExchangeType::direct);// Send messages to the exchangechannel.publish(exchange_name, routing_key, "Hello client this is a info message");event_base_dispatch(evbase);event_base_free(evbase);
Parameter | Description |
exchange_name | Exchange name, which can be obtained from the exchange list in the console. |
routing_key | The routing key supported by the message queue. |
ConnHandler handler;// Establish a connectionAMQP::TcpConnection connection(handler, AMQP::Address(host, part, AMQP::Login(username, password), vhost));// Create a channelAMQP::TcpChannel channel(&connection);channel.onError([&handler](const char *message) {std::cout << "Channel error: " << message << std::endl;handler.Stop();});
Parameter | Description |
String | Cluster access address, which can be obtained in the console by clicking Access Address in the “Operation” column on the Cluster page. |
port | Port number in the cluster access address. |
name-server | |
password | |
vhost | Vhost name in the format of “cluster ID + | + vhost name”. |
// Declare an exchangechannel.declareExchange(exchange_name, AMQP::ExchangeType::direct);// Declare a message queuechannel.declareQueue(queue_name, AMQP::durable);// Bind the message queue to the exchangechannel.bindQueue(exchange_name, queue_name, routing_key);
Parameter | Description |
exchange_name | Exchange name, which can be obtained from the exchange list in the console. |
queue_name | Message queue name, which can be obtained from the queue list in the console. |
routing_key | The routing key supported by the message queue. |
// Subscribe to the messagechannel.consume(queue_name).onReceived([&channel](const AMQP::Message &msg, uint64_t tag, bool redelivered) {// Process the messagestd::cout << "Received: " << msg.body() << std::endl;// Acknowledge the message, or reject it if the consumption failedchannel.ack(tag);});handler.Start();
Parameter | Description |
queue_name | Message queue name, which can be obtained from the queue list in the console. |
Was this page helpful?