private void PostConfiguration(ServiceBusOptions serviceBusConfig) { if (serviceBusConfig == null) { return; } if (serviceBusConfig.RetryPolicy == null) { serviceBusConfig.Policy = RetryPolicy.Default; } else if (serviceBusConfig.RetryPolicy.MaximumRetryCount == 0 && serviceBusConfig.RetryPolicy.MaximumBackoffInSeconds == 0 && serviceBusConfig.RetryPolicy.MinimumBackoffInSeconds == 0 && serviceBusConfig.RetryPolicy.DeltaBackoffInSeconds == 0) { serviceBusConfig.Policy = RetryPolicy.NoRetry; } else { var retryExponential = new RetryExponential(TimeSpan.FromSeconds(serviceBusConfig.RetryPolicy.MinimumBackoffInSeconds), TimeSpan.FromSeconds(serviceBusConfig.RetryPolicy.MaximumBackoffInSeconds), TimeSpan.FromSeconds(serviceBusConfig.RetryPolicy.DeltaBackoffInSeconds), serviceBusConfig.RetryPolicy.MaximumRetryCount); serviceBusConfig.Policy = retryExponential; } }
public void Listen() { subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); RetryPolicy policy = new RetryExponential(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(29), 10); subscriptionClient.ServiceBusConnection.RetryPolicy = policy; var sessionOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { AutoComplete = false, MaxConcurrentSessions = _concurrentSessions, MaxAutoRenewDuration = TimeSpan.FromSeconds(20) //MessageWaitTimeout = TimeSpan.FromSeconds(30) }; subscriptionClient.PrefetchCount = 250; subscriptionClient.RegisterSessionHandler(OnMessage, sessionOptions); if (_autoTryReconnect) { while (true) { Task.Delay(10000).GetAwaiter().GetResult(); TryReconnect(); } } }
public async void Listen() { subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); RetryPolicy policy = new RetryExponential(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), 3); subscriptionClient.ServiceBusConnection.RetryPolicy = policy; var sessionOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { AutoComplete = false, MaxConcurrentCalls = _concurrentSessions, MaxAutoRenewDuration = TimeSpan.FromMinutes(_messageLockMinutes) }; subscriptionClient.PrefetchCount = 0; subscriptionClient.RegisterMessageHandler(OnMessage, sessionOptions); if (_autoTryReconnect) { while (true) { await Task.Delay(10000); TryReconnect(); } } }
async Task <ConnectionContext> CreateConnection(ISupervisor supervisor) { var endpoint = new UriBuilder(_hostConfiguration.HostAddress) { Path = "" }.Uri.ToString(); if (supervisor.Stopping.IsCancellationRequested) { throw new OperationCanceledException($"The connection is stopping and cannot be used: {endpoint}"); } var settings = _hostConfiguration.Settings; var retryPolicy = new RetryExponential(settings.RetryMinBackoff, settings.RetryMaxBackoff, settings.RetryLimit); var connection = new ServiceBusConnection(endpoint, settings.TransportType, retryPolicy) { TokenProvider = settings.TokenProvider, OperationTimeout = settings.OperationTimeout }; var managementClient = new ManagementClient(endpoint, settings.TokenProvider); return(new ServiceBusConnectionContext(connection, managementClient, retryPolicy, settings.OperationTimeout, supervisor.Stopped)); }
void RetryExponentialServerBusyShouldSelfResetTest() { RetryExponential policy1 = (RetryExponential)RetryPolicy.Default; int retryCount = 0; TimeSpan duration = Constants.DefaultOperationTimeout; ServerBusyException exception = new ServerBusyException(string.Empty); TimeSpan retryInterval; // First ServerBusy exception Assert.False(policy1.IsServerBusy, "policy1.IsServerBusy should start with false"); Assert.True(policy1.ShouldRetry(duration, retryCount, exception, out retryInterval), "We should retry, but it returned false"); Assert.True(policy1.IsServerBusy, "policy1.IsServerBusy should be true"); System.Threading.Thread.Sleep(3000); // Setting it a second time should not prolong the call. Assert.True(policy1.IsServerBusy, "policy1.IsServerBusy should be true"); Assert.True(policy1.ShouldRetry(duration, retryCount, exception, out retryInterval), "We should retry, but it return false"); Assert.True(policy1.IsServerBusy, "policy1.IsServerBusy should be true"); System.Threading.Thread.Sleep(8000); // 3 + 8 = 11s Assert.False(policy1.IsServerBusy, "policy1.IsServerBusy should stay false after 11s"); // Setting ServerBusy for second time. Assert.True(policy1.ShouldRetry(duration, retryCount, exception, out retryInterval), "We should retry, but it return false"); Assert.True(policy1.IsServerBusy, "policy1.IsServerBusy is not true"); }
private void PostConfiguration(ServiceBusOptions serviceBusConfig) { if (!Enum.IsDefined(typeof(TransportType), serviceBusConfig.TransportType)) { throw new ArgumentOutOfRangeException(nameof(serviceBusConfig.TransportType), $"Value specified was not a valid {typeof(TransportType).FullName}."); } if (!Enum.IsDefined(typeof(ReceiveMode), serviceBusConfig.ReceiveMode)) { throw new ArgumentOutOfRangeException(nameof(serviceBusConfig.ReceiveMode), $"Value specified was not a valid {typeof(ReceiveMode).FullName}."); } if (serviceBusConfig.RetryPolicy == null) { serviceBusConfig.Policy = RetryPolicy.Default; } else if (serviceBusConfig.RetryPolicy.MaximumRetryCount == 0 && serviceBusConfig.RetryPolicy.MaximumBackoff == 0 && serviceBusConfig.RetryPolicy.MinimumBackoff == 0) { serviceBusConfig.Policy = RetryPolicy.NoRetry; } else { var retryExponential = new RetryExponential(TimeSpan.FromSeconds(serviceBusConfig.RetryPolicy.MinimumBackoff), TimeSpan.FromSeconds(serviceBusConfig.RetryPolicy.MaximumBackoff), serviceBusConfig.RetryPolicy.MaximumRetryCount); serviceBusConfig.Policy = retryExponential; } }
public ISubscriptionClient Create(string connectionString, string topicName, string subscriptionName, IRetryPolicy retryPolicy) { ValidateInputs(connectionString, topicName, subscriptionName, retryPolicy); if (retryPolicy == RetryPolicyBase.NoRetry) { return(new SubscriptionClient( connectionString, topicName, subscriptionName, ReceiveMode.PeekLock, new NoRetry())); } /* * Uses an exponential back off, this means that the first retry will be performed within a fairly short space of time, * and the retries will get a progressively longer. */ var policy = new RetryExponential( TimeSpan.FromSeconds(retryPolicy.MinimumAllowableRetrySeconds), TimeSpan.FromSeconds(retryPolicy.MaximumAllowableRetrySeconds), retryPolicy.MaximumRetryCount ); return(new SubscriptionClient( connectionString, topicName, subscriptionName, ReceiveMode.PeekLock, policy)); }
public async Task CanPurgeQueue() { var queueName = TestConfig.GetName("send"); await _managementClient.CreateQueueIfNotExistsAsync(queueName); var retryPolicy = new RetryExponential(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5), 10); var queueClient = new QueueClient(ConnectionString, queueName, receiveMode: ReceiveMode.PeekLock, retryPolicy: retryPolicy); await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!"))); await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!"))); await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!"))); await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven!"))); await ManagementExtensions.PurgeQueue(ConnectionString, queueName); var client = new QueueClient(ConnectionString, queueName); var somethingWasReceived = new ManualResetEvent(false); client.RegisterMessageHandler(async(_, __) => somethingWasReceived.Set(), async _ => somethingWasReceived.Set()); Assert.That(somethingWasReceived.WaitOne(TimeSpan.FromSeconds(1)), Is.False, $"Looks like a message was received from the queue '{queueName}' even though it was purged :o"); }
private void InitializeClients(ChannelType channelType) { var channelName = _configuration.GetServiceBusChannelName(channelType, _channel); var retryPolicy = new RetryExponential(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 10); var connectionString = _configuration.GetServiceBusConnectionString(); switch (channelType) { case ChannelType.PublishSubscribe: _receiverClient = new SubscriptionClient(connectionString, channelName, _subscriptionName, retryPolicy: retryPolicy); _senderClient = new TopicClient(connectionString, channelName, retryPolicy); break; case ChannelType.PointToPoint: var queueClient = new QueueClient(connectionString, channelName, ReceiveMode.PeekLock, retryPolicy); _receiverClient = queueClient; _senderClient = queueClient; break; default: throw new Exception($"Unkown channel type {channelType}"); } string deadPath = EntityNameHelper.FormatDeadLetterPath(_receiverClient.Path); _deadLetterQueue = new MessageReceiver(connectionString, deadPath, ReceiveMode.PeekLock); }
void RetryExponentialShouldRetryTest(Exception exception, int currentRetryCount, bool expectedShouldRetry) { var retry = new RetryExponential(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(20), 5); var remainingTime = Constants.DefaultOperationTimeout; TimeSpan retryInterval; var shouldRetry = retry.ShouldRetry(remainingTime, currentRetryCount, exception, out retryInterval); Assert.True(shouldRetry == expectedShouldRetry); }
public void RetryExponentialShouldRetryTest(string retryTestCaseName) { var testCase = RetryTestCases[retryTestCaseName]; var retry = new RetryExponential(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(20), 5); var remainingTime = Constants.DefaultOperationTimeout; var shouldRetry = retry.ShouldRetry(remainingTime, testCase.RetryCount, testCase.Exception, out var _); Assert.True(shouldRetry == testCase.ShouldRetry); }
public ServiceBusQueueListener(Func <ServiceBusQueueListenerModel, Task> callback, string serviceBusConnectionString, string queueName, ILogger logger) { _callback = callback; _serviceBusConnectionString = serviceBusConnectionString; var retryPolicy = new RetryExponential(TimeSpan.Zero, TimeSpan.FromMinutes(5), 3); _queueClient = new QueueClient(serviceBusConnectionString, queueName, ReceiveMode.PeekLock, retryPolicy); _logger = logger; }
public GatewayMessageSender(GatewaySettings settings, ILogger logger) { _settings = settings; _logger = logger; var retryPolicy = new RetryExponential(TimeSpan.FromSeconds(3), TimeSpan.FromMinutes(3), 25); _sender = new MessageSender(settings.ServieBusConnectionString, settings.SendQueueName, retryPolicy); _logger.Information($"Conneted SmsSender '{settings.ServiceName}' to outgoing queue: '{settings.SendQueueName}' "); }
private ITopicClient CreateTopicClient() { var retryPolicy = new RetryExponential( minimumBackoff: TimeSpan.FromSeconds(_minimumBackOff), maximumBackoff: TimeSpan.FromSeconds(_maximumBackOff), maximumRetryCount: _maxRetryCountOnSend ); return(new TopicClient(_serviceBusConnectionString, _topicName, retryPolicy)); }
public ServiceBusMessageReceiverService(IMvxMessenger messenger) { _messenger = messenger; var minimumBackoff = TimeSpan.FromSeconds(0); var maximumBackoff = TimeSpan.FromSeconds(5); var maxRetryCount = 3; _retryPolicy = new RetryExponential(minimumBackoff, maximumBackoff, maxRetryCount); }
private MessageSender CreateMessageSender() { var retryPolicy = new RetryExponential( minimumBackoff: TimeSpan.FromSeconds(_minimumBackOff), maximumBackoff: TimeSpan.FromSeconds(_maximumBackOff), maximumRetryCount: _maxRetryCountOnSend ); return(new MessageSender(_serviceBusConnectionString, _queueName, retryPolicy)); }
public AzureServiceBus(ServiceBusConnection connection, ServiceBusConfiguration config, IServiceProvider services) { _connection = connection; Configuration = config; _services = services; _retryPolicy = new RetryExponential( TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1), 10); }
private ITopicClient Create(string topicName) { RetryExponential retryPolicy = new RetryExponential(_serviceBusConfiguration.RetryPolicyMinimumBackoff, _serviceBusConfiguration.RetryPolicyMaximumBackoff, _serviceBusConfiguration.RetryPolicyMaximumRetryCount); var topicClient = new TopicClient(_serviceBusConfiguration.ConnectionString, topicName, retryPolicy); return(topicClient); }
private static SubscriptionClient CreateMessageReceiver() { var retryPolicy = new RetryExponential( RETRY_MIN_BACKOFF, RETRY_MAX_BACKOFF, RETRY_COUNT); var subscriptionClient = new SubscriptionClient(CN, TOPIC_NAME, SUBSCRIPTION_NAME); return(subscriptionClient); }
static FileOrchestrator() { // For performance reasons we are using a static instance of the TopicClient per best practices if (_topicClient == null) { string connectionString = Environment.GetEnvironmentVariable("ServiceBusConnString", EnvironmentVariableTarget.Process); // Set the retry policy var retryPolicy = new RetryExponential(TimeSpan.FromMilliseconds(300), TimeSpan.FromMilliseconds(1000), 5); _topicClient = new TopicClient(connectionString, _topicName, retryPolicy); } }
protected override IQueue<SimpleWorkItem> GetQueue(int retries = 1, TimeSpan? workItemTimeout = null, TimeSpan? retryDelay = null, int deadLetterMaxItems = 100, bool runQueueMaintenance = true) { if (ConnectionStrings.Get("ServiceBusConnectionString") == null) return null; if (!retryDelay.HasValue) retryDelay = TimeSpan.Zero; var maxBackoff = retryDelay.Value > TimeSpan.Zero ? retryDelay.Value + retryDelay.Value : TimeSpan.FromSeconds(1); var retryPolicy = new RetryExponential(retryDelay.Value, maxBackoff, retries + 1); return new ServiceBusQueue<SimpleWorkItem>(ConnectionStrings.Get("ServiceBusConnectionString"), QueueName, retries, workItemTimeout, false, retryPolicy); }
protected override IQueue<SimpleWorkItem> GetQueue(int retries = 1, TimeSpan? workItemTimeout = null, TimeSpan? retryDelay = null, int deadLetterMaxItems = 100, bool runQueueMaintenance = true) { if (String.IsNullOrEmpty(Configuration.GetConnectionString("ServiceBusConnectionString"))) return null; if (!retryDelay.HasValue) retryDelay = TimeSpan.Zero; var maxBackoff = retryDelay.Value > TimeSpan.Zero ? retryDelay.Value + retryDelay.Value : TimeSpan.FromSeconds(1); var retryPolicy = new RetryExponential(retryDelay.Value, maxBackoff, retries + 1); _logger.Debug("Queue Id: {queueId}", _queueName); return new AzureServiceBusQueue<SimpleWorkItem>(Configuration.GetConnectionString("ServiceBusConnectionString"), _queueName, retries, workItemTimeout, retryPolicy, loggerFactory: Log); }
static async Task MainAsync() { var retryPolicy = new RetryExponential(TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(30), 3); queueClient = new QueueClient(ServiceBusConnectionString, QueueName, ReceiveMode.PeekLock, retryPolicy); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); RegisterOnMessageHandlerAndReceiveMessages(); Console.ReadKey(); await queueClient.CloseAsync(); }
private void InitializeSubscriptionClient(string subscriptionName) { var exponentialRetry = new RetryExponential( minimumBackoff: TimeSpan.FromSeconds(_serviceBusConfiguration.SbMinimumAllowedBackoffTime), maximumBackoff: TimeSpan.FromSeconds(_serviceBusConfiguration.SbMaximumAllowedBackoffTime), _serviceBusConfiguration.SbMaximumAllowedRetries); if (_serviceBusConfiguration.UseManagedIdentity) { var tokenProvider = Microsoft.Azure.ServiceBus.Primitives.TokenProvider.CreateManagedIdentityTokenProvider(); _subscriptionClient = new SubscriptionClient(_serviceBusConfiguration.EndpointURI, _serviceBusConfiguration.EntityPath, subscriptionName, tokenProvider, retryPolicy: exponentialRetry); } else { _subscriptionClient = new SubscriptionClient(_serviceBusConfiguration.ConnectionString, _serviceBusConfiguration.EntityPath, subscriptionName, retryPolicy: exponentialRetry); } }
public MessageBroker(string channel, IServiceProvider services, ChannelType channelType) { var configuration = services.GetRequiredService <IConfiguration>(); _serializer = services.GetRequiredService <MessageSerializer <TMessageType> >(); _version = configuration.GetChangeSetIdentifier(); var channelName = configuration.GetServiceBusChannelName(channelType, channel); var connectionString = configuration.GetServiceBusConnectionString(); var retryPolicy = new RetryExponential(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 10); switch (channelType) { case ChannelType.PublishSubscribe: _senderClient = new TopicClient(connectionString, channelName, retryPolicy); break; case ChannelType.PointToPoint: _senderClient = new QueueClient(connectionString, channelName, ReceiveMode.PeekLock, retryPolicy); break; } }
public CreyMessageBroker(string channel, IServiceProvider services, ChannelType channelType) { var configuration = services.GetRequiredService <IConfiguration>(); var retryPolicy = new RetryExponential(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 10); var connectionString = configuration.GetServiceBusConnectionString(); var channelName = configuration.GetChannelName(channel); _senderClient = channelType switch { ChannelType.PublishSubscribe => new TopicClient(connectionString, channelName, retryPolicy), ChannelType.PointToPoint => new QueueClient(connectionString, channelName, ReceiveMode.PeekLock, retryPolicy), _ => throw new Exception($"Unkown channel type {channelType}") }; _sessionInfo = services.GetRequiredService <SessionInfoStore>(); _version = configuration.GetValue <string>(ChangeSetIdentifier); }
protected override IQueue <SimpleWorkItem> GetQueue(int retries = 1, TimeSpan?workItemTimeout = null, TimeSpan?retryDelay = null, int deadLetterMaxItems = 100, bool runQueueMaintenance = true) { if (String.IsNullOrEmpty(Configuration.GetConnectionString("ServiceBusConnectionString"))) { return(null); } if (!retryDelay.HasValue) { retryDelay = TimeSpan.Zero; } var maxBackoff = retryDelay.Value > TimeSpan.Zero ? retryDelay.Value + retryDelay.Value : TimeSpan.FromSeconds(1); var retryPolicy = new RetryExponential(retryDelay.Value, maxBackoff, retries + 1); return(new AzureServiceBusQueue <SimpleWorkItem>(Configuration.GetConnectionString("ServiceBusConnectionString"), _queueName, retries, workItemTimeout, retryPolicy, loggerFactory: Log)); }
protected override IQueue <SimpleWorkItem> GetQueue(int retries = 1, TimeSpan?workItemTimeout = null, TimeSpan?retryDelay = null, int deadLetterMaxItems = 100, bool runQueueMaintenance = true) { if (ConnectionStrings.Get("ServiceBusConnectionString") == null) { return(null); } if (!retryDelay.HasValue) { retryDelay = TimeSpan.Zero; } var maxBackoff = retryDelay.Value > TimeSpan.Zero ? retryDelay.Value + retryDelay.Value : TimeSpan.FromSeconds(1); var retryPolicy = new RetryExponential(retryDelay.Value, maxBackoff, retries + 1); return(new ServiceBusQueue <SimpleWorkItem>(ConnectionStrings.Get("ServiceBusConnectionString"), QueueName, retries, workItemTimeout, false, retryPolicy)); }
public ISenderClient GetDataSender(ClientType type, string endpoint, string path) { var policy = new RetryExponential(TimeSpan.Zero, TimeSpan.FromSeconds(30), 5); ISenderClient client; switch (type) { case ClientType.Queue: client = new QueueClient(endpoint, path, createTokenProvider(), TransportType.Amqp, ReceiveMode.PeekLock, policy); break; case ClientType.Topic: client = new TopicClient(endpoint, path, createTokenProvider(), TransportType.Amqp, policy); break; default: throw new ArgumentException("Unknown type"); } return(client); }
void RetryExponentialRetryIntervalShouldIncreaseTest() { RetryExponential policy = (RetryExponential)RetryPolicy.Default; bool retry = true; int retryCount = 0; TimeSpan duration = Constants.DefaultOperationTimeout; TimeSpan lastRetryInterval = TimeSpan.Zero; ServiceBusException exception = new ServiceBusException(true, string.Empty); while (retry) { TimeSpan retryInterval; retry = policy.ShouldRetry(duration, retryCount, exception, out retryInterval); if (retry) { Assert.True(retryInterval >= lastRetryInterval, $"Retry sleep should not decrease. Retry = [{retryInterval}]"); retryCount++; lastRetryInterval = retryInterval; } } }
public async Task CanSendAndReceiveMessage() { var queueName = TestConfig.GetName("send-receive"); await _managementClient.CreateQueueIfNotExistsAsync(queueName); var retryPolicy = new RetryExponential(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5), 10); var queueClient = new QueueClient(ConnectionString, queueName, receiveMode: ReceiveMode.PeekLock, retryPolicy: retryPolicy); await ManagementExtensions.PurgeQueue(ConnectionString, queueName); await queueClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hej med dig min ven! Det spiller!"))); var messageReceiver = new MessageReceiver(ConnectionString, queueName, receiveMode: ReceiveMode.PeekLock); var message = await messageReceiver.ReceiveAsync(TimeSpan.FromSeconds(2)); Assert.That(message, Is.Not.Null); var text = Encoding.UTF8.GetString(message.Body); Assert.That(text, Is.EqualTo("Hej med dig min ven! Det spiller!")); }
internal Binding( AzureBusTopicSettings settings, AzureBusTopicManagement queueManagement, string topic, Type type, ILogger <AzureTopicPublisher> logger) { _logger = logger; _settings = settings; _topic = topic; _excludeTopicsFromLogging = new LoggingConfiguration().ExcludeTopicsFromLogging(); queueManagement.CreateTopicIfMissing(_topic, type); var retryPolicy = new RetryExponential( TimeSpan.FromSeconds(settings.AzureRetryMinimumBackoff), TimeSpan.FromSeconds(settings.AzureRetryMaximumBackoff), settings.AzureMaximumRetryCount ); _topicClient = new TopicClient(settings.ConnectionString, _topic, retryPolicy); _logger.LogInformation("Created new MQ binding '{topic}'.", _topic); }