You need to enable JavaScript to run this app.
导航
事务消息
最近更新时间:2025.05.09 10:28:39首次发布时间:2025.05.09 10:28:39
我的收藏
有用
有用
无用
无用

本文提供使用 C++ SDK 收发事务消息的示例代码供您参考。

发送事务消息

发送事务消息需要在控制台申请事务类型的topic,在发送half消息后,用户可以根据自身业务逻辑对消息选择:

  • TransactionImpl.COMMIT:提交事务。事务提交之后,之前暂存的消息对消费者可见。
  • TransactionImpl.ROLLBACK:回滚事务。事务回滚,之前暂存的消息会被标记为已经回滚。

另外事务消息需要自定义checker,以便于服收到消息确认后进行回查

#include <algorithm>
#include <atomic>
#include <iostream>
#include <random>
#include <string>
#include <system_error>

#include "gflags/gflags.h"
#include "rocketmq/Message.h"
#include "rocketmq/Producer.h"

using namespace ROCKETMQ_NAMESPACE;

const std::string& alphaNumeric() {
  static std::string alpha_numeric("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  return alpha_numeric;
}

std::string randomString(std::string::size_type len) {
  std::string result;
  result.reserve(len);
  std::random_device rd;
  std::mt19937 generator(rd());
  std::string source(alphaNumeric());
  std::string::size_type generated = 0;
  while (generated < len) {
    std::shuffle(source.begin(), source.end(), generator);
    std::string::size_type delta = std::min({len - generated, source.length()});
    result.append(source.substr(0, delta));
    generated += delta;
  }
  return result;
}

/*
     * 设置为您从火山引擎消息队列 RocketMQ版控制台获取的接入点信息,类似“http://{INSTANCE_ID}.rocketmq.ivolces.com.com:8080”。
     * 设置RocketMQ实例的AccessKey ID和AccessKey Secret。
    */
DEFINE_string(topic, "xxxx", "Topic to which messages are published");
DEFINE_string(access_point, "rocketmq-xxxx:8080", "Service access URL, provided by your service provider");
DEFINE_int32(message_body_size, 4096, "Message body size");
DEFINE_uint32(total, 256, "Number of sample messages to publish");
DEFINE_string(access_key, "xxxxx", "Your access key ID");
DEFINE_string(access_secret, "xxxx", "Your access secret");
DEFINE_bool(tls, false, "Use HTTP2 with TLS/SSL"); //当前火山不支持tsl/ssl模式,需要填写false

int main(int argc, char* argv[]) {
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  auto& logger = getLogger();
  logger.setConsoleLevel(Level::Info);
  logger.setLevel(Level::Info);
  logger.init();

  auto checker = [](const Message& message) -> TransactionState {
    std::cout << "Recovery orphan transactional message[topic=" << message.topic() << ", MsgId=" << message.id()
              << ", txn-id=" << message.extension().transaction_id << std::endl;
    return TransactionState::COMMIT;
  };

  CredentialsProviderPtr credentials_provider;
  if (!FLAGS_access_key.empty() && !FLAGS_access_secret.empty()) {
    credentials_provider = std::make_shared<StaticCredentialsProvider>(FLAGS_access_key, FLAGS_access_secret);
  }

  // In most case, you don't need to create too many producers, singletion pattern is recommended.
  auto producer = Producer::newBuilder()
                      .withConfiguration(Configuration::newBuilder()
                                             .withEndpoints(FLAGS_access_point)
                                             .withCredentialsProvider(credentials_provider)
                                             .withSsl(FLAGS_tls)
                                             .build())
                      .withTopics({FLAGS_topic})
                      .withTransactionChecker(checker)
                      .build();

  std::atomic_bool stopped;
  std::atomic_long count(0);

  auto stats_lambda = [&] {
    while (!stopped.load(std::memory_order_relaxed)) {
      long cnt = count.load(std::memory_order_relaxed);
      while (!count.compare_exchange_weak(cnt, 0)) {
        cnt = count.load(std::memory_order_relaxed);
      }
      std::this_thread::sleep_for(std::chrono::seconds(1));
      std::cout << "QPS: " << cnt << std::endl;
    }
  };

  std::thread stats_thread(stats_lambda);

  std::string body = randomString(FLAGS_message_body_size);

  try {
    auto message = Message::newBuilder().withTopic(FLAGS_topic).withTag("TagA").withBody(body).build();
    auto transaction = producer.beginTransaction();
    std::error_code ec;

    producer.send(std::move(message), ec, *transaction);

    if (!ec) {
      if (!transaction->commit()) {
        std::cerr << "Failed to commit message" << std::endl;
      }
    } else {
      std::cerr << "Failed to send transactional message to topic: " << FLAGS_topic << std::endl;
    }
  } catch (...) {
    std::cerr << "Ah...No!!!" << std::endl;
  }
  stopped.store(true, std::memory_order_relaxed);
  if (stats_thread.joinable()) {
    stats_thread.join();
  }

  std::cout << "Wait 5 minutes for potential unresolved transactional message callback" << std::endl;
  std::this_thread::sleep_for(std::chrono::minutes(5));

  return EXIT_SUCCESS;
}   

订阅事务消息

事务消息的订阅方式与普通消息一致,示例代码如下所示。

#include <chrono>
#include <iostream>
#include <thread>

#include "gflags/gflags.h"
#include "rocketmq/Logger.h"
#include "rocketmq/PushConsumer.h"

using namespace ROCKETMQ_NAMESPACE;

/*
     * 设置为您从火山引擎消息队列 RocketMQ版控制台获取的接入点信息,类似“http://{INSTANCE_ID}.rocketmq.ivolces.com.com:8080”。
     * 设置RocketMQ实例的AccessKey ID和AccessKey Secret。
    */
DEFINE_string(topic, "xxxx", "Topic to which messages are published");
DEFINE_string(group, "xxxx", "GroupId, created through your instance management console");
DEFINE_string(access_point, "rocketmq-xxxx:8080", "Service access URL, provided by your service provider");
DEFINE_int32(message_body_size, 4096, "Message body size");
DEFINE_uint32(total, 256, "Number of sample messages to publish");
DEFINE_string(access_key, "xxxxx", "Your access key ID");
DEFINE_string(access_secret, "xxxx", "Your access secret");
DEFINE_bool(tls, false, "Use HTTP2 with TLS/SSL"); //当前火山不支持tsl/ssl模式,需要填写false

int main(int argc, char* argv[]) {
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  auto& logger = getLogger();
  logger.setConsoleLevel(Level::Info);
  logger.setLevel(Level::Info);
  logger.init();

  std::string tag = "*";

  auto listener = [](const Message& message) {
    std::cout << "Received a message[topic=" << message.topic() << ", MsgId=" << message.id() << "]" << std::endl;
    return ConsumeResult::SUCCESS;
  };

  CredentialsProviderPtr credentials_provider;
  if (!FLAGS_access_key.empty() && !FLAGS_access_secret.empty()) {
    credentials_provider = std::make_shared<StaticCredentialsProvider>(FLAGS_access_key, FLAGS_access_secret);
  }

  // In most case, you don't need to create too many consumers, singletion pattern is recommended.
  auto push_consumer = PushConsumer::newBuilder()
                           .withGroup(FLAGS_group)
                           .withConfiguration(Configuration::newBuilder()
                                                  .withEndpoints(FLAGS_access_point)
                                                  .withRequestTimeout(std::chrono::seconds(3))
                                                  .withCredentialsProvider(credentials_provider)
                                                  .withSsl(FLAGS_tls)
                                                  .build())
                           .withConsumeThreads(4)
                           .withListener(listener)
                           .subscribe(FLAGS_topic, tag)
                           .build();

  std::this_thread::sleep_for(std::chrono::minutes(30));

  return EXIT_SUCCESS;
}