C ++中的Google Cloud Pubsub异步流API [英] Google Cloud Pubsub Async Streaming API in C++

查看:112
本文介绍了C ++中的Google Cloud Pubsub异步流API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to find a documentation on using Pubsub Streaming API over async grpc but can't find any.

我有这个简单的代码,可以读取主题中的所有消息:

I have this simple code to read all messages from topic:

  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调用,因此该代码在回调内部被调用:

Basically I wanna do the same but with async rpc call so this code is called inside of callback:

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

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

Could you help me with a simple example of async code doing the same?

推荐答案

当前 Google Cloud Platform C ++ Github来跟踪社区为进一步图书馆开发所做的努力线程.

Currently Google Cloud Platform C++ Pub/Sub library has not yet being officially announced by the vendor and there are no ETAs for the implementation, thus the code base exists as a proof of concept and still not fully documented with no decent examples of usage. You can keep track the community efforts for further library development following this Github thread.

据我所知,您正在寻找执行 StreamingPull 的任何见解 gRPC 调用,查看

As far as I see you're looking for any of insights performing StreamingPull via async gRPC call, reviewing the answer being posted by @Manuel Menzella I would recommend to check out his approach that can be conceptually suitable for your use case.

这篇关于C ++中的Google Cloud Pubsub异步流API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆