Exemple #1
0
        public void Consumer()
        {
            DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("BroadcastConsumer");

            consumer.setMessageModel(MessageModel.BROADCASTING);

            consumer.subscribe("TopicBroadcast", "*");

            consumer.registerMessageListener(new MessageListener());

            consumer.start();
            Console.WriteLine("Consumer started .....");
        }
        static void Main(string[] args)
        {
            DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("BroadcastConsumerGroup");

            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);


            consumer.setMessageModel(MessageModel.BROADCASTING);

            consumer.subscribe("BroadTopicTest", "TagA");

            consumer.registerMessageListener(new TestListener());

            consumer.start();
            Console.WriteLine("Broadcast Consumer Started.");
        }
Exemple #3
0
        /// <summary>
        /// 创建消费者
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        public static DefaultMQPushConsumer CreateDefaultMQPushConsumer <T>(string groupName) where T : MessageListenerConcurrently
        {
            var consumer = consumers.Where(o => o.getConsumerGroup() == groupName).FirstOrDefault();

            if (consumer == null) //双if +lock
            {
                lock (consumer_lock)
                {
                    consumer = consumers.Where(o => o.getConsumerGroup() == groupName).FirstOrDefault();
                    if (consumer == null)
                    {
                        consumer = new DefaultMQPushConsumer(groupName);
                        consumer.setNamesrvAddr(namesrvAddr);
                        consumer.setMessageModel(MessageModel.CLUSTERING);
                        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
                        consumer.registerMessageListener(Activator.CreateInstance <T>());
                        consumer.start();
                        consumers.Add(consumer);
                    }
                }
            }
            return(consumer);
        }