azure-messaging-eventhubs
Loading...
Searching...
No Matches
consumer_client.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// cspell: word myeventhub
5
6#pragma once
7#include "models/consumer_client_models.hpp"
8#include "models/management_models.hpp"
9#include "partition_client.hpp"
10
11#include <azure/core/amqp.hpp>
12#include <azure/core/amqp/internal/connection.hpp>
13#include <azure/core/context.hpp>
14#include <azure/core/credentials/credentials.hpp>
15#include <azure/core/diagnostics/logger.hpp>
16#include <azure/core/http/policies/policy.hpp>
17#include <azure/core/internal/diagnostics/log.hpp>
18namespace Azure { namespace Messaging { namespace EventHubs {
19 namespace _detail {
20 class EventHubsPropertiesClient;
21 }
22
24 constexpr const char* DefaultConsumerGroup = "$Default";
25
29 {
32 std::string ApplicationID;
33
37 Azure::Core::Http::Policies::RetryOptions RetryOptions{};
38
40 std::string Name{};
41 };
42
52 class ConsumerClient final {
53 public:
55 ConsumerClient(ConsumerClient const& other) = delete;
56
58 ConsumerClient(ConsumerClient&& other) = delete;
59
61 ConsumerClient& operator=(ConsumerClient const& other) = delete;
62
65
67
72 std::string const& GetEventHubName() const { return m_eventHub; }
73
78 std::string const& GetConsumerGroup() const { return m_consumerGroup; }
79
85 {
87 details.ClientId = m_consumerClientOptions.ApplicationID;
88 details.ConsumerGroup = m_consumerGroup;
89 details.EventHubName = m_eventHub;
90 details.FullyQualifiedNamespace = m_fullyQualifiedNamespace;
91 return details;
92 }
97 Azure::Core::Http::Policies::RetryOptions const& GetRetryOptions() const
98 {
99 return m_consumerClientOptions.RetryOptions;
100 }
101
119 std::string const& connectionString,
120 std::string const& eventHub = {},
121 std::string const& consumerGroup = DefaultConsumerGroup,
122 ConsumerClientOptions const& options = {});
123
134 ConsumerClient(
135 std::string const& fullyQualifiedNamespace,
136 std::string const& eventHub,
137 std::shared_ptr<Azure::Core::Credentials::TokenCredential> credential,
138 std::string const& consumerGroup = DefaultConsumerGroup,
139 ConsumerClientOptions const& options = {});
140
147 PartitionClient CreatePartitionClient(
148 std::string const& partitionId,
149 PartitionClientOptions const& options = {},
150 Azure::Core::Context const& context = {});
151
158 void Close(Azure::Core::Context const& context);
159
164 Models::EventHubProperties GetEventHubProperties(Core::Context const& context = {});
165
173 Models::EventHubPartitionProperties GetPartitionProperties(
174 std::string const& partitionID,
175 Core::Context const& context = {});
176
177 private:
179 std::string m_connectionString;
180
182 std::string m_fullyQualifiedNamespace;
183
185 std::string m_eventHub;
186
188 std::string m_consumerGroup;
189
191 std::shared_ptr<Core::Credentials::TokenCredential> m_credential;
192
194 std::string m_hostUrl;
195
199 std::uint16_t m_targetPort = Azure::Core::Amqp::_internal::AmqpTlsPort;
200
202 std::mutex m_receiversLock;
203 std::map<std::string, Azure::Core::Amqp::_internal::MessageReceiver> m_receivers;
204
206 std::recursive_mutex m_sessionsLock;
207 std::map<std::string, Azure::Core::Amqp::_internal::Session> m_sessions;
208 std::map<std::string, Azure::Core::Amqp::_internal::Connection> m_connections;
209
210 // Client used for GetProperty operations.
211 std::mutex m_propertiesClientLock;
212 std::shared_ptr<_detail::EventHubsPropertiesClient> m_propertiesClient;
213
215 ConsumerClientOptions m_consumerClientOptions;
216
217 void EnsureConnection(std::string const& partitionId);
218 void EnsureSession(std::string const& partitionId);
219 Azure::Core::Amqp::_internal::Connection CreateConnection(std::string const& partitionId) const;
220 Azure::Core::Amqp::_internal::Session CreateSession(std::string const& partitionId) const;
221 Azure::Core::Amqp::_internal::Session GetSession(std::string const& partitionId);
222 std::shared_ptr<_detail::EventHubsPropertiesClient> GetPropertiesClient();
223 };
224}}} // namespace Azure::Messaging::EventHubs
The ConsumerClient class is a high level class used to consume events from an Event Hub.
Definition consumer_client.hpp:52
ConsumerClient & operator=(ConsumerClient const &other)=delete
Models::EventHubPartitionProperties GetPartitionProperties(std::string const &partitionID, Core::Context const &context={})
GetPartitionProperties gets properties for a specific partition. This includes data like the last enq...
Definition consumer_client.cpp:178
std::string const & GetConsumerGroup() const
Getter for consumer group name.
Definition consumer_client.hpp:78
PartitionClient CreatePartitionClient(std::string const &partitionId, PartitionClientOptions const &options={}, Azure::Core::Context const &context={})
Create new Partition client.
Definition consumer_client.cpp:154
ConsumerClient(ConsumerClient &&other)=delete
void Close(Azure::Core::Context const &context)
Closes the consumer client canceling any operations outstanding on any of the existing partition clie...
Definition consumer_client.cpp:78
ConsumerClient & operator=(ConsumerClient &&other)=delete
ConsumerClient(ConsumerClient const &other)=delete
Models::EventHubProperties GetEventHubProperties(Core::Context const &context={})
GetEventHubProperties gets properties of an eventHub. This includes data like name,...
Definition consumer_client.cpp:173
std::string const & GetEventHubName() const
Getter for event hub name.
Definition consumer_client.hpp:72
Models::ConsumerClientDetails GetDetails() const
Getter for client details.
Definition consumer_client.hpp:84
Azure::Core::Http::Policies::RetryOptions const & GetRetryOptions() const
Getter for retry options.
Definition consumer_client.hpp:97
Contains options for the ConsumerClient creation.
Definition consumer_client.hpp:29
std::string ApplicationID
ApplicationID is used as the identifier when setting the User-Agent property.
Definition consumer_client.hpp:32
Azure::Core::Http::Policies::RetryOptions RetryOptions
RetryOptions controls how often operations are retried from this client and any Receivers and Senders...
Definition consumer_client.hpp:37
std::string Name
Name of the consumer client.
Definition consumer_client.hpp:40
Contains options for the ConsumerClient creation.
Definition consumer_client_models.hpp:13
std::string ClientId
A unique name used to identify this consumer.
Definition consumer_client_models.hpp:29
std::string FullyQualifiedNamespace
The Fully Qualified Namespace that the Event Hub exists in.
Definition consumer_client_models.hpp:16
std::string ConsumerGroup
The name of the consumer group that this consumer is associated with. Events will be read only in the...
Definition consumer_client_models.hpp:21
std::string EventHubName
The name of the Event Hub that the consumer is connected to.
Definition consumer_client_models.hpp:25