public void Start()
 {
     _topics.ForEach(topic =>
     {
         try
         {
             if (!string.IsNullOrWhiteSpace(topic))
             {
                 // Receive messages
                 var subscriptionClient = _serviceBusClient.CreateSubscriptionClient(topic, _subscriptionName);
                 _consumeWorkTasks.Add(Task.Factory.StartNew(ConsumeMessages, subscriptionClient, TaskCreationOptions.LongRunning));
             }
         }
         catch (Exception e)
         {
             _logger.Error(e.GetBaseException().Message, e);
         }
     });
 }
Exemple #2
0
        public void Start()
        {
            #region init sending commands Worker

            #region Init  Command Queue client
            if (_commandQueueNames != null && _commandQueueNames.Length > 0)
            {
                _commandQueueNames.ForEach(commandQueueName =>
                                           _commandQueueClients.Add(_serviceBusClient.CreateQueueClient(commandQueueName)));
            }
            #endregion

            _sendCommandWorkTask = Task.Factory.StartNew(() =>
            {
                using (var messageStore = IoCFactory.Resolve <IMessageStore>())
                {
                    messageStore.GetAllUnSentCommands()
                    .ForEach(commandContext => _toBeSentCommandQueue.Add(commandContext));
                }
                while (!_exit)
                {
                    try
                    {
                        var commandContext = _toBeSentCommandQueue.Take();
                        SendCommand(commandContext);
                        Task.Factory.StartNew(() =>
                        {
                            using (var messageStore = IoCFactory.Resolve <IMessageStore>())
                            {
                                messageStore.RemoveSentCommand(commandContext.MessageID);
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        _logger.Debug("send command quit", ex);
                    }
                }
            }, TaskCreationOptions.LongRunning);
            #endregion

            #region init process command reply worker

            _replySubscriptionClient = _serviceBusClient.CreateSubscriptionClient(_replyTopicName, _replySubscriptionName);

            _subscriptionConsumeTask = Task.Factory.StartNew(() =>
            {
                while (!_exit)
                {
                    BrokeredMessage brokeredMessage = null;
                    try
                    {
                        brokeredMessage = _replySubscriptionClient.Receive();
                        if (brokeredMessage != null)
                        {
                            var reply = new MessageContext(brokeredMessage);
                            ConsumeReply(reply);
                        }
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(1000);
                        _logger.Error("consume reply error", ex);
                    }
                    finally
                    {
                        if (brokeredMessage != null)
                        {
                            brokeredMessage.Complete();
                        }
                    }
                }
            }, TaskCreationOptions.LongRunning);

            #endregion
        }