コード例 #1
0
ファイル: EQueueConsumer.cs プロジェクト: netdebug/IFramework
        private void ReceiveMessages(CancellationTokenSource cancellationTokenSource,
                                     OnEQueueMessageReceived onMessageReceived)
        {
            IEnumerable <EQueueMessages.QueueMessage> messages = null;

            #region peek messages that not been consumed since last time

            while (!cancellationTokenSource.IsCancellationRequested)
            {
                try
                {
                    messages = PullMessages(100, 2000, cancellationTokenSource.Token);
                    foreach (var message in messages)
                    {
                        try
                        {
                            AddMessage(message);
                            onMessageReceived(this, message);
                            BlockIfFullLoad();
                        }
                        catch (OperationCanceledException)
                        {
                            return;
                        }
                        catch (ThreadAbortException)
                        {
                            return;
                        }
                        catch (Exception ex)
                        {
                            if (message.Body != null)
                            {
                                RemoveMessage(message.QueueId, message.QueueOffset);
                            }
                            _logger.Error(ex.GetBaseException().Message, ex);
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    if (!cancellationTokenSource.IsCancellationRequested)
                    {
                        Thread.Sleep(1000);
                        _logger.Error(ex.GetBaseException().Message, ex);
                    }
                }
            }

            #endregion
        }
コード例 #2
0
 public EQueueConsumer(string clusterName,
                       List <IPEndPoint> nameServerList,
                       string[] topics,
                       string groupId,
                       string consumerId,
                       OnEQueueMessageReceived onMessageReceived,
                       ConsumerConfig consumerConfig = null,
                       bool start = true)
     : base(topics, groupId, consumerId, consumerConfig)
 {
     _onMessageReceived = onMessageReceived;
     ClusterName        = clusterName;
     NameServerList     = nameServerList;
     SlidingDoors       = new ConcurrentDictionary <string, SlidingDoor>();
 }
コード例 #3
0
        private static EQueueConsumer CreateConsumer(string consumerId)
        {
            OnEQueueMessageReceived onMessageReceived = (equeueConsumer, queueMessage) =>
            {
                var message  = Encoding.UTF8.GetString(queueMessage.Body);
                var sendTime = DateTime.Parse(message);
                Console.WriteLine(
                    $"consumer:{equeueConsumer.ConsumerId} {DateTime.Now.ToString("HH:mm:ss.fff")} consume message: {message} cost: {(DateTime.Now - sendTime).TotalMilliseconds}");
                equeueConsumer.CommitOffset(queueMessage.BrokerName, queueMessage.QueueId, queueMessage.QueueOffset);
            };

            var consumer = new EQueueConsumer(clusterName, NameServerList, topic, Environment.MachineName, consumerId,
                                              onMessageReceived);

            return(consumer);
        }
コード例 #4
0
ファイル: EQueueConsumer.cs プロジェクト: netdebug/IFramework
 public EQueueConsumer(string clusterName,
                       List <IPEndPoint> nameServerList,
                       string topic,
                       string groupId,
                       string consumerId,
                       OnEQueueMessageReceived onMessageReceived,
                       ConsumerConfig consumerConfig = null,
                       bool start = true)
 {
     _onMessageReceived = onMessageReceived;
     ClusterName        = clusterName;
     _consumerConfig    = consumerConfig ?? ConsumerConfig.DefaultConfig;
     NameServerList     = nameServerList;
     SlidingDoors       = new ConcurrentDictionary <int, SlidingDoor>();
     Topic      = topic;
     GroupId    = groupId;
     ConsumerId = consumerId ?? string.Empty;
     if (start)
     {
         Start();
     }
 }