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 pathServiceBusReplicationTasks.cs
More file actions
140 lines (123 loc) · 5.83 KB
/
Copy pathServiceBusReplicationTasks.cs
File metadata and controls
140 lines (123 loc) · 5.83 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
// 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 ServiceBusReplicationTasks
{
public static Task ForwardToEventHub(ServiceBusReceivedMessage[] input, EventHubProducerClient output, ILogger log)
{
return ConditionalForwardToEventHub(input, output, log);
}
public static Task ConditionalForwardToEventHub(ServiceBusReceivedMessage[] input, EventHubProducerClient output, ILogger log, Func<ServiceBusReceivedMessage, EventData> factory = null)
{
var tasks = new List<Task>();
var noPartitionBatch = new List<EventData>();
var partitionBatches = new Dictionary<string, List<EventData>>();
foreach (var message in input)
{
EventData eventData;
var key = message.PartitionKey ?? message.SessionId;
if (factory != null)
{
eventData = factory(message);
if (eventData == null)
{
continue;
}
}
else
{
eventData = new EventData(message.Body);
foreach (var property in message.ApplicationProperties)
{
eventData.Properties.Add(property);
}
}
eventData.Properties[Constants.ReplEnqueuedTimePropertyName] =
(message.ApplicationProperties.ContainsKey(Constants.ReplEnqueuedTimePropertyName)
? message.ApplicationProperties[Constants.ReplEnqueuedTimePropertyName] + ";"
: string.Empty) +
message.ScheduledEnqueueTime.ToString("u");
eventData.Properties[Constants.ReplOffsetPropertyName] =
(message.ApplicationProperties.ContainsKey(Constants.ReplOffsetPropertyName)
? message.ApplicationProperties[Constants.ReplOffsetPropertyName] + ";"
: string.Empty) +
message.SequenceNumber.ToString();
eventData.Properties[Constants.ReplSequencePropertyName] =
(message.ApplicationProperties.ContainsKey(Constants.ReplSequencePropertyName)
? message.ApplicationProperties[Constants.ReplSequencePropertyName] + ";"
: string.Empty) +
message.SequenceNumber.ToString();
if (key != null)
{
if (!partitionBatches.ContainsKey(key))
{
partitionBatches[key] = new List<EventData>();
}
partitionBatches[key].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(ServiceBusReceivedMessage[] input, IAsyncCollector<ServiceBusMessage> output,
ILogger log)
{
return ConditionalForwardToServiceBus(input, output, log);
}
public static async Task ConditionalForwardToServiceBus(ServiceBusReceivedMessage[] input, IAsyncCollector<ServiceBusMessage> output,
ILogger log, Func<ServiceBusReceivedMessage, ServiceBusMessage> factory = null)
{
foreach (ServiceBusReceivedMessage message in input)
{
var forwardedMessage = factory != null ? factory(message) : new ServiceBusMessage(message); //?
if (forwardedMessage == null)
{
continue;
}
forwardedMessage.ApplicationProperties[Constants.ReplEnqueuedTimePropertyName] =
(message.ApplicationProperties.ContainsKey(Constants.ReplEnqueuedTimePropertyName)
? message.ApplicationProperties[Constants.ReplEnqueuedTimePropertyName] + ";"
: string.Empty) +
message.ScheduledEnqueueTime.ToString("u");
forwardedMessage.ApplicationProperties[Constants.ReplOffsetPropertyName] =
(message.ApplicationProperties.ContainsKey(Constants.ReplOffsetPropertyName)
? message.ApplicationProperties[Constants.ReplOffsetPropertyName] + ";"
: string.Empty) +
message.SequenceNumber.ToString();
forwardedMessage.ApplicationProperties[Constants.ReplSequencePropertyName] =
(message.ApplicationProperties.ContainsKey(Constants.ReplSequencePropertyName)
? message.ApplicationProperties[Constants.ReplSequencePropertyName] + ";"
: string.Empty) +
message.SequenceNumber.ToString();
await output.AddAsync(forwardedMessage);
}
}
}
}