Пример #1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="KafkaProducerEndpoint" /> class.
        /// </summary>
        /// <param name="nameResolverType">
        ///     The type of the <see cref="IKafkaProducerEndpointNameResolver" /> to be used to resolve the actual
        ///     endpoint name.
        /// </param>
        /// <param name="clientConfig">
        ///     The <see cref="KafkaClientConfig" /> to be used to initialize the
        ///     <see cref="KafkaProducerConfig" />.
        /// </param>
        public KafkaProducerEndpoint(Type nameResolverType, KafkaClientConfig?clientConfig = null)
            : base(nameResolverType)
        {
            Configuration = new KafkaProducerConfig(clientConfig);

            if (!typeof(IKafkaProducerEndpointNameResolver).IsAssignableFrom(nameResolverType))
            {
                throw new ArgumentException(
                          "The specified type must implement IKafkaProducerEndpointNameResolver.",
                          nameof(nameResolverType));
            }

            _partitionFunction = (envelope, serviceProvider) =>
                                 ((IKafkaProducerEndpointNameResolver)serviceProvider.GetRequiredService(nameResolverType))
                                 .GetPartition(envelope) ?? Partition.Any;
        }
Пример #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="KafkaProducerEndpoint" /> class.
        /// </summary>
        /// <param name="nameFunction">
        ///     The function returning the endpoint name for the message being produced.
        /// </param>
        /// <param name="partitionFunction">
        ///     The optional function returning the target partition index for the message being produced. If <c>null</c>
        ///     the partition is automatically derived from the message key (use <see cref="KafkaKeyMemberAttribute" />
        ///     to specify a message key, otherwise a random one will be generated).
        /// </param>
        /// <param name="clientConfig">
        ///     The <see cref="KafkaClientConfig" /> to be used to initialize the
        ///     <see cref="KafkaProducerConfig" />.
        /// </param>
        public KafkaProducerEndpoint(
            Func <IOutboundEnvelope, string> nameFunction,
            Func <IOutboundEnvelope, int>?partitionFunction,
            KafkaClientConfig?clientConfig = null)
            : base(nameFunction)
        {
            Configuration = new KafkaProducerConfig(clientConfig);

            if (partitionFunction != null)
            {
                _partitionFunction = (envelope, _) => partitionFunction.Invoke(envelope);
            }
            else
            {
                _partitionFunction = AnyPartitionFunction;
            }
        }
Пример #3
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="KafkaProducerEndpoint" /> class.
        /// </summary>
        /// <param name="name">
        ///     The name of the topic.
        /// </param>
        /// <param name="partition">
        ///     The optional partition index. If <c>null</c> the partition is automatically derived from the message
        ///     key (use <see cref="KafkaKeyMemberAttribute" /> to specify a message key, otherwise a random one will be
        ///     generated).
        /// </param>
        /// <param name="clientConfig">
        ///     The <see cref="KafkaClientConfig" /> to be used to initialize the
        ///     <see cref="KafkaProducerConfig" />.
        /// </param>
        public KafkaProducerEndpoint(
            string name,
            int?partition,
            KafkaClientConfig?clientConfig = null)
            : base(name)
        {
            if (partition != null && partition < Partition.Any)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(partition),
                          "The partition index must be greater or equal to 0, or Partition.Any (-1).");
            }

            Configuration = new KafkaProducerConfig(clientConfig);

            if (partition != null)
            {
                _partitionFunction = (_, _) => partition.Value;
            }
            else
            {
                _partitionFunction = AnyPartitionFunction;
            }
        }