tencent cloud

Feedback

SDK for C++

Last updated: 2022-05-24 14:56:42

    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
      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
      vhost Vhost name in the format of “cluster ID + | + vhost name”. img
    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
      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
      vhost Vhost name in the format of “cluster ID + | + vhost name”. img
    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