This repository was archived by the owner on Jun 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEventHubReplicationTasks.cs
More file actions
147 lines (134 loc) · 6.29 KB
/
Copy pathEventHubReplicationTasks.cs
File metadata and controls
147 lines (134 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Microsoft Corporation.
// See LICENSE file in the project root for full license information.
namespace Azure.Messaging.Replication
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public class EventHubReplicationTasks
{
public static Task ForwardToEventHub(EventData[] input, EventHubProducerClient output,
ILogger log)
{
return ConditionalForwardToEventHub(input, output, log);
}
public static Task ConditionalForwardToEventHub(EventData[] input, EventHubProducerClient output,
ILogger log, Func<EventData, EventData> factory = null)
{
var tasks = new List<Task>();
var noPartitionBatch = new List<EventData>();
var partitionBatches = new Dictionary<string, List<EventData>>();
foreach (EventData inputEventData in input)
{
var eventData = factory != null ? factory(inputEventData) : inputEventData;
if (eventData == null)
{
continue;
}
eventData.Properties[Constants.ReplEnqueuedTimePropertyName] =
(eventData.Properties.ContainsKey(Constants.ReplEnqueuedTimePropertyName)
? eventData.Properties[Constants.ReplEnqueuedTimePropertyName] + ";"
: string.Empty) +
eventData.EnqueuedTime.ToString("u");
eventData.Properties[Constants.ReplOffsetPropertyName] =
(eventData.Properties.ContainsKey(Constants.ReplOffsetPropertyName)
? eventData.Properties[Constants.ReplOffsetPropertyName] + ";"
: string.Empty) +
eventData.Offset;
eventData.Properties[Constants.ReplSequencePropertyName] =
(eventData.Properties.ContainsKey(Constants.ReplSequencePropertyName)
? eventData.Properties[Constants.ReplSequencePropertyName] + ";"
: string.Empty) +
eventData.SequenceNumber.ToString();
if (eventData.PartitionKey != null)
{
if (!partitionBatches.ContainsKey(eventData.PartitionKey))
{
partitionBatches[eventData.PartitionKey] = new List<EventData>();
}
partitionBatches[eventData.PartitionKey].Add(eventData);
}
else
{
noPartitionBatch.Add(eventData);
}
}
if (noPartitionBatch.Count > 0)
{
tasks.Add(output.SendAsync(noPartitionBatch));
}
if (partitionBatches.Count > 0)
{
foreach (var batch in partitionBatches)
{
var options = new SendEventOptions
{
PartitionKey = batch.Key
};
tasks.Add(output.SendAsync(batch.Value, options));
}
}
return Task.WhenAll(tasks);
}
public static Task ForwardToServiceBus(EventData[] input, IAsyncCollector<ServiceBusMessage> output,
ILogger log)
{
return ConditionalForwardToServiceBus(input, output, log);
}
public static async Task ConditionalForwardToServiceBus(EventData[] input, IAsyncCollector<ServiceBusMessage> output,
ILogger log, Func<EventData, ServiceBusMessage> factory = null)
{
foreach (EventData eventData in input)
{
ServiceBusMessage message;
if (factory != null)
{
message = factory(eventData);
if (message == null)
{
continue;
}
}
else
{
message = new ServiceBusMessage(eventData.Body.ToArray())
{
ContentType = eventData.SystemProperties["content-type"] as string,
To = eventData.SystemProperties["to"] as string,
CorrelationId = eventData.SystemProperties["correlation-id"] as string,
ReplyTo = eventData.SystemProperties["reply-to"] as string,
ReplyToSessionId = eventData.SystemProperties["reply-to-group-name"] as string,
MessageId = eventData.SystemProperties["message-id"] as string ?? eventData.Offset.ToString(),
PartitionKey = eventData.PartitionKey,
SessionId = eventData.PartitionKey
};
foreach (var property in eventData.Properties)
{
message.ApplicationProperties.Add(property);
}
}
message.ApplicationProperties[Constants.ReplEnqueuedTimePropertyName] =
(eventData.Properties.ContainsKey(Constants.ReplEnqueuedTimePropertyName)
? eventData.Properties[Constants.ReplEnqueuedTimePropertyName] + ";"
: string.Empty) +
eventData.EnqueuedTime.ToString("u");
message.ApplicationProperties[Constants.ReplOffsetPropertyName] =
(eventData.Properties.ContainsKey(Constants.ReplOffsetPropertyName)
? eventData.Properties[Constants.ReplOffsetPropertyName] + ";"
: string.Empty) +
eventData.Offset;
message.ApplicationProperties[Constants.ReplSequencePropertyName] =
(eventData.Properties.ContainsKey(Constants.ReplSequencePropertyName)
? eventData.Properties[Constants.ReplSequencePropertyName] + ";"
: string.Empty) +
eventData.SequenceNumber.ToString();
await output.AddAsync(message);
}
}
}
}