How to cancel AWS Kinesis shard subscription? – AWS CPP SDK

  amazon-kinesis, amazon-web-services, aws-sdk, c++

I am using the enhanced fan-out feature of Kinesis Stream. In order to do this, I have to subscribe to shard by consumer ARN. This subscription is valid for 5 minutes and after that, we have to subscribe again (according to documentation).

All is good so far, but the thing is I want to cancel this subscription whenever I want before 5 minutes, and there is no method for that, neither any documentation.

Note: SubscribeToShard is a blocking funcion.

Kinesis::KinesisClient client_(credentials, config);

Kinesis::Model::SubscribeToShardRequest sub_shared_req;
sub_shared_req.SetConsumerARN(consumer_arn_);
sub_shared_req.SetShardId(shard_id_);
sub_shared_req.SetStartingPosition(
        Aws::Kinesis::Model::StartingPosition().WithType(Aws::Kinesis::Model::ShardIteratorType::LATEST));

Aws::Kinesis::Model::SubscribeToShardHandler handler;
handler.SetSubscribeToShardEventCallback([this](const Aws::Kinesis::Model::SubscribeToShardEvent &event) {
    const auto &records = event.GetRecords();
    for (const auto &rec: records) {
        std::string message((char *) rec.GetData().GetUnderlyingData(), rec.GetData().GetLength());
        handle_record(std::move(message));
    }
});

sub_shared_req.SetEventStreamHandler(handler);

client_.SubscribeToShard(sub_shared_req);  // this is blocking call

PS: There is Java documentation on how to do it but I don’t think it’s the same as CPP-SDK.
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-kinesis-stream.html#use-a-custom-subscriber

Source: Windows Questions C++

LEAVE A COMMENT