protected QueueProcessor(string subscription) { subscription = subscription.ToLower(); subscriptions = subscriptions.Append(subscription).ToArray(); var xexecutionThread = Web.Configuration.Settings.GetString( EastFive.Security.SessionServer.Configuration.AppSettings.ServiceBusConnectionString, serviceBusConnectionString => { var receiveClient = new Microsoft.Azure.ServiceBus.QueueClient(serviceBusConnectionString, subscription); // TODO: Create the topic if it does not exist already but swallow the errors if manage privilige is not available Func <Microsoft.Azure.ServiceBus.Message, CancellationToken, Task> processMessagesAsync = async(message, cancellationToken) => { try { processing = true; await ProcessAsync(message, receiveClient); } catch (Exception ex) { ex.Message.GetType(); } finally { processing = false; } }; var messageHandlerOptions = new Microsoft.Azure.ServiceBus.MessageHandlerOptions(ExceptionReceivedHandler) { // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity. // Set it according to how many messages the application wants to process in parallel. MaxConcurrentCalls = 1, // Indicates whether the message pump should automatically complete the messages after returning from user callback. // False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync(). AutoComplete = false, MaxAutoRenewDuration = TimeSpan.FromHours(1), }; // Register the function that processes messages. receiveClient.RegisterMessageHandler(processMessagesAsync, messageHandlerOptions); return(string.Empty); }, (why) => why); }
public MessageProcessor(Microsoft.Azure.ServiceBus.Core.MessageReceiver messageReceiver, Microsoft.Azure.ServiceBus.MessageHandlerOptions messageOptions) { }
public void ReceiveMessages(Func <InitiativeCreatedEventArgs, CancellationToken, Task> initiativeCreatedHandler = null, Func <WorkOrderCreatedEventArgs, CancellationToken, Task> workOrderCreatedHandler = null, Func <WorkOrderUpdatedEventArgs, CancellationToken, Task> workOrderUpdatedHandler = null, Func <InitiativeLoggedEventArgs, CancellationToken, Task> initiativeLoggedHandler = null, Func <InitiativeStatusChangedEventArgs, CancellationToken, Task> statusChangedHandler = null, Func <InitiativeStatusDescriptionChangedEventArgs, CancellationToken, Task> statusDescriptionChangedHandler = null, Microsoft.Azure.ServiceBus.MessageHandlerOptions options = null) { if (_logger.IsEnabled(Serilog.Events.LogEventLevel.Information)) { StringBuilder handlerNames = new StringBuilder(); if (initiativeCreatedHandler != null) { handlerNames.Append("initiativeCreatedHndler"); } if (workOrderCreatedHandler != null) { if (handlerNames.Length > 0) { handlerNames.Append(", "); } handlerNames.Append("workOrderCreatedHandler"); } if (workOrderUpdatedHandler != null) { if (handlerNames.Length > 0) { handlerNames.Append(", "); } handlerNames.Append("workOrderUpdatedHandler"); } if (initiativeLoggedHandler != null) { if (handlerNames.Length > 0) { handlerNames.Append(", "); } handlerNames.Append("initiativeLoggedHandler"); } if (statusDescriptionChangedHandler != null) { if (handlerNames.Length > 0) { handlerNames.Append(", "); } handlerNames.Append("statusDescriptionChangedHandler"); } _logger.Information("Starting message pump with handlers " + handlerNames.ToString()); // + " on topic '{TopicName}' and subscription '{Subscription}'", _subscriptionClient.TopicPath, _subscriptionClient.SubscriptionName); } var messageHandlerOptions = options ?? new Microsoft.Azure.ServiceBus.MessageHandlerOptions(OnDefaultError); messageHandlerOptions.AutoComplete = false; _messageReceiver.RegisterMessageHandler(async(msg, token) => { _logger.Information("Received service bus message {MessageId}: {Label}", msg.Id.ToString(), msg.Label); switch (msg.Label) { case InitiativeMessageSender.INITIATIVE_CREATED: { if (initiativeCreatedHandler != null) { await ReceiveInitiativeCreated(msg, token, initiativeCreatedHandler); } else { await _messageReceiver.CompleteAsync(msg.LockToken); } break; } case InitiativeMessageSender.REMEDY_WORK_ITEM_CREATED: if (workOrderCreatedHandler != null) { await ReceiveInitiativeWorkItemCreated(msg, token, workOrderCreatedHandler); } else { await _messageReceiver.CompleteAsync(msg.LockToken); } break; case InitiativeMessageSender.WORK_ORDER_UPDATED: if (workOrderUpdatedHandler != null) { await ReceiveWorkOrderUpdated(msg, token, workOrderUpdatedHandler); } else { await _messageReceiver.CompleteAsync(msg.LockToken); } break; case InitiativeMessageSender.INITIATIVE_LOGGED: if (initiativeLoggedHandler != null) { await ReceiveInitiativeLogged(msg, token, initiativeLoggedHandler); } else { await _messageReceiver.CompleteAsync(msg.LockToken); } break; case InitiativeMessageSender.STATUS_CHANGED: if (statusChangedHandler != null) { await ReceiveInitiativeStatusChanged(msg, token, statusChangedHandler); } else { await _messageReceiver.CompleteAsync(msg.LockToken); } break; case InitiativeMessageSender.STATUS_DESCRIPTION_CHANGED: if (statusDescriptionChangedHandler != null) { await ReceiveInitiativeStatusDescriptionChanged(msg, token, statusDescriptionChangedHandler); } else { await _messageReceiver.CompleteAsync(msg.LockToken); } break; default: { await _messageReceiver.DeadLetterAsync(msg.LockToken, $"Unknown message type: { msg.Label }"); break; } } }, messageHandlerOptions); }