| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Amqp |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Amqp; |
| | 10 | | using Azure.Amqp.Encoding; |
| | 11 | | using Core; |
| | 12 | | using Framing; |
| | 13 | | using Primitives; |
| | 14 | |
|
| | 15 | | internal sealed class AmqpSubscriptionClient : IInnerSubscriptionClient |
| | 16 | | { |
| | 17 | | int prefetchCount; |
| | 18 | | readonly object syncLock; |
| | 19 | | MessageReceiver innerReceiver; |
| | 20 | |
|
| | 21 | | static AmqpSubscriptionClient() |
| | 22 | | { |
| 0 | 23 | | AmqpCodec.RegisterKnownTypes(AmqpTrueFilterCodec.Name, AmqpTrueFilterCodec.Code, () => new AmqpTrueFilterCod |
| 0 | 24 | | AmqpCodec.RegisterKnownTypes(AmqpFalseFilterCodec.Name, AmqpFalseFilterCodec.Code, () => new AmqpFalseFilter |
| 0 | 25 | | AmqpCodec.RegisterKnownTypes(AmqpCorrelationFilterCodec.Name, AmqpCorrelationFilterCodec.Code, () => new Amq |
| 0 | 26 | | AmqpCodec.RegisterKnownTypes(AmqpSqlFilterCodec.Name, AmqpSqlFilterCodec.Code, () => new AmqpSqlFilterCodec( |
| 0 | 27 | | AmqpCodec.RegisterKnownTypes(AmqpEmptyRuleActionCodec.Name, AmqpEmptyRuleActionCodec.Code, () => new AmqpEmp |
| 0 | 28 | | AmqpCodec.RegisterKnownTypes(AmqpSqlRuleActionCodec.Name, AmqpSqlRuleActionCodec.Code, () => new AmqpSqlRule |
| 0 | 29 | | AmqpCodec.RegisterKnownTypes(AmqpRuleDescriptionCodec.Name, AmqpRuleDescriptionCodec.Code, () => new AmqpRul |
| 0 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | public AmqpSubscriptionClient( |
| 0 | 33 | | string path, |
| 0 | 34 | | ServiceBusConnection servicebusConnection, |
| 0 | 35 | | RetryPolicy retryPolicy, |
| 0 | 36 | | ICbsTokenProvider cbsTokenProvider, |
| 0 | 37 | | int prefetchCount = 0, |
| 0 | 38 | | ReceiveMode mode = ReceiveMode.ReceiveAndDelete) |
| | 39 | | { |
| 0 | 40 | | this.syncLock = new object(); |
| 0 | 41 | | this.Path = path; |
| 0 | 42 | | this.ServiceBusConnection = servicebusConnection; |
| 0 | 43 | | this.RetryPolicy = retryPolicy; |
| 0 | 44 | | this.CbsTokenProvider = cbsTokenProvider; |
| 0 | 45 | | this.PrefetchCount = prefetchCount; |
| 0 | 46 | | this.ReceiveMode = mode; |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public MessageReceiver InnerReceiver |
| | 50 | | { |
| | 51 | | get |
| | 52 | | { |
| 0 | 53 | | if (this.innerReceiver == null) |
| | 54 | | { |
| 0 | 55 | | lock (this.syncLock) |
| | 56 | | { |
| 0 | 57 | | if (this.innerReceiver == null) |
| | 58 | | { |
| 0 | 59 | | this.innerReceiver = new MessageReceiver( |
| 0 | 60 | | this.Path, |
| 0 | 61 | | MessagingEntityType.Subscriber, |
| 0 | 62 | | this.ReceiveMode, |
| 0 | 63 | | this.ServiceBusConnection, |
| 0 | 64 | | this.CbsTokenProvider, |
| 0 | 65 | | this.RetryPolicy, |
| 0 | 66 | | this.PrefetchCount); |
| | 67 | | } |
| 0 | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | return this.innerReceiver; |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Gets or sets the number of messages that the subscription client can simultaneously request. |
| | 77 | | /// </summary> |
| | 78 | | /// <value>The number of messages that the subscription client can simultaneously request.</value> |
| | 79 | | public int PrefetchCount |
| | 80 | | { |
| 0 | 81 | | get => this.prefetchCount; |
| | 82 | | set |
| | 83 | | { |
| 0 | 84 | | if (value < 0) |
| | 85 | | { |
| 0 | 86 | | throw Fx.Exception.ArgumentOutOfRange(nameof(this.PrefetchCount), value, "Value cannot be less than |
| | 87 | | } |
| 0 | 88 | | this.prefetchCount = value; |
| 0 | 89 | | if (this.innerReceiver != null) |
| | 90 | | { |
| 0 | 91 | | this.innerReceiver.PrefetchCount = value; |
| | 92 | | } |
| 0 | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | ServiceBusConnection ServiceBusConnection { get; } |
| | 97 | |
|
| 0 | 98 | | RetryPolicy RetryPolicy { get; } |
| | 99 | |
|
| 0 | 100 | | ICbsTokenProvider CbsTokenProvider { get; } |
| | 101 | |
|
| 0 | 102 | | ReceiveMode ReceiveMode { get; } |
| | 103 | |
|
| 0 | 104 | | string Path { get; } |
| | 105 | |
|
| | 106 | | public Task CloseAsync() |
| | 107 | | { |
| 0 | 108 | | return this.innerReceiver?.CloseAsync(); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | public async Task OnAddRuleAsync(RuleDescription description) |
| | 112 | | { |
| | 113 | | try |
| | 114 | | { |
| 0 | 115 | | var amqpRequestMessage = AmqpRequestMessage.CreateRequest( |
| 0 | 116 | | ManagementConstants.Operations.AddRuleOperation, |
| 0 | 117 | | this.ServiceBusConnection.OperationTimeout, |
| 0 | 118 | | null); |
| 0 | 119 | | amqpRequestMessage.Map[ManagementConstants.Properties.RuleName] = description.Name; |
| 0 | 120 | | amqpRequestMessage.Map[ManagementConstants.Properties.RuleDescription] = |
| 0 | 121 | | AmqpMessageConverter.GetRuleDescriptionMap(description); |
| | 122 | |
|
| 0 | 123 | | var response = await this.InnerReceiver.ExecuteRequestResponseAsync(amqpRequestMessage).ConfigureAwait(f |
| | 124 | |
|
| 0 | 125 | | if (response.StatusCode != AmqpResponseStatusCode.OK) |
| | 126 | | { |
| 0 | 127 | | throw response.ToMessagingContractException(); |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | | catch (Exception exception) |
| | 131 | | { |
| 0 | 132 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 133 | | } |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public async Task OnRemoveRuleAsync(string ruleName) |
| | 137 | | { |
| | 138 | | try |
| | 139 | | { |
| 0 | 140 | | var amqpRequestMessage = |
| 0 | 141 | | AmqpRequestMessage.CreateRequest( |
| 0 | 142 | | ManagementConstants.Operations.RemoveRuleOperation, |
| 0 | 143 | | this.ServiceBusConnection.OperationTimeout, |
| 0 | 144 | | null); |
| 0 | 145 | | amqpRequestMessage.Map[ManagementConstants.Properties.RuleName] = ruleName; |
| | 146 | |
|
| 0 | 147 | | var response = await this.InnerReceiver.ExecuteRequestResponseAsync(amqpRequestMessage).ConfigureAwait(f |
| | 148 | |
|
| 0 | 149 | | if (response.StatusCode != AmqpResponseStatusCode.OK) |
| | 150 | | { |
| 0 | 151 | | throw response.ToMessagingContractException(); |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | | catch (Exception exception) |
| | 155 | | { |
| 0 | 156 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 157 | | } |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | public async Task<IList<RuleDescription>> OnGetRulesAsync(int top, int skip) |
| | 161 | | { |
| | 162 | | try |
| | 163 | | { |
| 0 | 164 | | var amqpRequestMessage = |
| 0 | 165 | | AmqpRequestMessage.CreateRequest( |
| 0 | 166 | | ManagementConstants.Operations.EnumerateRulesOperation, |
| 0 | 167 | | this.ServiceBusConnection.OperationTimeout, |
| 0 | 168 | | null); |
| 0 | 169 | | amqpRequestMessage.Map[ManagementConstants.Properties.Top] = top; |
| 0 | 170 | | amqpRequestMessage.Map[ManagementConstants.Properties.Skip] = skip; |
| | 171 | |
|
| 0 | 172 | | var response = await this.InnerReceiver.ExecuteRequestResponseAsync(amqpRequestMessage).ConfigureAwait(f |
| 0 | 173 | | var ruleDescriptions = new List<RuleDescription>(); |
| 0 | 174 | | if (response.StatusCode == AmqpResponseStatusCode.OK) |
| | 175 | | { |
| 0 | 176 | | var ruleList = response.GetListValue<AmqpMap>(ManagementConstants.Properties.Rules); |
| 0 | 177 | | foreach (var entry in ruleList) |
| | 178 | | { |
| 0 | 179 | | var amqpRule = (AmqpRuleDescriptionCodec)entry[ManagementConstants.Properties.RuleDescription]; |
| 0 | 180 | | var ruleDescription = AmqpMessageConverter.GetRuleDescription(amqpRule); |
| 0 | 181 | | ruleDescriptions.Add(ruleDescription); |
| | 182 | | } |
| | 183 | | } |
| 0 | 184 | | else if (response.StatusCode == AmqpResponseStatusCode.NoContent) |
| | 185 | | { |
| | 186 | | // Do nothing. Return empty list; |
| | 187 | | } |
| | 188 | | else |
| | 189 | | { |
| 0 | 190 | | throw response.ToMessagingContractException(); |
| | 191 | | } |
| | 192 | |
|
| 0 | 193 | | return ruleDescriptions; |
| | 194 | | } |
| | 195 | | catch (Exception exception) |
| | 196 | | { |
| 0 | 197 | | throw AmqpExceptionHelper.GetClientException(exception); |
| | 198 | | } |
| 0 | 199 | | } |
| | 200 | | } |
| | 201 | | } |