Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Producer&lt;TKey, TData&gt;"/> class.
        /// </summary>
        /// <param name="config">The config object.</param>
        /// <param name="partitioner">The partitioner that implements <see cref="IPartitioner&lt;TKey&gt;" />
        /// used to supply a custom partitioning strategy based on the message key.</param>
        /// <param name="producerPool">Pool of producers, one per broker.</param>
        /// <param name="populateProducerPool">if set to <c>true</c>, producers should be populated.</param>
        /// <remarks>
        /// Should be used for testing purpose only.
        /// </remarks>
        internal Producer(
            ProducerConfiguration config,
            IPartitioner <TKey> partitioner,
            IProducerPool <TData> producerPool,
            bool populateProducerPool = true)
        {
            Guard.NotNull(config, "config");
            Guard.NotNull(producerPool, "producerPool");

            this.config               = config;
            this.partitioner          = partitioner ?? new DefaultPartitioner <TKey>();
            this.populateProducerPool = populateProducerPool;
            this.producerPool         = producerPool;
            if (this.config.IsZooKeeperEnabled)
            {
                this.brokerPartitionInfo = new ZKBrokerPartitionInfo(this.config, this.Callback);
            }
            else
            {
                this.brokerPartitionInfo = new ConfigBrokerPartitionInfo(this.config);
            }

            if (this.populateProducerPool)
            {
                IDictionary <int, Broker> allBrokers = this.brokerPartitionInfo.GetAllBrokerInfo();
                foreach (var broker in allBrokers)
                {
                    this.producerPool.AddProducer(
                        new Broker(broker.Key, broker.Value.Host, broker.Value.Host, broker.Value.Port));
                }
            }
        }
Esempio n. 2
0
 public DefaultCallbackHandler(ProducerConfiguration config,
                               IPartitioner <TK> partitioner,
                               IEncoder <TV> encoder,
                               IBrokerPartitionInfo brokerPartitionInfo,
                               ISyncProducerPool syncProducerPool)
 {
     this.producerConfig = config;
     this.partitioner    = partitioner;
     Logger.DebugFormat("partitioner  {0}", this.partitioner == null ? "Null" : this.partitioner.GetType().ToString());
     this.encoder             = encoder;
     this.syncProducerPool    = syncProducerPool;
     this.brokerPartitionInfo = brokerPartitionInfo;
 }
        void ZkAwareProducerSendsLotsOfMessagesAndSessionCreatedHandlerInvokedInTheBackgroundShouldNotThrowException_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (true)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                Producer <string, Message> producer            = e.Argument as Producer <string, Message>;
                IBrokerPartitionInfo       brokerPartitionInfo =
                    ReflectionHelper.GetInstanceField <IBrokerPartitionInfo>("brokerPartitionInfo", producer);
                IZooKeeperClient zkclient = ReflectionHelper.GetInstanceField <IZooKeeperClient>(
                    "zkclient", brokerPartitionInfo);
                var sessionCreatedHandler = ReflectionHelper.GetInstanceField <ZooKeeperClient.ZooKeeperEventHandler <ZooKeeperSessionCreatedEventArgs> >("sessionCreatedHandlers", zkclient);
                sessionCreatedHandler.Invoke(ZooKeeperSessionCreatedEventArgs.Empty);
                Thread.Sleep(1000);
            }
        }