Пример #1
0
        public void Subscribe(string channelName)
        {
            using (var redisClient = new RedisClient(_connection.Url, _connection.Port))
                using (_subscription = redisClient.CreateSubscription())
                {
                    _subscription.OnSubscribe = channel =>
                    {
                        _logging.Info(String.Format("Redis Started Listening On to '{0}'", channel));
                    };
                    _subscription.OnUnSubscribe = channel =>
                    {
                        _logging.Info(String.Format("Redis UnSubscribed from '{0}'", channel));
                        redisClient.UnSubscribe();
                    };
                    _subscription.OnMessage = (channel, msg) =>
                    {
                        _logging.Info(String.Format("Received '{0}' from channel '{1}'", msg, channel));

                        MessageReceiveEventArgs receivedMsg = new Util.MessageReceiveEventArgs(msg);
                        OnMessageReceivedEventHandler(this, receivedMsg);
                    };

                    if (OnSubscribeDoneEventHandler != null)
                    {
                        SubscribeDoneEventArgs subscribeDoneEventMsg = new SubscribeDoneEventArgs(true);
                        OnSubscribeDoneEventHandler(this, subscribeDoneEventMsg);
                    }

                    _subscription.SubscribeToChannels(channelName); //blocking
                }
        }
Пример #2
0
        public void Subscribe(string channel)
        {
            using (var connection = _connectionFactory.CreateConnection())
                using (_channelMq = connection.CreateModel())
                {
                    cancelled = false;

                    _channelMq.QueueDeclare(channel, true, false, false, null);

                    var consumer = new EventingBasicConsumer(_channelMq);
                    EventHandler <BasicDeliverEventArgs> queueMsgReceived = (model, ea) =>
                    {
                        var body    = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        _logging.Info("Rabbit Received {0}", message);

                        MessageReceiveEventArgs receivedMsg = new Util.MessageReceiveEventArgs(message);
                        OnMessageReceivedEventHandler(this, receivedMsg);
                    };
                    consumer.Received += queueMsgReceived;
                    string c = _channelMq.BasicConsume(channel, true, consumer);

                    Console.CancelKeyPress += (_, e) =>
                    {
                        e.Cancel  = true; // prevent the process from terminating.
                        cancelled = true;
                    };

                    if (OnSubscribeDoneEventHandler != null)
                    {
                        SubscribeDoneEventArgs subscribeDoneEventMsg = new SubscribeDoneEventArgs(true);
                        OnSubscribeDoneEventHandler(this, subscribeDoneEventMsg);
                    }

                    while (!cancelled)
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(10));
                    }

                    consumer.Received -= queueMsgReceived;
                }
        }
Пример #3
0
        public void Subscribe(string channel)
        {
            using (_consumerKafka =
                       new Consumer <Null, string>(constructConfig(_brokerList, true, new Guid().ToString()), null, new StringDeserializer(Encoding.UTF8)))
            {
                // Note: All event handlers are called on the main thread.

                _consumerKafka.OnMessage += (_, msg)
                                            =>
                {
                    _logging.Info($"Executor ID : {_executorID} Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");

                    MessageReceiveEventArgs receivedMsg = new Util.MessageReceiveEventArgs(msg.Value);
                    OnMessageReceivedEventHandler(this, receivedMsg);
                };

                _consumerKafka.Subscribe(channel);

                if (OnSubscribeDoneEventHandler != null)
                {
                    SubscribeDoneEventArgs subscribeDoneEventMsg = new SubscribeDoneEventArgs(true);
                    OnSubscribeDoneEventHandler(this, subscribeDoneEventMsg);
                }


                var cancelled = false;
                Console.CancelKeyPress += (_, e) =>
                {
                    e.Cancel  = true; // prevent the process from terminating.
                    cancelled = true;
                };

                while (!cancelled)
                {
                    _consumerKafka.Poll(TimeSpan.FromMilliseconds(10));
                }
            }
        }