Exemple #1
0
        public void Register <T>(ListenerInfo queueInfo, Func <T, Task> callback)
        {
            var connection = Factory.CreateConnection();
            var channel    = connection.CreateModel();

            string jsonResult = String.Empty;
            var    ouOk       = channel.QueueDeclare(queue: queueInfo.QueueName, durable: false,
                                                     exclusive: false,
                                                     autoDelete: false,
                                                     arguments: null);

            var consumer = new AsyncEventingBasicConsumer(channel);

            Console.WriteLine($"Read QN: {ouOk.QueueName} CC: {ouOk.ConsumerCount} MC: {ouOk.MessageCount}");

            consumer.Received += async(sender, @event) =>
            {
                jsonResult = Encoding.UTF8.GetString(@event.Body);
                T t = JsonConvert.DeserializeObject <T>(jsonResult);
                await callback.Invoke(t);
            };


            channel.BasicConsume(queue: queueInfo.RoutingKey, autoAck: true, consumer: consumer);
        }
Exemple #2
0
        public async Task <T> ReadAsync <T>(ListenerInfo queueInfo)
        {
            using (var connection = Factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    string jsonResult = String.Empty;
                    var    ouOk       = channel.QueueDeclare(queue: queueInfo.QueueName, durable: false,
                                                             exclusive: false,
                                                             autoDelete: false,
                                                             arguments: null);

                    var consumer = new AsyncEventingBasicConsumer(channel);
                    Console.WriteLine($"Read QN: {ouOk.QueueName} CC: {ouOk.ConsumerCount} MC: {ouOk.MessageCount}");

                    var data = await Task.Run(() => channel.BasicGet(queueInfo.RoutingKey, true)).ConfigureAwait(false);

                    channel.BasicConsume(queue: queueInfo.RoutingKey, autoAck: true, consumer: consumer);
                    if (data != null)
                    {
                        return(JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(data.Body)));
                    }
                }

            return(default(T));
        }
Exemple #3
0
        public void RegisterAsync(ListenerInfo queueInfo, AsyncEventHandler <BasicDeliverEventArgs> consumerOnReceived)
        {
            string jsonResult = String.Empty;
            var    ouOk       = channel.QueueDeclare(queue: queueInfo.RoutingKey, durable: false,
                                                     exclusive: false,
                                                     autoDelete: false,
                                                     arguments: null);

            var consumer = new AsyncEventingBasicConsumer(channel);

            // DEBUG: Console.WriteLine($"Read QN: {ouOk.QueueName} CC: {ouOk.ConsumerCount} MC: {ouOk.MessageCount}");
            consumer.Received += consumerOnReceived;
            channel.BasicConsume(queue: queueInfo.RoutingKey, autoAck: true, consumer: consumer);
        }