【问题标题】:Google Cloud Pubsub Async Streaming API in C++C++ 中的 Google Cloud Pubsub Async Streaming API
【发布时间】:2020-10-11 14:33:36
【问题描述】:

我正在尝试查找有关通过异步 grpc 使用 Pubsub Streaming API 的文档,但找不到任何文档。

我有这个简单的代码来读取来自主题的所有消息:

  auto creds = grpc::GoogleDefaultCredentials();
  auto stub = std::make_unique<Subscriber::Stub>(
        grpc::CreateChannel("pubsub.googleapis.com", creds));

  ClientContext context;
  std::unique_ptr<ClientReaderWriter<
      StreamingPullRequest, StreamingPullResponse>> stream(
          stub->StreamingPull(&context));

  StreamingPullRequest request;
  request.set_subscription(
      "projects/test/subscriptions/test-subscription");
  request.set_stream_ack_deadline_seconds(10);
  stream->Write(request);

  StreamingPullResponse response;
  while (stream->Read(&response)) {
    StreamingPullRequest ack_request;
    for (const auto &message : response.received_messages()) {
      ack_request.add_ack_ids(message.ack_id());
    }
    stream->Write(ack_request);
  }

基本上我也想做同样的事情,但使用异步 rpc 调用,所以这段代码在回调内部调用:

    StreamingPullRequest ack_request;
    for (const auto &message : response.received_messages()) {
      ack_request.add_ack_ids(message.ack_id());
    }
    stream->Write(ack_request);

您能帮我举一个简单的异步代码示例吗?

【问题讨论】:

    标签: c++ asynchronous google-cloud-platform grpc google-cloud-pubsub


    【解决方案1】:

    目前Google Cloud Platform C++Pub/Sub 库尚未由供应商正式宣布,也没有用于实现的 ETA,因此代码库作为概念证明存在,但仍不完整的文档,没有像样的使用示例。您可以关注this Github 线程,跟踪社区为进一步开发库所做的努力。

    据我所知,您正在寻找通过异步gRPC 调用执行StreamingPull 的任何见解,查看@Manuel Menzella 发布的answer 我建议您查看他的方法,该方法可以在概念上适合您的用例。

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2018-06-22
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 2021-06-05
      • 2020-05-25
      相关资源
      最近更新 更多