Пример #1
0
        Producer <TKey, TValue> CreateProducer <TKey, TValue>(string topic, IBrokerConfig brokerConfig, Action <Producer <TKey, TValue> > onProducerCreate, IDataSerializer serializer)
        {
            Dictionary <string, object> config = new Dictionary <string, object> {
                { "bootstrap.servers", _producer.BrokerConfig.Uri }
            };
            var topicConfig = brokerConfig?.Topics?.FirstOrDefault(t => t.Name == topic);

            if (topicConfig != null && topicConfig.ProducerConfig != null)
            {
                var producerConfig = topicConfig.ProducerConfig;
                if (producerConfig.acks != null)
                {
                    config.Add("default.topic.config", new Dictionary <string, object> {
                        { "acks", producerConfig.acks.Value }
                    });
                }
                if (producerConfig.topic_metadata_refresh_inverval_ms != null)
                {
                    config.Add("topic.metadata.refresh.interval.ms", producerConfig.topic_metadata_refresh_inverval_ms.Value);
                }
                if (producerConfig.metadata_max_age_ms != null)
                {
                    config.Add("metadata.max.age.ms", producerConfig.metadata_max_age_ms.Value);
                }
                if (!string.IsNullOrEmpty(producerConfig.debug))
                {
                    config.Add("debug", producerConfig.debug);
                }
            }
            SerializerType serializerType = (topicConfig != null && topicConfig.SerializerType != null) ? topicConfig.SerializerType.Value : brokerConfig.SerializerType;
            ConfluentKafkaSerializer <TValue> kafkaSerializer = null;

            if (serializer == null)
            {
                kafkaSerializer = new ConfluentKafkaSerializer <TValue>(serializerType);
            }
            else
            {
                kafkaSerializer = new ConfluentKafkaSerializer <TValue>(serializer);
            }
            ConfluentKafkaSerializer <TKey> kafkaKeySerializer = null;

            if (typeof(TKey) != typeof(Null))
            {
                if (serializer == null)
                {
                    kafkaKeySerializer = new ConfluentKafkaSerializer <TKey>(serializerType);
                }
                else
                {
                    kafkaKeySerializer = new ConfluentKafkaSerializer <TKey>(serializer);
                }
            }
            Producer <TKey, TValue> producer = new Producer <TKey, TValue>(config, kafkaKeySerializer, kafkaSerializer);

            onProducerCreate?.Invoke(producer);
            return(producer);
        }
Пример #2
0
        private IRabbitMqUtilitiesService GetService(IBrokerConfig config = null)
        {
            var service = new RabbitMqUtilitiesService(config ?? _brokerConfig)
            {
                ServiceDescription = "Test service - sender"
            };

            return(service);
        }
Пример #3
0
        public static IBusFactory UseTestBroker(this IBrokerConfig brokerConfig)
        {
            var services = new ServiceCollection();

            services.AddSingleton <IConsumer, TestConsumer>();
            services.AddSingleton <IConsumerFactory, TestConsumerFactory>();
            services.AddSingleton <ISubscriptionManager, TestSubscriptionManager>();
            return(brokerConfig.AddBroker <TestBroker>().AddServices(services));
        }
Пример #4
0
 internal Connection(IBrokerConfig brokerConfig, ILog log)
 {
     if (brokerConfig == null)
     {
         throw new ArgumentNullException(nameof(brokerConfig));
     }
     Log = new LogHelpler(log);
     Log.Info("create connection for broker {0}, {1}", brokerConfig.Name, brokerConfig.Uri);
     BrokerConfig = brokerConfig;
     Producer     = new Producer(brokerConfig, this);
 }
Пример #5
0
        private IBrokerTransport GetTransporter(string queue)
        {
            if (_brokers.TryGetValue(queue, out IBrokerTransport transport))
            {
                return(transport);
            }

            IBrokerConfig config = _config.GetConfigForQueue(queue);

            transport = config.CreateTransporterInstance();
            _brokers.Add(queue, transport);

            return(transport);
        }
Пример #6
0
        public static IBusFactory UseRabbitMQ(this IBrokerConfig factory, Action <IConfig> action = null)
        {
            var config = new Config();

            action?.Invoke(config);
            var services = new ServiceCollection();

            services.AddSingleton <IConfig>(config);
            services.AddSingleton <IChannelFactory, ChannelFactory>();
            services.AddSingleton <ISubscriptionManager, SubscriptionManager>();
            services.AddTransient <IConsumerFactory, ConsumerFactory>();

            return(factory.AddBroker <Broker>().AddServices(services));
        }
Пример #7
0
        internal Producer <TKey, TValue> GetProducer <TKey, TValue>(string topic, IBrokerConfig brokerConfig, Action <Producer <TKey, TValue> > onProducerCreate, IDataSerializer serializer)
        {
            string producerKey = string.Format(ProducerKeyFormat, topic, typeof(TKey).FullName, typeof(TValue).FullName);

            if (_producerDict.ContainsKey(producerKey))
            {
                return(_producerDict[producerKey] as Producer <TKey, TValue>);
            }
            lock (_producerDict)
            {
                if (_producerDict.ContainsKey(producerKey))
                {
                    return(_producerDict[producerKey] as Producer <TKey, TValue>);
                }

                var producer = CreateProducer <TKey, TValue>(topic, brokerConfig, onProducerCreate, serializer);
                _producerDict.Add(producerKey, producer);
                return(producer);
            }
        }
Пример #8
0
 internal Producer(IBrokerConfig brokerConfig, Connection connection)
 {
     BrokerConfig     = brokerConfig;
     Connection       = connection;
     _producerManager = new ConfluentKafkaProducerContainer(this);
 }
Пример #9
0
 internal Producer <Null, T> GetProducer <T>(string topic, IBrokerConfig brokerConfig, Action <Producer <Null, T> > onProducerCreate, IDataSerializer serializer)
 {
     return(GetProducer <Null, T>(topic, brokerConfig, onProducerCreate, serializer));
 }
Пример #10
0
 public RabbitMqUtilitiesService(IBrokerConfig config)
 {
     _config = config;
 }