tencent cloud

Feedback

SDK for C++

Last updated: 2024-01-03 11:45:32

    Overview

    This document describes how to use open-source SDK to send and receive messages by using the SDK for C++ as an example and helps you better understand the message sending and receiving processes.

    Prerequisites

    Directions

    Step 1. Prepare the environment

    1. Install a C and C++ client library. This document uses AMQP-CPP as an example.
    2. Import the dynamic library and header file.

    Step 2. Produce messages

    1. Establish a connection.
    auto evbase = event_base_new();
    LibEventHandlerMyError hndl(evbase);
    
    // Establish a connection
    AMQP::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 channel
    AMQP::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. ![img](https://qcloudimg.tencent-cloud.cn/raw/cefeaa6929d8c0b5565739924a70d499.png)
    port
    Port number in the cluster access address.
    name-server
    Role name, which can be copied on the Role Management page.
    password
    Role token, which can be copied in the Token column on the Role Management page. ![img](https://qcloudimg.tencent-cloud.cn/raw/5334afbe5e78680da2f7755c26d63852.png)
    vhost
    Vhost name in the format of “cluster ID + | + vhost name”. ![img](https://qcloudimg.tencent-cloud.cn/raw/3838fa2613ceafce1585a9502962ddb0.png)
    2. Send messages.
    // Declare an exchange
    channel.declareExchange(exchange_name, AMQP::ExchangeType::direct);
    
    // Send messages to the exchange
    channel.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.

    Step 3. Consume messages

    1. Establish a connection.
    ConnHandler handler;
    // Establish a connection
    AMQP::TcpConnection connection(handler, AMQP::Address(host, part, AMQP::Login(username, password), vhost));
    // Create a channel
    AMQP::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. ![img](https://qcloudimg.tencent-cloud.cn/raw/580f962992ff34fbafa7a647d010c3f0.png)
    port
    Port number in the cluster access address.
    name-server
    Role name, which can be copied on the Role Management page.
    password
    Role token, which can be copied in the Token column on the Role Management page. ![img](https://qcloudimg.tencent-cloud.cn/raw/894511a1bb5f3a4dd895ee6cc78735a3.png)
    vhost
    Vhost name in the format of “cluster ID + | + vhost name”. ![img](https://qcloudimg.tencent-cloud.cn/raw/4b6f6629c0f12e3ce09ad9949c5a11d7.png)
    2. Declare an exchange and a message queue and bind them.
    // Declare an exchange
    channel.declareExchange(exchange_name, AMQP::ExchangeType::direct);
    // Declare a message queue
    channel.declareQueue(queue_name, AMQP::durable);
    // Bind the message queue to the exchange
    channel.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.
    3. Subscribe to messages
    // Subscribe to the message
    channel.consume(queue_name)
    .onReceived
    (
    [&channel](const AMQP::Message &msg, uint64_t tag, bool redelivered) {
    // Process the message
    std::cout << "Received: " << msg.body() << std::endl;
    // Acknowledge the message, or reject it if the consumption failed
    channel.ack(tag);
    }
    );
    handler.Start();
    
    Parameter
    Description
    queue_name
    Message queue name, which can be obtained from the queue list in the console.
    Note:
    For a complete sample or more information, see Demo, AMQP-CPP , or AMQP-CPP Examples.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support