-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathServiceBusListener.cs
129 lines (106 loc) · 4.63 KB
/
ServiceBusListener.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Listeners;
using Microsoft.ServiceBus.Messaging;
namespace Microsoft.Azure.WebJobs.ServiceBus.Listeners
{
internal sealed class ServiceBusListener : IListener
{
private readonly MessagingProvider _messagingProvider;
private readonly string _entityPath;
private readonly ServiceBusTriggerExecutor _triggerExecutor;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly MessageProcessor _messageProcessor;
private MessagingFactory _messagingFactory;
private MessageReceiver _receiver;
private bool _disposed;
private bool _started;
public ServiceBusListener(MessagingFactory messagingFactory, string entityPath, ServiceBusTriggerExecutor triggerExecutor, ServiceBusConfiguration config)
{
_messagingFactory = messagingFactory;
_entityPath = entityPath;
_triggerExecutor = triggerExecutor;
_cancellationTokenSource = new CancellationTokenSource();
_messagingProvider = config.MessagingProvider;
_messageProcessor = config.MessagingProvider.CreateMessageProcessor(entityPath);
}
public Task StartAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
if (_started)
{
throw new InvalidOperationException("The listener has already been started.");
}
_receiver = _messagingProvider.CreateMessageReceiver(_messagingFactory, _entityPath);
_receiver.OnMessageAsync(ProcessMessageAsync, _messageProcessor.MessageOptions);
_started = true;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
if (!_started)
{
throw new InvalidOperationException("The listener has not yet been started or has already been stopped.");
}
// cancel our token source to signal any in progress
// ProcessMessageAsync invocations to cancel
_cancellationTokenSource.Cancel();
// abort the message factory which will stop all receivers
// created by it so no new work is started
_messagingFactory.Abort();
_started = false;
return Task.CompletedTask;
}
public void Cancel()
{
ThrowIfDisposed();
_cancellationTokenSource.Cancel();
}
[SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "_cancellationTokenSource")]
public void Dispose()
{
if (!_disposed)
{
// Running callers might still be using the cancellation token.
// Mark it canceled but don't dispose of the source while the callers are running.
// Otherwise, callers would receive ObjectDisposedException when calling token.Register.
// For now, rely on finalization to clean up _cancellationTokenSource's wait handle (if allocated).
_cancellationTokenSource.Cancel();
if (_messagingFactory != null)
{
_messagingFactory.Abort();
_messagingFactory = null;
}
// Aborting the messaging factory aborts the receiver as well
_receiver = null;
_disposed = true;
}
}
private Task ProcessMessageAsync(BrokeredMessage message)
{
return ProcessMessageAsync(message, _cancellationTokenSource.Token);
}
internal async Task ProcessMessageAsync(BrokeredMessage message, CancellationToken cancellationToken)
{
if (!await _messageProcessor.BeginProcessingMessageAsync(message, cancellationToken))
{
return;
}
FunctionResult result = await _triggerExecutor.ExecuteAsync(message, cancellationToken);
await _messageProcessor.CompleteProcessingMessageAsync(message, result, cancellationToken);
}
private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(null);
}
}
}
}