Example #1
0
        public async Task HandleReceivedMessage(string message, int retryCount, ulong deliveryTag, string requestKey, Func <string, int, ulong, string, Task <bool> > func)
        {
            var success = await func.Invoke(message, retryCount, deliveryTag, requestKey);

            try
            {
                if (success)
                {
                    if (this.QueueProcessorSettings.AutoAckOnSuccess)
                    {
                        this.HandleSuccededEvent(deliveryTag);
                    }
                }
                else
                {
                    this.HandleFailedEvent(message, retryCount, deliveryTag, requestKey);
                }
            }
            catch (Exception)
            {
                lock (this.LockAckError)
                {
                    StaticSimpleLogger.Error(nameof(QueueProcessor), nameof(HandleReceivedMessage), $"Error on handle received message {deliveryTag}", null, requestKey);
                    this.QueueClient.TryConnect();
                }
            }
        }
Example #2
0
        public bool ExecuteConsumer(Func <string, int, ulong, string, Task <bool> > func)
        {
            try
            {
                this.QueueClient.TryConnect();

                this.QueueClient.ReceiveMessage += (message, retryCount, deliveryTag, requestKey) =>
                {
                    lock (this.LockThreads)
                    {
                        do
                        {
                            this.Threads.Where(t => t.IsCompleted).ToList()
                            .ForEach(task => task.Dispose());
                            this.Threads.RemoveAll(t => t.IsCompleted);
                        }while (this.CanAddThread() == false);
                    }

                    this.Threads.Add(HandleReceivedMessage(message, retryCount, deliveryTag, requestKey, func));

                    return(Task.CompletedTask);
                };

                StaticSimpleLogger.Info(nameof(QueueProcessor), nameof(ExecuteConsumer), "Queue connected!");

                return(true);
            }
            catch (Exception e)
            {
                StaticSimpleLogger.Error(nameof(QueueProcessor), nameof(ExecuteConsumer), "An exception occurred while trying to connect with queue!", e);
                Thread.Sleep(2000);
                return(false);
            }
        }