Beispiel #1
0
        static void Main(string[] args)
        {
            //Producer aa = new Producer(new KafkaSetting().AsKafkaSetting());
            Setup();
            List <string> topics   = new List <string>();
            var           prodocer = _conn.Rent();
            var           aa       = Encoding.Default.GetBytes("aaaa");

            prodocer.ProduceAsync("test1", null, aa);
            topics.Add("test1");
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       tok    = source.Token;

            factory.Create("simple-csharp-consumer").Subscribe(topics);
            factory.Create("simple-csharp-consumer").OnMessageReceived += Program_OnMessageReceived;
            Task t = new Task(() =>
            {
                factory.Create("simple-csharp-consumer").Listening(TimeSpan.FromSeconds(1), tok);
            }, tok);

            t.Start();



            _bus.Send("ServiceBus.TestPublish.NetCore", new RequestIntegrationEvent()
            {
                Message = "Masstransit test Succees!"
            });
            _logger.Info("ServiceBus Logs test!!");
            Console.WriteLine("Masstransit test Succees!");
            Console.ReadKey();
        }
Beispiel #2
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;
        }
Beispiel #3
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;
 }
Beispiel #4
0
        public static void Run_Consume(string topics)
        {
            using (var consumer = factory.Create("simple-csharp-consumer") as Consumer <Ignore, string>)
            {
                // Note: All event handlers are called on the main thread.

                consumer.OnPartitionEOF += (_, end)
                                           => Console.WriteLine($"Reached end of topic {end.Topic} partition {end.Partition}, next message will be at offset {end.Offset}");

                consumer.OnError += (_, error)
                                    => Console.WriteLine($"Error: {error}");

                consumer.OnConsumeError += (_, error)
                                           => Console.WriteLine($"Consume error: {error}");

                consumer.OnPartitionsAssigned += (_, partitions) =>
                {
                    Console.WriteLine($"Assigned partitions: [{string.Join(", ", partitions)}], member id: {consumer.MemberId}");
                    consumer.Assign(partitions);
                };

                consumer.OnPartitionsRevoked += (_, partitions) =>
                {
                    Console.WriteLine($"Revoked partitions: [{string.Join(", ", partitions)}]");
                    consumer.Unassign();
                };

                consumer.OnStatistics += (_, json)
                                         => Console.WriteLine($"Statistics: {json}");

                consumer.Subscribe(topics);

                Console.WriteLine($"Started consumer, Ctrl-C to stop consuming");

                var cancelled = false;
                Console.CancelKeyPress += (_, e) => {
                    e.Cancel  = true; // prevent the process from terminating.
                    cancelled = true;
                };

                while (!cancelled)
                {
                    Message <Ignore, string> msg;
                    if (!consumer.Consume(out msg, TimeSpan.FromMilliseconds(100)))
                    {
                        continue;
                    }

                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");

                    if (msg.Offset % 5 == 0)
                    {
                        Console.WriteLine($"Committing offset");
                        var committedOffsets = consumer.CommitAsync(msg).Result;
                        Console.WriteLine($"Committed offset: {committedOffsets}");
                    }
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EventBusHostedService"/> class.
        /// </summary>
        /// <param name="consumerClientFactory">The consumer client factory.</param>
        /// <param name="subscribers">The subscribers.</param>
        /// <param name="transportOptions">The transport options.</param>
        /// <exception cref="ArgumentNullException">subscribers</exception>
        public EventBusHostedService(
            IConsumerClientFactory consumerClientFactory,
            IEnumerable <ISubscriber> subscribers,
            IOptions <TransportOptions> transportOptions)
        {
            _subscribers = subscribers ?? throw new ArgumentNullException(nameof(subscribers));
            _options     = transportOptions.Value;

            //TODO think groups id
            _consumerClient = consumerClientFactory.Create("group_1");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EventBusHostedService"/> class.
        /// </summary>
        /// <param name="consumerClientFactory">The consumer client factory.</param>
        /// <param name="subscribers">The subscribers.</param>
        /// <param name="transportOptions">The transport options.</param>
        /// <exception cref="ArgumentNullException">subscribers</exception>
        public EventBusHostedService(
            IConsumerClientFactory consumerClientFactory,
            IServiceScopeFactory serviceScopeFactory,
            IOptions <TransportOptions> transportOptions)
        {
            _serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
            _options             = transportOptions.Value;

            //TODO think groups id
            _consumerClient = consumerClientFactory.Create(_options.DefaultGroup);
        }
Beispiel #7
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);
        }
Beispiel #8
0
        public void Start()
        {
            var groupingMatches = _selector.GetCandidatesMethodsOfGroupNameGrouped();

            foreach (var matchGroup in groupingMatches)
                Task.Factory.StartNew(() =>
                {
                    using (var client = _consumerClientFactory.Create(matchGroup.Key))
                    {
                        RegisterMessageProcessor(client);

                        client.Subscribe(matchGroup.Value.Select(x => x.Attribute.Name));

                        client.Listening(_pollingDelay, _cts.Token);
                    }
                }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            _compositeTask = Task.CompletedTask;
        }
Beispiel #9
0
        public void Start()
        {
            if (_subscribers.Count() == 0)
            {
                throw new NoSubscriberException();
            }
            if (_options.SubscribeThreadCount == 0)
            {
                throw new ArgumentException("the parameter SubscribeThreadCount must be great than 0.");
            }

            foreach (var subscriber in _subscribers)
            {
                var count = subscriber.ThreadCount > 0 ? subscriber.ThreadCount : _options.SubscribeThreadCount;
                for (int i = 0; i < count; i++)
                {
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            using (var client = _consumerClientFactory.Create(subscriber.ClientOption))
                            {
                                RegisterMessageProcessor(client, subscriber);

                                client.Listening(_pollingDelay, _cts.Token, subscriber.PrefetchCount);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            //ignore
                        }
                        catch (BrokerConnectionException e)
                        {
                            _logger.LogError(e, e.Message);
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(e, e.Message);
                        }
                    }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
                }
            }
            _compositeTask = Task.CompletedTask;
        }
Beispiel #10
0
        public void Start()
        {
            _cts = new CancellationTokenSource();

            var groupingMatches = _selector.GetCandidatesMethodsOfGroupNameGrouped();

            foreach (var matchGroup in groupingMatches)
            {
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        using (var client = _consumerClientFactory.Create(matchGroup.Key))
                        {
                            _serverAddress = client.ServersAddress;

                            RegisterMessageProcessor(client);

                            client.Subscribe(matchGroup.Value.Select(x => x.Attribute.Name));

                            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.FromResult(true);
        }
Beispiel #11
0
        public void Start()
        {
            var groupingMatchs = _selector.GetCandidatesMethodsOfGroupNameGrouped(_serviceProvider);

            foreach (var matchGroup in groupingMatchs)
            {
                Task.Factory.StartNew(() =>
                {
                    using (var client = _consumerClientFactory.Create(matchGroup.Key))
                    {
                        RegisterMessageProcessor(client);

                        foreach (var item in matchGroup.Value)
                        {
                            client.Subscribe(item.Attribute.Name);
                        }

                        client.Listening(_pollingDelay);
                    }
                }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);
            }
            _compositeTask = Task.CompletedTask;
        }
Beispiel #12
0
        public void Start()
        {
            var queueGroup = _consumerExecutorDescriptor.GroupBy(d => d.Attribute.Name);

            foreach (var qg in queueGroup)
            {
                Task.Factory.StartNew(() =>
                {
                    using (var client = _consumerClientFactory.Create(qg.Key, qg.First().Attribute.ExchangeName))
                    {
                        HandleMessage(client);

                        foreach (var qgt in qg)
                        {
                            client.Subscribe(qgt.Attribute.ExchangeName, qgt.Attribute.Group);
                        }

                        client.Listening(_pollingDelay, _cts.Token);
                    }
                }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }

            _compositeTask = Task.CompletedTask;
        }