tencent cloud

Feedback

SDK for C++

Last updated: 2024-06-28 11:33:56

    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

    1. Prepare the environment.
    1.1 Install the Pulsar C++ client in the client environment as instructed in Pulsar C++ client.
    1.2 Introduce the files and dynamic libraries that are related to the Pulsar C++ client to the project.
    2. Create a client.
    // Client configuration information
    ClientConfiguration config;
    // Set the role token
    AuthenticationPtr auth = pulsar::AuthToken::createWithToken(AUTHENTICATION);
    config.setAuth(auth);
    // Create a client
    Client client(SERVICE_URL, config);
    
    Parameter
    Description
    SERVICE_URL
    Cluster access address, which can be viewed and copied on the Cluster page in the console.
    
    
    
    AUTHENTICATION
    Role token, which can be copied in the Token column on the Role Management page.
    
    
    
    3. Create a producer.
    // Producer configurations
    ProducerConfiguration producerConf;
    producerConf.setBlockIfQueueFull(true);
    producerConf.setSendTimeout(5000);
    // Producer
    Producer producer;
    // Create a producer
    Result result = client.createProducer(
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`
    "persistent://pulsar-xxx/sdk_cpp/topic1",
    producerConf,
    producer);
    if (result != ResultOk) {
    std::cout << "Error creating producer: " << result << std::endl;
    return -1;
    }
    
    Note:
    You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.
    4. Send the message.
    // Message content
    std::string content = "hello cpp client, this is a msg";
    // Create a message object
    Message msg = MessageBuilder().setContent(content)
    .setPartitionKey("mykey") // Business key
    .setProperty("x", "1") // Set message parameters
    .build();
    // Send the message
    Result result = producer.send(msg);
    if (result != ResultOk) {
    // The message failed to be sent
    std::cout << "The message " << content << " could not be sent, received code: " << result << std::endl;
    } else {
    // The message was successfully sent
    std::cout << "The message " << content << " sent successfully" << std::endl;
    }
    
    5. Create a consumer.
    // Consumer configuration information
    ConsumerConfiguration consumerConfiguration;
    consumerConfiguration.setSubscriptionInitialPosition(pulsar::InitialPositionEarliest);
    // Consumer
    Consumer consumer;
    // Subscribe to a topic
    Result result = client.subscribe(
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`
    "persistent://pulsar-xxx/sdk_cpp/topic1",
    // Subscription name
    "sub_topic1",
    consumerConfiguration,
    consumer);
    
    if (result != ResultOk) {
    std::cout << "Failed to subscribe: " << result << std::endl;
    return -1;
    }
    
    Note:
    You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic page in the console.
    img
    
    
    You need to enter the subscription name in the subscriptionName parameter, which can be viewed on the Consumption Management page.
    6. Consume the message.
    Message msg;
    // Obtain the message
    consumer.receive(msg);
    // Simulate the business processing logic
    std::cout << "Received: " << msg << " with payload '" << msg.getDataAsString() << "'" << std::endl;
    // Return `ack` as the acknowledgement
    consumer.acknowledge(msg);
    // Return `nack` if the consumption fails, and the message will be delivered again
    // consumer.negativeAcknowledge(msg);
    
    7. Log in to the TDMQ for Apache Pulsar console, click Topic > Topic Name to enter the Consumption Management page, and click the triangle below a subscription name to view the production and consumption records.
    
    img
    
    
    Note:
    The above is a brief introduction to the way of publishing and subscribing to messages. For more operations, see Demo or Pulsar C++ client.
    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