protected virtual void ConsumeMessages(object stateObject) { var subscriptionClient = stateObject as SubscriptionClient; if (subscriptionClient == null) { return; } while (!_exit) { try { BrokeredMessage brokeredMessage = null; brokeredMessage = subscriptionClient.Receive(); if (brokeredMessage != null) { var eventContext = new MessageContext(brokeredMessage); ConsumeMessage(eventContext); brokeredMessage.Complete(); MessageCount++; } } catch (Exception ex) { Thread.Sleep(1000); _logger.Error(ex.GetBaseException().Message, ex); } } }
public IMessageContext WrapMessage(object message, string correlationId = null, string topic = null, string key = null, string replyEndPoint = null, string messageId = null) { var messageContext = new MessageContext(message, messageId); if (!string.IsNullOrEmpty(correlationId)) { messageContext.CorrelationID = correlationId; } if (!string.IsNullOrEmpty(topic)) { messageContext.Topic = topic; } if (!string.IsNullOrEmpty(key)) { messageContext.Key = key; } if (!string.IsNullOrEmpty(replyEndPoint)) { messageContext.ReplyToEndPoint = replyEndPoint; } return messageContext; }
private void ReceiveMessages(CancellationTokenSource cancellationSource, Action<IMessageContext> onMessageReceived, Func<BrokeredMessage> receiveMessage) { while (!cancellationSource.IsCancellationRequested) { try { BrokeredMessage brokeredMessage = null; brokeredMessage = receiveMessage(); if (brokeredMessage != null) { var eventContext = new MessageContext(brokeredMessage); onMessageReceived(eventContext); brokeredMessage.Complete(); } } catch (OperationCanceledException) { return; } catch (ThreadAbortException) { return; } catch (Exception ex) { Thread.Sleep(1000); _logger.Error(ex.GetBaseException().Message, ex); } } }
void PublishEvent() { try { while (true) { var eventContext = MessageQueue.Take(); while(true) { try { var topicClient = _serviceBusClient.GetTopicClient(eventContext.Topic ?? _topic); topicClient.Send(((MessageContext)eventContext).BrokeredMessage); Task.Factory.StartNew(() => { using (var messageStore = IoCFactory.Resolve<IMessageStore>()) { messageStore.RemovePublishedEvent(eventContext.MessageID); } }); break; } catch (Exception ex) { if (ex is InvalidOperationException) { eventContext = new MessageContext(eventContext.Message as IMessage); } Thread.Sleep(1000); } } } } catch (Exception ex) { _logger.Debug("end publish working", ex); } }