Ejemplo n.º 1
0
 public void Start()
 {
     foreach (KeyValuePair <string, IReadOnlyList <ConsumerExecutorDescriptor> > matchGroup in _selector.GetCandidatesMethodsOfGroupNameGrouped())
     {
         for (int i = 0; i < _options.ConsumerThreadCount; i++)
         {
             Task.Factory.StartNew(delegate
             {
                 try
                 {
                     IConsumerClient consumerClient = _consumerClientFactory.Create(matchGroup.Key);
                     _serverAddress = consumerClient.BrokerAddress;
                     RegisterMessageProcessor(consumerClient);
                     consumerClient.Subscribe(Enumerable.Select <ConsumerExecutorDescriptor, string>((IEnumerable <ConsumerExecutorDescriptor>)matchGroup.Value, (Func <ConsumerExecutorDescriptor, string>)((ConsumerExecutorDescriptor x) => x.TopicName)));
                     consumerClient.Listening(_pollingDelay, _cts.Token);
                 }
                 catch (OperationCanceledException)
                 {
                 }
                 catch (BrokerConnectionException ex2)
                 {
                     _isHealthy = false;
                     _logger.LogError(ex2, ex2.Message);
                 }
                 catch (Exception ex3)
                 {
                     _logger.LogError(ex3, ex3.Message);
                 }
             }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
         }
     }
     _compositeTask = Task.CompletedTask;
 }
Ejemplo n.º 2
0
        private StringOrIntValue GetComponent(BrokerAddress address, bool isPub)
        {
            if (isPub)
            {
                switch (address.Name)
                {
                case "RabbitMQ":
                    return("rabbitmq-producer");

                case "Kafka":
                    return("kafka-producer");
                }
            }
            else
            {
                switch (address.Name)
                {
                case "RabbitMQ":
                    return("rabbitmq-consumer");

                case "Kafka":
                    return("kafka-consumer");
                }
            }
            return(Components.CAP);
        }
Ejemplo n.º 3
0
        public void TestBrokerAddressToString()
        {
            var address = new BrokerAddress {
                Host = "localhost", Port = 9292
            };

            Assert.Equal("localhost:9292", address.ToString());
        }
Ejemplo n.º 4
0
        public void Execute()
        {
            var groupingMatches = _selector.GetCandidatesMethodsOfGroupNameGrouped();

            foreach (var matchGroup in groupingMatches)
            {
                ICollection <string> topics;
                try
                {
                    using (var client = _consumerClientFactory.Create(matchGroup.Key))
                    {
                        topics = client.FetchTopics(matchGroup.Value.Select(x => x.TopicName));
                    }
                }
                catch (BrokerConnectionException e)
                {
                    _isHealthy = false;
                    _logger.LogError(e, e.Message);
                    return;
                }

                for (int i = 0; i < _options.ConsumerThreadCount; i++)
                {
                    var topicIds = topics.Select(t => t);
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            using (var client = _consumerClientFactory.Create(matchGroup.Key))
                            {
                                _serverAddress = client.BrokerAddress;

                                RegisterMessageProcessor(client);

                                client.Subscribe(topicIds);

                                client.Listening(_pollingDelay, _cts.Token);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            //ignore
                        }
                        catch (BrokerConnectionException e)
                        {
                            _isHealthy = false;
                            _logger.LogError(e, e.Message);
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(e, e.Message);
                        }
                    }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
                }
            }
            _compositeTask = Task.CompletedTask;
        }
Ejemplo n.º 5
0
        private void DoWork(KeyValuePair <string, List <string> > matchGroup)
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    _logger.LogInformation($"启调新work线程,线程Id:{Thread.CurrentThread.ManagedThreadId.ToString()}");
                    using (var client = _consumerClientFactory.Create(matchGroup.Key))
                    {
                        _serverAddress = client.BrokerAddress;

                        RegisterMessageProcessor(client);

                        client.Subscribe(matchGroup.Value);

                        client.Listening(_pollingDelay, _cts.Token);
                    }
                }
                catch (OperationCanceledException)
                {
                    //ignore
                }
                catch (BrokerConnectionException e)
                {
                    //_isHealthy = false;
                    _logger.LogError(e, "BrokerConnectionException:" + e.Message);

                    DoWork(matchGroup);
                    _logger.LogInformation("准备销毁当前线程");
                    try { System.Threading.Thread.CurrentThread.Abort(); }
                    catch
                    {
                        _logger.LogInformation("当前线程已销毁");
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "OtherException:" + e.Message);
                    DoWork(matchGroup);
                    _logger.LogInformation("准备销毁当前线程");
                    try { System.Threading.Thread.CurrentThread.Abort(); }
                    catch
                    {
                        _logger.LogInformation("当前线程已销毁");
                    }
                }
            }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }
Ejemplo n.º 6
0
 private void TracingAfter(long?tracingTimestamp, TransportMessage message, BrokerAddress broker)
 {
     if (tracingTimestamp.HasValue && s_diagnosticListener.IsEnabled("DotNetCore.CAP.WriteConsumeAfter"))
     {
         long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
         CapEventDataSubStore eventData = new CapEventDataSubStore
         {
             OperationTimestamp = now,
             Operation          = message.GetName(),
             BrokerAddress      = broker,
             TransportMessage   = message,
             ElapsedTimeMs      = now - tracingTimestamp.Value
         };
         s_diagnosticListener.Write("DotNetCore.CAP.WriteConsumeAfter", eventData);
     }
 }
Ejemplo n.º 7
0
 private long?TracingBefore(TransportMessage message, BrokerAddress broker)
 {
     if (s_diagnosticListener.IsEnabled("DotNetCore.CAP.WriteConsumeBefore"))
     {
         CapEventDataSubStore eventData = new CapEventDataSubStore
         {
             OperationTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
             Operation          = message.GetName(),
             BrokerAddress      = broker,
             TransportMessage   = message
         };
         s_diagnosticListener.Write("DotNetCore.CAP.WriteConsumeBefore", eventData);
         return(eventData.OperationTimestamp);
     }
     return(null);
 }
Ejemplo n.º 8
0
        private void TracingAfter(long?tracingTimestamp, TransportMessage message, BrokerAddress broker)
        {
            if (tracingTimestamp != null && s_diagnosticListener.IsEnabled(CapDiagnosticListenerNames.AfterConsume))
            {
                var now       = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                var eventData = new CapEventDataSubStore()
                {
                    OperationTimestamp = now,
                    Operation          = message.GetName(),
                    BrokerAddress      = broker,
                    TransportMessage   = message,
                    ElapsedTimeMs      = now - tracingTimestamp.Value
                };

                s_diagnosticListener.Write(CapDiagnosticListenerNames.AfterConsume, eventData);
            }
        }
Ejemplo n.º 9
0
        private long? TracingBefore(TransportMessage message, BrokerAddress broker)
        {
            if (s_diagnosticListener.IsEnabled(CapDiagnosticListenerNames.BeforeConsume))
            {
                var eventData = new CapEventDataSubStore()
                {
                    OperationTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    Operation = message.GetName(),
                    BrokerAddress = broker,
                    TransportMessage = message
                };

                s_diagnosticListener.Write(CapDiagnosticListenerNames.BeforeConsume, eventData);

                return eventData.OperationTimestamp;
            }

            return null;
        }
Ejemplo n.º 10
0
        private void TracingError(long?tracingTimestamp, TransportMessage message, BrokerAddress broker, OperateResult result)
        {
            if (tracingTimestamp != null && s_diagnosticListener.IsEnabled(CapDiagnosticListenerNames.ErrorPublish))
            {
                var ex  = new PublisherSentFailedException(result.ToString(), result.Exception);
                var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                var eventData = new CapEventDataPubSend()
                {
                    OperationTimestamp = now,
                    Operation          = message.GetName(),
                    BrokerAddress      = broker,
                    TransportMessage   = message,
                    ElapsedTimeMs      = now - tracingTimestamp.Value,
                    Exception          = ex
                };

                s_diagnosticListener.Write(CapDiagnosticListenerNames.ErrorPublish, eventData);
            }
        }