예제 #1
0
        protected override void OnInit()
        {
            OnDispose();
            _senders = new List <RedisMQConnection>();

            if (Config != null)
            {
                if (Config.ClientQueues != null)
                {
                    _clientQueues = Config.ClientQueues.FirstOf(
                        c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true && c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                        c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true,
                        c => c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true,
                        c => c.EnvironmentName.IsNullOrWhitespace());
                }
                _senderOptions          = Config.RequestOptions?.ClientSenderOptions;
                _receiverOptions        = Config.ResponseOptions?.ClientReceiverOptions;
                _receiverOptionsTimeout = TimeSpan.FromSeconds(_receiverOptions?.TimeoutInSec ?? 20);
                UseSingleResponseQueue  = _receiverOptions?.Parameters?[ParameterKeys.SingleResponseQueue].ParseTo(true) ?? true;

                if (_clientQueues?.SendQueues?.Any() == true)
                {
                    foreach (var queue in _clientQueues.SendQueues)
                    {
                        Core.Log.LibVerbose("New Producer from QueueClient");
                        var redisConnection = new RedisMQConnection(queue);
                        _senders.Add(redisConnection);
                    }
                }
                if (_clientQueues?.RecvQueue != null && !SendOnly)
                {
                    _receiverConnection = new RedisMQConnection(_clientQueues.RecvQueue);
                    if (!UseSingleResponseQueue)
                    {
                        _receiverConnection.Name += "-" + Core.InstanceIdString;
                        Core.Log.InfoBasic("Using custom response queue: {0}", _receiverConnection.Name);
                    }
                    _receiverConnection.SubscribeAsync(new Action <RedisChannel, RedisValue>(ProcessReceivedMessage)).WaitAsync();
                }
            }

            Core.Status.Attach(collection =>
            {
                if (_senders != null)
                {
                    for (var i = 0; i < _senders.Count; i++)
                    {
                        collection.Add("Sender Path: {0}".ApplyFormat(i), _senders[i].Route);
                    }
                }
                if (_clientQueues?.RecvQueue != null)
                {
                    collection.Add("Receiver Path", _clientQueues.RecvQueue.Route);
                }
            });
        }
예제 #2
0
        protected override async Task OnListenerTaskStartAsync(CancellationToken token)
        {
            _token      = token;
            _connection = new RedisMQConnection(Connection);
            await _connection.SubscribeAsync(_messageHandlerDelegate).ConfigureAwait(false);

            _monitorTask = Task.Run(_monitorDelegate, _token);
            await token.WhenCanceledAsync().ConfigureAwait(false);

            OnDispose();
            await _monitorTask.ConfigureAwait(false);
        }
예제 #3
0
 protected override void OnDispose()
 {
     if (_senders != null)
     {
         _senders.Clear();
         _senders = null;
     }
     if (_receiverConnection is null)
     {
         return;
     }
     _receiverConnection.UnsubscribeAsync().WaitAsync();
     _receiverConnection = null;
 }
예제 #4
0
 protected override void OnDispose()
 {
     if (_connection is null)
     {
         return;
     }
     try
     {
         _connection.UnsubscribeAsync().WaitAsync();
     }
     catch
     {
         // ignored
     }
     _connection = null;
 }
예제 #5
0
        private async Task MonitorProcess()
        {
            while (!_token.IsCancellationRequested)
            {
                try
                {
                    if (Interlocked.CompareExchange(ref _exceptionSleep, 0, 1) == 1)
                    {
                        OnDispose();
                        Core.Log.Warning("An exception has been thrown, the listener has been stopped for {0} seconds.", Config.RequestOptions.ServerReceiverOptions.SleepOnExceptionInSec);
                        await Task.Delay(Config.RequestOptions.ServerReceiverOptions.SleepOnExceptionInSec * 1000, _token).ConfigureAwait(false);

                        _connection = new RedisMQConnection(Connection);
                        await _connection.SubscribeAsync(_messageHandlerDelegate).ConfigureAwait(false);

                        Core.Log.Warning("The listener has been resumed.");
                    }

                    if (Counters.CurrentMessages >= Config.RequestOptions.ServerReceiverOptions.MaxSimultaneousMessagesPerQueue)
                    {
                        OnDispose();
                        Core.Log.Warning("Maximum simultaneous messages per queue has been reached, the message needs to wait to be processed, consider increase the MaxSimultaneousMessagePerQueue value, CurrentValue={0}.", Config.RequestOptions.ServerReceiverOptions.MaxSimultaneousMessagesPerQueue);

                        while (!_token.IsCancellationRequested && Counters.CurrentMessages >= Config.RequestOptions.ServerReceiverOptions.MaxSimultaneousMessagesPerQueue)
                        {
                            await Task.Delay(500, _token).ConfigureAwait(false);
                        }

                        _connection = new RedisMQConnection(Connection);
                        await _connection.SubscribeAsync(_messageHandlerDelegate).ConfigureAwait(false);

                        Core.Log.Warning("The listener has been resumed.");
                    }

                    await Task.Delay(1000, _token).ConfigureAwait(false);
                }
                catch (TaskCanceledException) { }
                catch (Exception ex)
                {
                    Core.Log.Write(ex);
                    if (!_token.IsCancellationRequested)
                    {
                        await Task.Delay(2000, _token).ConfigureAwait(false);
                    }
                }
            }
        }