Esempio n. 1
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);
        }
Esempio n. 2
0
 static Task CreateConsumerTask(string consumerId, CancellationTokenSource cancellationTokenSource)
 {
     return(Task.Run(() =>
     {
         var consumer = new EQueueConsumer(brokerAddress, consumerPort, adminPort, topic, Environment.MachineName, consumerId);
         consumer.Start();
         while (true)
         {
             try
             {
                 foreach (var queueMessage in consumer.PullMessages(100, 2000, cancellationTokenSource.Token))
                 {
                     var message = Encoding.UTF8.GetString(queueMessage.Body);
                     var sendTime = DateTime.Parse(message);
                     Console.WriteLine($"consumer:{consumer.ConsumerId} {DateTime.Now.ToString("HH:mm:ss.fff")} consume message: {message} cost: {(DateTime.Now - sendTime).TotalMilliseconds}");
                     consumer.CommitOffset(queueMessage.QueueId, queueMessage.QueueOffset);
                 }
             }
             catch (OperationCanceledException)
             {
                 break;
             }
             catch (ThreadAbortException)
             {
                 break;
             }
             catch (Exception ex)
             {
                 if (!cancellationTokenSource.IsCancellationRequested)
                 {
                     Console.WriteLine(ex.GetBaseException().Message);
                 }
             }
         }
         consumer.Stop();
     }));
 }