-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathMessageProcessor.cs
81 lines (75 loc) · 3.52 KB
/
MessageProcessor.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
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.ServiceBus.Messaging;
namespace Microsoft.Azure.WebJobs.ServiceBus
{
/// <summary>
/// This class defines a strategy used for processing ServiceBus messages.
/// </summary>
/// <remarks>
/// Custom <see cref="MessageProcessor"/> implementations can be specified by implementing
/// a custom <see cref="MessagingProvider"/> and setting it via <see cref="ServiceBusConfiguration.MessagingProvider"/>.
/// </remarks>
public class MessageProcessor
{
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="messageOptions">The <see cref="OnMessageOptions"/> to use.</param>
public MessageProcessor(OnMessageOptions messageOptions)
{
if (messageOptions == null)
{
throw new ArgumentNullException("messageOptions");
}
MessageOptions = messageOptions;
}
/// <summary>
/// Gets the <see cref="OnMessageOptions"/> that will be used by the <see cref="MessageReceiver"/>.
/// </summary>
public OnMessageOptions MessageOptions { get; protected set; }
/// <summary>
/// This method is called when there is a new message to process, before the job function is invoked.
/// This allows any preprocessing to take place on the message before processing begins.
/// </summary>
/// <param name="message">The message to process.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>A <see cref="Task"/> that returns true if the message processing should continue, false otherwise.</returns>
public virtual async Task<bool> BeginProcessingMessageAsync(BrokeredMessage message, CancellationToken cancellationToken)
{
return await Task.FromResult<bool>(true);
}
/// <summary>
/// This method completes processing of the specified message, after the job function has been invoked.
/// </summary>
/// <param name="message">The message to complete processing for.</param>
/// <param name="result">The <see cref="FunctionResult"/> from the job invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use</param>
/// <returns>A <see cref="Task"/> that will complete the message processing.</returns>
public virtual async Task CompleteProcessingMessageAsync(BrokeredMessage message, FunctionResult result, CancellationToken cancellationToken)
{
if (result.Succeeded)
{
if (!MessageOptions.AutoComplete)
{
// AutoComplete is true by default, but if set to false
// we need to complete the message
cancellationToken.ThrowIfCancellationRequested();
await message.CompleteAsync();
}
}
else
{
// if the invocation failed, we must propagate the
// exception back to SB so it can handle message state
// correctly
cancellationToken.ThrowIfCancellationRequested();
throw result.Exception;
}
}
}
}