private static async Task DeployDataWorkerQueue(TextWriter writer, bool recreate = false) { // "dumpling-service data-worker queue path" // "dumpling-service bus connection string" var dataworkerQueuePath = NearbyConfig.Settings["dumpling-service data-worker queue path"]; var servicebusConnString = NearbyConfig.Settings["dumpling-service bus connection string"]; NamespaceManager manager = NamespaceManager.CreateFromConnectionString(servicebusConnString); CreationDelegate createQueue = async() => { writer.WriteLine("creating new dataworker queue"); await manager.CreateQueueAsync(dataworkerQueuePath); }; var exists = await manager.QueueExistsAsync(dataworkerQueuePath); if (exists && recreate) { writer.WriteLine("dataworker queue exists; deleting current instance"); await manager.DeleteQueueAsync(dataworkerQueuePath); createQueue(); } else if (!exists) { createQueue(); } else { writer.WriteLine($"'{dataworkerQueuePath}' exists already and we're not recreating"); } }
protected override async Task EnsureQueueCreatedAsync(CancellationToken cancellationToken = new CancellationToken()) { if (QueueIsCreated) { return; } using (await _lock.LockAsync().AnyContext()) { if (QueueIsCreated) { return; } var sw = Stopwatch.StartNew(); try { await _namespaceManager.CreateQueueAsync(CreateQueueDescription()).AnyContext(); } catch (MessagingEntityAlreadyExistsException) { } _queueClient = QueueClient.CreateFromConnectionString(_options.ConnectionString, _options.Name); if (_options.RetryPolicy != null) { _queueClient.RetryPolicy = _options.RetryPolicy; } sw.Stop(); _logger.LogTrace("Ensure queue exists took {0}ms.", sw.ElapsedMilliseconds); } }
async Task CreateQueue(NamespaceManager namespaceManager, Uri serviceUri, string queueName) { if (namespaceManager == null) { throw new TransportException(serviceUri, "The namespace manager is not available"); } var description = new QueueDescription(queueName) { DefaultMessageTimeToLive = TimeSpan.FromDays(365), EnableBatchedOperations = true, LockDuration = TimeSpan.FromMinutes(5), MaxDeliveryCount = 5, EnableDeadLetteringOnMessageExpiration = true, DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(10) }; try { var queueDescription = await namespaceManager.CreateQueueAsync(description); } catch (MessagingEntityAlreadyExistsException) { } }
public async Task CreateQueueIfNotExists(string queueName) { if (!_namespaceManager.QueueExists(queueName)) { await _namespaceManager.CreateQueueAsync(queueName); } }
async Task <IEnumerable <QueueDescription> > SetupSagaTopologyAsync(NamespaceManager nm) { Console.WriteLine("Setup"); return(new List <QueueDescription> { await nm.QueueExistsAsync(SagaResultQueueName) ? await nm.GetQueueAsync(SagaResultQueueName) : await nm.CreateQueueAsync(SagaResultQueueName), await nm.QueueExistsAsync(CancelFlightQueueName) ? await nm.GetQueueAsync(CancelFlightQueueName) : await nm.CreateQueueAsync(new QueueDescription(CancelFlightQueueName)), await nm.QueueExistsAsync(BookFlightQueueName) ? await nm.GetQueueAsync(BookFlightQueueName) : await nm.CreateQueueAsync( new QueueDescription(BookFlightQueueName) { // on failure, we move deadletter messages off to the flight // booking compensator's queue EnableDeadLetteringOnMessageExpiration = true, ForwardDeadLetteredMessagesTo = CancelFlightQueueName }), await nm.QueueExistsAsync(CancelHotelQueueName) ? await nm.GetQueueAsync(CancelHotelQueueName) : await nm.CreateQueueAsync(new QueueDescription(CancelHotelQueueName)), await nm.QueueExistsAsync(BookHotelQueueName) ? await nm.GetQueueAsync(BookHotelQueueName) : await nm.CreateQueueAsync( new QueueDescription(BookHotelQueueName) { // on failure, we move deadletter messages off to the hotel // booking compensator's queue EnableDeadLetteringOnMessageExpiration = true, ForwardDeadLetteredMessagesTo = CancelHotelQueueName }), await nm.QueueExistsAsync(CancelRentalCarQueueName) ? await nm.GetQueueAsync(CancelRentalCarQueueName) : await nm.CreateQueueAsync(new QueueDescription(CancelRentalCarQueueName)), await nm.QueueExistsAsync(BookRentalCarQueueName) ? await nm.GetQueueAsync(BookRentalCarQueueName) : await nm.CreateQueueAsync( new QueueDescription(BookRentalCarQueueName) { // on failure, we move deadletter messages off to the car rental // compensator's queue EnableDeadLetteringOnMessageExpiration = true, ForwardDeadLetteredMessagesTo = CancelRentalCarQueueName }), await nm.QueueExistsAsync(SagaInputQueueName) ? await nm.GetQueueAsync(SagaInputQueueName) : await nm.CreateQueueAsync( new QueueDescription(SagaInputQueueName) { // book car is the first step ForwardTo = BookRentalCarQueueName }) }); }
private async Task <QueueClient> GetQueueClient(string queueName) { if (!string.IsNullOrEmpty(queueName) && !_namespaceManager.QueueExists(queueName)) { await _namespaceManager.CreateQueueAsync(queueName); } return(QueueClient.CreateFromConnectionString(_serviceBusConnectionString, queueName)); }
public async Task <QueueClient> GetQueueClientAsync(string queueName) { if (!_namespaceManager.QueueExists(queueName)) { await _namespaceManager.CreateQueueAsync(queueName).ConfigureAwait(false); } return(QueueClient.CreateFromConnectionString(_serviceBusConnectionString, queueName)); }
public async Task <QueueDescription> CreateQueue(QueueDescription queueDescription) { var create = true; try { queueDescription = await _namespaceManager.GetQueueAsync(queueDescription.Path).ConfigureAwait(false); create = false; } catch (MessagingEntityNotFoundException) { } if (create) { var created = false; try { LogContext.Debug?.Log("Creating queue {Queue}", queueDescription.Path); queueDescription = await _namespaceManager.CreateQueueAsync(queueDescription).ConfigureAwait(false); created = true; } catch (MessagingEntityAlreadyExistsException) { } catch (MessagingException mex) { if (mex.Message.Contains("(409)")) { } else { throw; } } if (!created) { queueDescription = await _namespaceManager.GetQueueAsync(queueDescription.Path).ConfigureAwait(false); } } LogContext.Debug?.Log("Queue: {Queue} ({Attributes})", queueDescription.Path, string.Join(", ", new[] { queueDescription.EnableExpress ? "express" : "", queueDescription.RequiresDuplicateDetection ? "dupe detect" : "", queueDescription.EnableDeadLetteringOnMessageExpiration ? "dead letter" : "", queueDescription.RequiresSession ? "session" : "" }.Where(x => !string.IsNullOrWhiteSpace(x)))); return(queueDescription); }
private async static Task QueueCreateAsync(NamespaceManager ns, QueueDescription queueDescription) { if (!await ns.QueueExistsAsync(queueDescription.Path)) { await ns.CreateQueueAsync(queueDescription); ServiceBusEventSource.Log.CreatedQueue(ns.Address.ToString(), queueDescription.Path); } }
private async static Task QueueCreateAsync(NamespaceManager ns, QueueDescription queueDescription) { if (!await ns.QueueExistsAsync(queueDescription.Path).ConfigureAwait(false)) { await ns.CreateQueueAsync(queueDescription) .ConfigureAwait(false); ServiceBusEventSource.Log.CreatedQueue(ns.Address.ToString(), queueDescription.Path); } }
public async Task ServiceBus_Node_DoesNotExhaustConnections() { var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsServiceBus"); NamespaceManager manager = NamespaceManager.CreateFromConnectionString(connectionString); // Start with an empty queue await manager.DeleteQueueAsync("node"); // Pre-create the queue as we can end up with 409s if a bunch of requests // try to create the queue at once await manager.CreateQueueAsync("node"); int i = 0, j = 0, lastConnectionCount = 0, lastConnectionLimit = 0; using (var client = CreateClient()) { // make this longer as we'll start seeing long timeouts from Service Bus upon failure. client.Timeout = TimeSpan.FromMinutes(5); // max connections in dynamic is currently 300 for (i = 0; i < 25; i++) { List <Task <HttpResponseMessage> > requestTasks = new List <Task <HttpResponseMessage> >(); for (j = 0; j < 25; j++) { requestTasks.Add(client.GetAsync($"api/ServiceBusNode?code={_fixture.FunctionDefaultKey}")); } await Task.WhenAll(requestTasks); foreach (var requestTask in requestTasks) { HttpResponseMessage response = await requestTask; JObject result = await response.Content.ReadAsAsync <JObject>(); if (response.IsSuccessStatusCode) { // store these off for error details lastConnectionCount = (int)result["connections"]; lastConnectionLimit = (int)result["connectionLimit"]; // make sure we have the correct limit Assert.Equal(300, lastConnectionLimit); } Assert.True(response.IsSuccessStatusCode, $"Error: {response.StatusCode}, Last successful response: Connections: {lastConnectionCount}, ConnectionLimit: {lastConnectionLimit}"); } } } QueueDescription queueDescription = manager.GetQueue("node"); Assert.Equal(i * j, queueDescription.MessageCountDetails.ActiveMessageCount); }
private async Task CreateQueueIfNotExists() { bool queueExists = await _namespaceManager.QueueExistsAsync(_queueName); if (queueExists == false) { var queueDescription = new QueueDescription(_queueName); queueDescription.EnablePartitioning = false; await _namespaceManager.CreateQueueAsync(queueDescription); } }
private async Task <QueueDescription> GetOrCreateQueueDescriptionAsync() { try { return(await _namespaceManager.GetQueueAsync(_path).ConfigureAwait(false)); } catch (MessagingEntityNotFoundException) { return(await _namespaceManager.CreateQueueAsync(_path).ConfigureAwait(false)); } }
public static async Task SendAndCreateQueueIfNotExistsAsync(this MessageSender sender, BrokeredMessage message, Guid functionInstanceId, NamespaceManager namespaceManager, AccessRights accessRights, CancellationToken cancellationToken) { if (sender == null) { throw new ArgumentNullException("sender"); } else if (namespaceManager == null) { throw new ArgumentNullException("namespaceManager"); } ServiceBusCausalityHelper.EncodePayload(functionInstanceId, message); bool threwMessgingEntityNotFoundException = false; cancellationToken.ThrowIfCancellationRequested(); try { await sender.SendAsync(message); return; } catch (MessagingEntityNotFoundException) { if (accessRights != AccessRights.Manage) { // if we don't have the required rights to create the queue, // rethrow the exception throw; } threwMessgingEntityNotFoundException = true; } Debug.Assert(threwMessgingEntityNotFoundException); cancellationToken.ThrowIfCancellationRequested(); try { await namespaceManager.CreateQueueAsync(sender.Path); } catch (MessagingEntityAlreadyExistsException) { } // Clone the message because it was already consumed before (when trying to send) // otherwise, you get an exception message = message.Clone(); cancellationToken.ThrowIfCancellationRequested(); await sender.SendAsync(message); }
/// <summary> /// Create a Message Queue, if it does not already exist, and indicate if the attempt was successful. /// </summary> public bool CreateQueue() { string queueName = EnvironmentType.MessageQueueName(); bool queueExists = QueueExists; if (!queueExists) { _namespaceManager.CreateQueueAsync(queueName).Wait(); queueExists = QueueExists; } return(queueExists); }
private async Task CreateCleansedNameQueueAsync(string cleansedName, bool isSessionRequired) { var qd = new QueueDescription(cleansedName); qd.RequiresSession = isSessionRequired; qd.EnableDeadLetteringOnMessageExpiration = true; qd.RequiresDuplicateDetection = true; if (!_manager.QueueExists(cleansedName)) { await _manager.CreateQueueAsync(qd); } }
private static async Task EnsureQueueExists(NamespaceManager namespaceManager, string queueName) { var queueExists = await namespaceManager.QueueExistsAsync(queueName); if (!queueExists) { await namespaceManager.CreateQueueAsync( new QueueDescription(queueName) { SupportOrdering = false }); } }
private async Task CreateResponseQueue() { NamespaceManager manager = NamespaceManager.CreateFromConnectionString(ConfigurationManager.AppSettings[Helpers.ConnectionStringKey]); if (!manager.QueueExists(Helpers.ResponseQueue)) { QueueDescription queueDescription = new QueueDescription(Helpers.ResponseQueue) { RequiresSession = true }; await manager.CreateQueueAsync(queueDescription); } }
private async Task EnsureReceiverExists(string path, string subscription, string filter = null) { Trace(); try { switch (string.IsNullOrEmpty(subscription)) { case true: if (!await NamespaceManager.QueueExistsAsync(path)) { await NamespaceManager.CreateQueueAsync(path); } QueueReceiver = MessagingFactory.CreateQueueClient(path, ReceiveMode.ReceiveAndDelete); break; case false: if (!await NamespaceManager.TopicExistsAsync(path)) { await NamespaceManager.CreateTopicAsync(path); } //recreate subscription if necessary if (await NamespaceManager.SubscriptionExistsAsync(path, subscription)) { await NamespaceManager.DeleteSubscriptionAsync(path, subscription); } if (!await NamespaceManager.SubscriptionExistsAsync(path, subscription)) { if (!string.IsNullOrEmpty(filter)) { SqlFilter sf = new SqlFilter(filter); await NamespaceManager.CreateSubscriptionAsync(path, subscription, sf); } else { await NamespaceManager.CreateSubscriptionAsync(path, subscription); } } TopicReceiver = MessagingFactory.CreateSubscriptionClient(path, subscription, ReceiveMode.ReceiveAndDelete); break; } } catch (Exception e) { Error(e.ToString()); } }
/// <summary> /// Create If Not Exists /// </summary> /// <returns></returns> public virtual async Task <bool> CreateIfNotExists() { var created = false; if (!manager.QueueExists(name)) { var qd = new QueueDescription(name) { EnablePartitioning = true, }; await manager.CreateQueueAsync(qd); created = true; } return(created); }
public static async Task CreateQueueIfNotExistsAsync(this NamespaceManager manager, string path, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); string parentQueuePath = SplitQueuePath(path)[0]; if (!await manager.QueueExistsAsync(parentQueuePath)) { try { cancellationToken.ThrowIfCancellationRequested(); await manager.CreateQueueAsync(parentQueuePath); } catch (MessagingEntityAlreadyExistsException) { } } }
public Task CreateQueueAsync(QueueName name) { if (name.IsSimpleQueue) { return(_namespaceManager.CreateQueueAsync(name.TopicName)); } else { if (name.IsTopic) { return(_namespaceManager.CreateTopicAsync(name.TopicName)); } else { return(_namespaceManager.CreateSubscriptionAsync(name.TopicName, name.SubscriptionName)); } } }
public override async Task DeleteQueueAsync() { if (await _namespaceManager.QueueExistsAsync(_queueName).AnyContext()) { await _namespaceManager.DeleteQueueAsync(_queueName).AnyContext(); } _queueDescription = new QueueDescription(_queueName) { MaxDeliveryCount = _retries + 1, LockDuration = _workItemTimeout }; await _namespaceManager.CreateQueueAsync(_queueDescription).AnyContext(); _enqueuedCount = 0; _dequeuedCount = 0; _completedCount = 0; _abandonedCount = 0; _workerErrorCount = 0; }
private async Task SafeCreateQueueAsync(string name) { if (await NamespaceManager.QueueExistsAsync(name)) { return; } try { // Configure Queue Settings var qd = new QueueDescription(name); await RetryPolicy.ExecuteAsync(async() => await NamespaceManager.CreateQueueAsync(qd)); } catch (Exception) { if (NamespaceManager.QueueExists(name)) { return; } throw; } }
/// <summary> /// This is the main entry point for your service instance. /// </summary> /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param> protected override async Task RunAsync(CancellationToken cancellationToken) { //Setup NamespaceManager manager = NamespaceManager.CreateFromConnectionString("..."); try { await manager.CreateQueueAsync("qtkcart2016"); } catch (Exception ex) { } QueueClient queue = QueueClient.CreateFromConnectionString("...", "qtkcart2016"); while (true) { var msg = await queue.ReceiveAsync(); if (msg != null) { //Do Something - save etc ServiceEventSource.Current.Message($"{this.Context.InstanceId} - {msg.MessageId}"); await msg.CompleteAsync(); } } }
public static async Task <QueueDescription> CreateQueueSafeAsync(this NamespaceManager namespaceManager, QueueDescription queueDescription) { bool create = true; try { queueDescription = await namespaceManager.GetQueueAsync(queueDescription.Path).ConfigureAwait(false); create = false; } catch (MessagingEntityNotFoundException) { } if (create) { bool created = false; try { if (_log.IsDebugEnabled) { _log.DebugFormat("Creating queue {0}", queueDescription.Path); } queueDescription = await namespaceManager.CreateQueueAsync(queueDescription).ConfigureAwait(false); created = true; } catch (MessagingEntityAlreadyExistsException) { } catch (MessagingException mex) { if (mex.Message.Contains("(409)")) { } else { throw; } } if (!created) { queueDescription = await namespaceManager.GetQueueAsync(queueDescription.Path).ConfigureAwait(false); } } if (_log.IsDebugEnabled) { _log.DebugFormat("Queue: {0} ({1})", queueDescription.Path, string.Join(", ", new[] { queueDescription.EnableExpress ? "express" : "", queueDescription.RequiresDuplicateDetection ? "dupe detect" : "", queueDescription.EnableDeadLetteringOnMessageExpiration ? "dead letter" : "" }.Where(x => !string.IsNullOrWhiteSpace(x)))); } return(queueDescription); }
protected override async Task EnsureQueueCreatedAsync(CancellationToken cancellationToken = new CancellationToken()) { if (_queueClient != null) { return; } using (await _lock.LockAsync(cancellationToken).AnyContext()) { if (_queueClient != null) { return; } QueueDescription queueDescription; if (!await _namespaceManager.QueueExistsAsync(_queueName).AnyContext()) { try { queueDescription = await _namespaceManager.CreateQueueAsync(new QueueDescription(_queueName) { MaxDeliveryCount = _retries + 1, LockDuration = _workItemTimeout, AutoDeleteOnIdle = _autoDeleteOnIdle, DefaultMessageTimeToLive = _defaultMessageTimeToLive }).AnyContext(); } catch (MessagingException) { queueDescription = await _namespaceManager.GetQueueAsync(_queueName).AnyContext(); } } else { queueDescription = await _namespaceManager.GetQueueAsync(_queueName).AnyContext(); bool changes = false; int newMaxDeliveryCount = _retries + 1; if (queueDescription.MaxDeliveryCount != newMaxDeliveryCount) { queueDescription.MaxDeliveryCount = newMaxDeliveryCount; changes = true; } if (queueDescription.LockDuration != _workItemTimeout) { queueDescription.LockDuration = _workItemTimeout; changes = true; } if (queueDescription.AutoDeleteOnIdle != _autoDeleteOnIdle) { queueDescription.AutoDeleteOnIdle = _autoDeleteOnIdle; changes = true; } if (queueDescription.DefaultMessageTimeToLive != _defaultMessageTimeToLive) { queueDescription.DefaultMessageTimeToLive = _defaultMessageTimeToLive; changes = true; } if (changes) { await _namespaceManager.UpdateQueueAsync(queueDescription).AnyContext(); } } _queueClient = QueueClient.CreateFromConnectionString(_connectionString, queueDescription.Path); if (_retryPolicy != null) { _queueClient.RetryPolicy = _retryPolicy; } } }
public Task CreateQueue(QueueDescription description) { return(manager.CreateQueueAsync(description)); }
public async Task Run(string namespaceAddress, string manageToken) { Console.WriteLine("\nCreating topology\n"); this.sharedAccessRuleKey = SharedAccessAuthorizationRule.GenerateRandomKey(); var namespaceManageTokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken); // Create namespace manager and create destination queue with a SAS rule that allows sending to that queue. var namespaceManager = new NamespaceManager(namespaceAddress, namespaceManageTokenProvider); var targetQueue = new QueueDescription("TargetQueue") { Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) }, }; targetQueue = (await namespaceManager.QueueExistsAsync(targetQueue.Path)) ? await namespaceManager.UpdateQueueAsync(targetQueue) : await namespaceManager.CreateQueueAsync(targetQueue); var topic = new TopicDescription("SourceTopic") { Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) } }; topic = (await namespaceManager.TopicExistsAsync(topic.Path)) ? await namespaceManager.UpdateTopicAsync(topic) : await namespaceManager.CreateTopicAsync(topic); var forwardingSubscription = namespaceManager.CreateSubscription( new SubscriptionDescription(topic.Path, "Sub1") { ForwardTo = targetQueue.Path }); var forwardingQueue = new QueueDescription("SourceQueue") { ForwardTo = targetQueue.Path, Authorization = { new SharedAccessAuthorizationRule( "SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) } }; forwardingQueue = (await namespaceManager.QueueExistsAsync(forwardingQueue.Path)) ? await namespaceManager.UpdateQueueAsync(forwardingQueue) : await namespaceManager.CreateQueueAsync(forwardingQueue); Console.WriteLine("\nSending messages\n"); var topicFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey)); var topicSender = await topicFactory.CreateMessageSenderAsync(topic.Path); await topicSender.SendAsync(CreateMessage("M1")); var queueFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey)); var queueSender = await queueFactory.CreateMessageSenderAsync(forwardingQueue.Path); await queueSender.SendAsync(CreateMessage("M1")); var messagingFactory = MessagingFactory.Create(namespaceAddress, namespaceManageTokenProvider); var targetQueueReceiver = messagingFactory.CreateQueueClient(targetQueue.Path); while (true) { var message = await targetQueueReceiver.ReceiveAsync(TimeSpan.FromSeconds(10)); if (message != null) { await this.PrintReceivedMessage(message); await message.CompleteAsync(); } else { break; } } await targetQueueReceiver.CloseAsync(); Console.WriteLine("\nPress ENTER to delete topics and exit\n"); Console.ReadLine(); messagingFactory.Close(); Task.WaitAll( namespaceManager.DeleteQueueAsync(targetQueue.Path), namespaceManager.DeleteQueueAsync(forwardingQueue.Path), namespaceManager.DeleteTopicAsync(topic.Path)); }
public async Task Run(string namespaceAddress, string manageToken) { Console.WriteLine("\nCreating topology\n"); this.sharedAccessRuleKey = SharedAccessAuthorizationRule.GenerateRandomKey(); var namespaceManageTokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken); // Create namespace manager and create destination queue with a SAS rule that allows sending to that queue. var namespaceManager = new NamespaceManager(namespaceAddress, namespaceManageTokenProvider); var targetQueue = new QueueDescription("TargetQueue") { Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) }, }; targetQueue = (await namespaceManager.QueueExistsAsync(targetQueue.Path)) ? await namespaceManager.UpdateQueueAsync(targetQueue) : await namespaceManager.CreateQueueAsync(targetQueue); var topic = new TopicDescription("SourceTopic") { Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) } }; topic = (await namespaceManager.TopicExistsAsync(topic.Path)) ? await namespaceManager.UpdateTopicAsync(topic) : await namespaceManager.CreateTopicAsync(topic); var forwardingSubscription = namespaceManager.CreateSubscription( new SubscriptionDescription(topic.Path, "Sub1") { ForwardTo = targetQueue.Path }); var forwardingQueue = new QueueDescription("SourceQueue") { ForwardTo = targetQueue.Path, Authorization = { new SharedAccessAuthorizationRule( "SendKey", this.sharedAccessRuleKey, new[] {AccessRights.Send}) } }; forwardingQueue = (await namespaceManager.QueueExistsAsync(forwardingQueue.Path)) ? await namespaceManager.UpdateQueueAsync(forwardingQueue) : await namespaceManager.CreateQueueAsync(forwardingQueue); Console.WriteLine("\nSending messages\n"); var topicFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey)); var topicSender = await topicFactory.CreateMessageSenderAsync(topic.Path); await topicSender.SendAsync(CreateMessage("M1")); var queueFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey)); var queueSender = await queueFactory.CreateMessageSenderAsync(forwardingQueue.Path); await queueSender.SendAsync(CreateMessage("M1")); var messagingFactory = MessagingFactory.Create(namespaceAddress, namespaceManageTokenProvider); var targetQueueReceiver = messagingFactory.CreateQueueClient(targetQueue.Path); while (true) { var message = await targetQueueReceiver.ReceiveAsync(TimeSpan.FromSeconds(10)); if (message != null) { await this.PrintReceivedMessage(message); await message.CompleteAsync(); } else { break; } } await targetQueueReceiver.CloseAsync(); Console.WriteLine("\nPress ENTER to delete topics and exit\n"); Console.ReadLine(); messagingFactory.Close(); Task.WaitAll( namespaceManager.DeleteQueueAsync(targetQueue.Path), namespaceManager.DeleteQueueAsync(forwardingQueue.Path), namespaceManager.DeleteTopicAsync(topic.Path)); }
public async Task SetupAsync(Type[] allMessageTypes, Type[] recievingMessageTypes) { _logger.Debug("Starting the setup of AzureServicebusTransport..."); _namespaceManager = NamespaceManager.CreateFromConnectionString(_configuration.GetConnectionString()); _messagingFactory = MessagingFactory.CreateFromConnectionString(_configuration.GetConnectionString()); _messageTypes = allMessageTypes; var sendCommandTypes = _messageTypes .Where(t => typeof (IBusCommand) .IsAssignableFrom(t)); var sendEventTypes = _messageTypes .Where(t => typeof(IBusEvent) .IsAssignableFrom(t)); var recieveCommandTypes = recievingMessageTypes .Where(t => typeof(IBusCommand) .IsAssignableFrom(t)); var recieveEventTypes = recievingMessageTypes .Where(t => typeof(IBusEvent) .IsAssignableFrom(t)); if (_configuration.GetEnableTopicAndQueueCreation()) { foreach (var type in sendCommandTypes) { var path = PathFactory.QueuePathFor(type); if (!_namespaceManager.QueueExists(path)) await _namespaceManager.CreateQueueAsync(path); var client = _messagingFactory.CreateQueueClient(path); client.PrefetchCount = 10; //todo;: in config? var eventDrivenMessagingOptions = new OnMessageOptions { AutoComplete = true, //todo: in config? MaxConcurrentCalls = 10 //todo: in config? }; eventDrivenMessagingOptions.ExceptionReceived += OnExceptionReceived; client.OnMessageAsync(OnMessageRecieved, eventDrivenMessagingOptions); if (!_queues.TryAdd(type, client)) { _logger.Error("Could not add the queue with type: {0}", type.FullName); } } foreach (var type in sendEventTypes) { var path = PathFactory.TopicPathFor(type); if (!_namespaceManager.TopicExists(path)) _namespaceManager.CreateTopic(path); var client = _messagingFactory.CreateTopicClient(path); if (!_topics.TryAdd(type, client)) { _logger.Error("Could not add the topic with type: {0}", type.FullName); } } } _logger.Debug("Setup of AzureServicebusTransport completed!"); throw new NotImplementedException(); }
async Task<IEnumerable<QueueDescription>> SetupSagaTopologyAsync(NamespaceManager nm) { Console.WriteLine("Setup"); return new List<QueueDescription> { await nm.QueueExistsAsync(SagaResultQueueName) ? await nm.GetQueueAsync(SagaResultQueueName) : await nm.CreateQueueAsync(SagaResultQueueName), await nm.QueueExistsAsync(CancelFlightQueueName) ? await nm.GetQueueAsync(CancelFlightQueueName) : await nm.CreateQueueAsync(new QueueDescription(CancelFlightQueueName)), await nm.QueueExistsAsync(BookFlightQueueName) ? await nm.GetQueueAsync(BookFlightQueueName) : await nm.CreateQueueAsync( new QueueDescription(BookFlightQueueName) { // on failure, we move deadletter messages off to the flight // booking compensator's queue EnableDeadLetteringOnMessageExpiration = true, ForwardDeadLetteredMessagesTo = CancelFlightQueueName }), await nm.QueueExistsAsync(CancelHotelQueueName) ? await nm.GetQueueAsync(CancelHotelQueueName) : await nm.CreateQueueAsync(new QueueDescription(CancelHotelQueueName)), await nm.QueueExistsAsync(BookHotelQueueName) ? await nm.GetQueueAsync(BookHotelQueueName) : await nm.CreateQueueAsync( new QueueDescription(BookHotelQueueName) { // on failure, we move deadletter messages off to the hotel // booking compensator's queue EnableDeadLetteringOnMessageExpiration = true, ForwardDeadLetteredMessagesTo = CancelHotelQueueName }), await nm.QueueExistsAsync(CancelRentalCarQueueName) ? await nm.GetQueueAsync(CancelRentalCarQueueName) : await nm.CreateQueueAsync(new QueueDescription(CancelRentalCarQueueName)), await nm.QueueExistsAsync(BookRentalCarQueueName) ? await nm.GetQueueAsync(BookRentalCarQueueName) : await nm.CreateQueueAsync( new QueueDescription(BookRentalCarQueueName) { // on failure, we move deadletter messages off to the car rental // compensator's queue EnableDeadLetteringOnMessageExpiration = true, ForwardDeadLetteredMessagesTo = CancelRentalCarQueueName }), await nm.QueueExistsAsync(SagaInputQueueName) ? await nm.GetQueueAsync(SagaInputQueueName) : await nm.CreateQueueAsync( new QueueDescription(SagaInputQueueName) { // book car is the first step ForwardTo = BookRentalCarQueueName }) }; }