コード例 #1
0
        public Consumer GetConsumer(String user, String tag)
        {
            var cs = Consumers.GetOrAdd(user, e => new Consumer(this, e));

            if (!tag.IsNullOrEmpty())
            {
                cs.Tag = tag;
            }

            return(cs);
        }
コード例 #2
0
    public virtual IConsumer <string, byte[]> Get(string groupId, string connectionName = null)
    {
        connectionName ??= KafkaConnections.DefaultConnectionName;

        return(Consumers.GetOrAdd(
                   connectionName, connection => new Lazy <IConsumer <string, byte[]> >(() =>
        {
            var config = new ConsumerConfig(Options.Connections.GetOrDefault(connection))
            {
                GroupId = groupId,
                EnableAutoCommit = false
            };

            Options.ConfigureConsumer?.Invoke(config);
            return new ConsumerBuilder <string, byte[]>(config).Build();
        })
                   ).Value);
    }
コード例 #3
0
        /// <summary>订阅主题</summary>
        /// <param name="user">消费者</param>
        /// <param name="tag">标签。消费者用于在主题队列内部过滤消息</param>
        /// <param name="onMessage">消费消息的回调函数</param>
        /// <param name="userState">订阅者</param>
        /// <returns></returns>
        public Boolean Add(String user, String tag, Func <Subscriber, Message, Task> onMessage, Object userState)
        {
            //if (Subscribers.ContainsKey(user)) return false;
            //Consumer cs = null;
            //if (!Consumers.TryGetValue(user, out cs))
            //{
            //    // 新增消费者集群
            //    cs = new Consumer(user);
            //    Consumers[user] = cs;
            //}
            // 新增消费者集群
            var cs = Consumers.GetOrAdd(user, e => new Consumer(e)
            {
                Host = this
            });

            cs.Add(userState, tag, onMessage);

            // 可能是第一个订阅者,赶紧消费积累下来的消息
            Notify();

            return(true);
        }