/// <summary> /// Initializes a new instance of the <see cref="SyncProducer"/> class. /// </summary> /// <param name="config"> /// The producer config. /// </param> public SyncProducer(SyncProducerConfiguration config) { Guard.NotNull(config, "config"); this.Config = config; this.connection = new KafkaConnection( this.Config.Host, this.Config.Port, config.BufferSize, config.SocketTimeout); }
public void ShouldThrowMessageTooLarge() { var connection = new Mock<IKafkaConnection>(); var config = new SyncProducerConfiguration { MaxMessageSize = 99 }; var producer = new SyncProducer(config, connection.Object); producer.Send(new ProducerRequest(1, "client", 0, 0, new List<TopicData>() { new TopicData("test", new List<PartitionData>() { new PartitionData(0, new BufferedMessageSet(new List<Message>() {new Message(new byte[100])}, 0)) }) })); }
public void ShouldNotUpdateInfoWhenErrorResponse() { var pool = new Mock<ISyncProducerPool>(); var producer = new Mock<ISyncProducer>(); var partitionMetadatas = new List<PartitionMetadata>() { new PartitionMetadata(0, new Broker(0, "host1", 1234), Enumerable.Empty<Broker>(), Enumerable.Empty<Broker>()) }; var metadatas = new List<TopicMetadata>() { new TopicMetadata("test", partitionMetadatas, ErrorMapping.LeaderNotAvailableCode) }; producer.Setup(p => p.Send(It.IsAny<TopicMetadataRequest>())).Returns(() => metadatas); var producerConfig = new SyncProducerConfiguration(new ProducerConfiguration((IList<BrokerConfiguration>)null), 0, "host1", 1234); producer.Setup(p => p.Config).Returns(producerConfig); pool.Setup(p => p.GetShuffledProducers()).Returns(() => new List<ISyncProducer>() { producer.Object }); var info = new BrokerPartitionInfo(pool.Object); info.GetBrokerPartitionInfo(1, "test", 1, "test"); }
public static string SyncProducerConfigToString(SyncProducerConfiguration syncConfig) { if (syncConfig == null) { return "(SyncProducerConfiguration is null)"; } return string.Format("BrokerId={0},Host={1}:Port={2}", syncConfig.BrokerId, syncConfig.Host, syncConfig.Port); }
public SyncProducerWrapper(SyncProducerConfiguration syncProducerConfig, int count) { Producers = new Queue<ISyncProducer>(count); for (int i = 0; i < count; i++) { //TODO: if can't create , should retry later. should not block Producers.Enqueue(new SyncProducer(syncProducerConfig)); } }
public void AddProducer(Broker broker) { var syncProducerConfig = new SyncProducerConfiguration(this.Config, broker.Id, broker.Host, broker.Port); var producerWrapper = new SyncProducerWrapper(syncProducerConfig, this.Config.SyncProducerOfOneBroker); Logger.DebugFormat("Creating sync producer for broker id = {0} at {1}:{2} SyncProducerOfOneBroker:{3}", broker.Id, broker.Host, broker.Port, this.Config.SyncProducerOfOneBroker); this.syncProducers.TryAdd(broker.Id, producerWrapper); }