Exemplo n.º 1
0
        public void SubscribeTopic <T, TH>(Func <TH> handler, string subscriberId, string exchangeName, string queueName, string bindKeyTopic)
            where T : MessageBase
            where TH : IProcessMessage <T>
        {
            if (handler == null)
            {
                return;
            }
            var eventName   = typeof(T).Name;
            var containsKey = _subsManager.HasSubscriptionsForEvent <T>();

            if (!containsKey)
            {
                //var processEvent = new ProcessEvent(
                //   async (x, y) => { await ProcessEvent(x, y); }
                //    );
                var client = new RabbitMQConsumerClient <T>(exchangeName, queueName, "", false, _connectionPool, _rabbitMQOptions, _logger);//, processEvent
                if (!busClientList.ContainsKey(queueName))
                {
                    busClientList.Add(queueName, client);
                }

                client.Subscribe(new List <string>(1)
                {
                    bindKeyTopic
                });
                client.Listening(new TimeSpan(0, 0, 2), CancellationToken.None);
                client.OnMessageReceievedToOutSide += async(x, y) => { await ProcessEvent(queueName, y); };
            }
            _subsManager.AddSubscription <T, TH>(handler);
        }
Exemplo n.º 2
0
        public void SubscribeTopic <T, TH>(Func <TH> handler, string borkerList, string groupID, List <string> topics)
            where TH : IProcessMessage <T>
            where T : MessageBase
        {
            var containsKey = _subsManager.HasSubscriptionsForEvent <T>();

            if (!containsKey)
            {
                Task.Run(() =>
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(borkerList))
                        {
                            borkerList = ConnectionBuilder.getBorkerList();
                        }
                        bool enableAutoCommit = false;

                        var config = new Config()
                        {
                            GroupId            = groupID,
                            EnableAutoCommit   = enableAutoCommit,
                            StatisticsInterval = TimeSpan.FromSeconds(60)
                        };

                        EventConsumer consumer = new EventConsumer(config, borkerList);

                        consumer.OnMessage += async(obj, msgs) => {
                            string text = Encoding.UTF8.GetString(msgs.Payload, 0, msgs.Payload.Length);
                            _logger.LogInformation("Topic: {0} Partition: {1} Offset: {2} {3}", msgs.Topic, msgs.Partition, msgs.Offset, text);

                            MessageBase mb = new MessageBase()
                            {
                                MessageBodyByte = msgs.Payload,
                                MessageRouter   = msgs.Topic
                            };

                            await ProcessEvent(mb.MessageRouter, mb);
                            if (!enableAutoCommit && msgs.Offset % 10 == 0)
                            {
                                _logger.LogInformation($"Committing offset");
                                consumer.Commit(msgs).Wait();
                                _logger.LogInformation($"Committed offset");
                            }
                        };

                        consumer.OnConsumerError += (obj, errorCode) =>
                        {
                            _logger.LogWarning("Consumer Error: {0}", errorCode);
                        };

                        consumer.OnEndReached += (obj, end) => {
                            _logger.LogInformation("Reached end of topic {0} partition {1}, next message will be at offset {2}", end.Topic, end.Partition, end.Offset);
                        };

                        consumer.OnError += (obj, error) => {
                            _logger.LogWarning("Error: {0} {1}", error.ErrorCode, error.Reason);
                        };

                        if (enableAutoCommit)
                        {
                            consumer.OnOffsetCommit += (obj, commit) => {
                                if (commit.Error != ErrorCode.NO_ERROR)
                                {
                                    _logger.LogInformation("Failed to commit offsets: {0}", commit.Error);
                                }
                                _logger.LogInformation("Successfully committed offsets: [{0}]", string.Join(", ", commit.Offsets));
                            };
                        }

                        consumer.OnPartitionsAssigned += (obj, partitions) => {
                            _logger.LogInformation("Assigned partitions: [{0}], member id: {1}", string.Join(", ", partitions), consumer.MemberId);
                            consumer.Assign(partitions);
                        };

                        consumer.OnPartitionsRevoked += (obj, partitions) => {
                            _logger.LogInformation("Revoked partitions: [{0}]", string.Join(", ", partitions));
                            consumer.Unassign();
                        };

                        consumer.OnStatistics += (obj, json) => {
                            _logger.LogInformation("Statistics: {0}", json);
                        };

                        consumer.Subscribe(topics);
                        consumer.Start();

                        _logger.LogInformation("Assigned to: [{0}]", string.Join(", ", consumer.Assignment));
                        _logger.LogInformation("Subscribed to: [{0}]", string.Join(", ", consumer.Subscription));
                        _logger.LogInformation("Started consumer, press enter to stop consuming");

                        while (true)
                        {
                            Thread.Sleep(1);
                        }
                        // Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError("SubscribeTopicAsync", ex);
                        return(ex.Message);
                    }
                });//----------end run
            }//-----------end if
            _subsManager.AddSubscription <T, TH>(handler);
        }//-----------end SubscribeTopic
Exemplo n.º 3
0
        public void SubscribeTopic <T, TH>(Func <TH> handler, string borkerList, string groupID, List <string> topics)
            where TH : IProcessMessage <T>
            where T : MessageBase
        {
            var eventName   = typeof(T).Name;
            var containsKey = _subsManager.HasSubscriptionsForEvent <T>();

            if (!containsKey)
            {
                if (!_consumerConnection.IsConnected)
                {
                    _consumerConnection.TryConnect();
                }
                _subsManager.AddSubscription <T, TH>(handler);

                using (var consumer = _consumerConnection.CreateConnect() as IConsumer <string, MessageBase>)
                {
                    consumer.Subscribe(eventName);

                    try
                    {
                        while (true)
                        {
                            try
                            {
                                var consumeResult = consumer.Consume();
                                if (consumeResult.IsPartitionEOF)
                                {
                                    _logger.LogInformation(
                                        $"Reached end of topic {consumeResult.Topic}, partition {consumeResult.Partition}, offset {consumeResult.Offset}.");

                                    continue;
                                }

                                _logger.LogInformation($"Received message at {consumeResult.TopicPartitionOffset}: {consumeResult.Value}");

                                ConsumerClient_OnMessage(null, consumeResult);

                                if (consumeResult.Offset % commitPeriod == 0)
                                {
                                    try
                                    {
                                        consumer.Commit(consumeResult);
                                    }
                                    catch (KafkaException e)
                                    {
                                        _logger.LogError($"Commit error: {e.Error.Reason}", e);
                                    }
                                }
                            }
                            catch (ConsumeException e)
                            {
                                _logger.LogError($"Consume error: {e.Error.Reason}", e);
                            }
                        }
                    }
                    catch (OperationCanceledException e)
                    {
                        _logger.LogError("Closing consumer.", e);
                        consumer.Close();
                    }
                }
            }
        }