azure-messaging-eventhubs
Loading...
Searching...
No Matches
producer_client.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// cspell: words myeventhub
5
6#pragma once
7#include "event_data_batch.hpp"
8#include "models/management_models.hpp"
9
10#include <azure/core/amqp.hpp>
11#include <azure/core/amqp/internal/management.hpp>
12#include <azure/core/amqp/internal/message_sender.hpp>
13#include <azure/core/context.hpp>
14#include <azure/core/credentials/credentials.hpp>
15#include <azure/core/http/policies/policy.hpp>
16
17#include <iostream>
18
19namespace Azure { namespace Messaging { namespace EventHubs {
20
24 {
27 std::string ApplicationID = "";
28
32 Azure::Core::Http::Policies::RetryOptions RetryOptions{};
33
36 std::string Name{};
37
40 Azure::Nullable<std::uint64_t> MaxMessageSize{};
41 };
42
45 class ProducerClient final {
46
47 public:
49 std::string const& GetEventHubName() { return m_eventHub; }
50
52 Azure::Core::Http::Policies::RetryOptions const& GetRetryOptions() const
53 {
54 return m_producerClientOptions.RetryOptions;
55 }
56
58 ProducerClient(ProducerClient const& other) = delete;
59
61 ProducerClient& operator=(ProducerClient const& other) = delete;
62
63 ProducerClient(ProducerClient&& other) = delete;
64 ProducerClient& operator=(ProducerClient&& other) = delete;
65
67 ProducerClient() = default;
68
76 std::string const& connectionString,
77 std::string const& eventHub,
78 ProducerClientOptions options = {});
79
88 std::string const& fullyQualifiedNamespace,
89 std::string const& eventHub,
90 std::shared_ptr<Azure::Core::Credentials::TokenCredential> credential,
91 ProducerClientOptions options = {});
92
93 ~ProducerClient()
94 {
95 for (auto& sender : m_senders)
96 {
97 sender.second.Close();
98 }
99 m_senders.clear();
100 }
101
109 EventDataBatch CreateBatch(
110 EventDataBatchOptions const& options = {},
111 Azure::Core::Context const& context = {});
112
118 void Send(EventDataBatch const& eventDataBatch, Core::Context const& context = {});
119
128 void Send(Models::EventData const& eventData, Core::Context const& context = {});
129
138 void Send(std::vector<Models::EventData> const& eventData, Core::Context const& context = {});
139
145 Models::EventHubProperties GetEventHubProperties(Core::Context const& context = {});
146
154 Models::EventHubPartitionProperties GetPartitionProperties(
155 std::string const& partitionID,
156 Core::Context const& context = {});
157
158 private:
160 std::string m_connectionString;
161
163 std::string m_fullyQualifiedNamespace;
164
166 std::string m_eventHub{};
167
169 std::string m_targetUrl{};
170
172 std::shared_ptr<Core::Credentials::TokenCredential> m_credential{};
173
174 ProducerClientOptions m_producerClientOptions{};
175
176 // Protects m_senders and m_connection.
177 std::mutex m_sendersLock;
178 std::map<std::string, Azure::Core::Amqp::_internal::Connection> m_connections{};
179 std::map<std::string, Azure::Core::Amqp::_internal::MessageSender> m_senders{};
180
181 std::mutex m_sessionsLock;
182 std::map<std::string, Azure::Core::Amqp::_internal::Session> m_sessions{};
183
184 Azure::Core::Amqp::_internal::Connection CreateConnection() const;
185 Azure::Core::Amqp::_internal::Session CreateSession(std::string const& partitionId);
186
187 // Ensure that the connection for this producer has been established.
188 void EnsureConnection(const std::string& partitionId);
189
190 // Ensure that a session for the specified partition ID has been established.
191 void EnsureSession(std::string const& partitionId);
192
193 // Ensure that a message sender for the specified partition has been created.
194 void EnsureSender(std::string const& partitionId, Azure::Core::Context const& context = {});
195
196 Azure::Core::Amqp::_internal::MessageSender GetSender(std::string const& partitionId);
197 Azure::Core::Amqp::_internal::Session GetSession(std::string const& partitionId);
198 };
199}}} // namespace Azure::Messaging::EventHubs
ProducerClient can be used to send events to an Event Hub.
Definition producer_client.hpp:45
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 producer_client.cpp:202
Azure::Core::Http::Policies::RetryOptions const & GetRetryOptions() const
Definition producer_client.hpp:52
ProducerClient & operator=(ProducerClient const &other)=delete
ProducerClient(ProducerClient const &other)=delete
std::string const & GetEventHubName()
Definition producer_client.hpp:49
EventDataBatch CreateBatch(EventDataBatchOptions const &options={}, Azure::Core::Context const &context={})
Create a new EventDataBatch to be sent to the Event Hub.
Definition producer_client.cpp:50
void Send(EventDataBatch const &eventDataBatch, Core::Context const &context={})
Send an EventDataBatch to the remote Event Hub.
Definition producer_client.cpp:66
Models::EventHubProperties GetEventHubProperties(Core::Context const &context={})
GetEventHubProperties gets properties of an eventHub. This includes data like name,...
Definition producer_client.cpp:193
Contains options for the ProducerClient creation.
Definition producer_client.hpp:24
Azure::Core::Http::Policies::RetryOptions RetryOptions
RetryOptions controls how often operations are retried from this client and any Receivers and Senders...
Definition producer_client.hpp:32
Azure::Nullable< std::uint64_t > MaxMessageSize
The maximum size of the message that can be sent.
Definition producer_client.hpp:40
std::string Name
The name of the producer client link, used in diagnostics.
Definition producer_client.hpp:36
std::string ApplicationID
Application ID that will be passed to the namespace.
Definition producer_client.hpp:27