예제 #1
0
 /// <summary>
 /// Declare Queueu
 /// </summary>
 /// <param name="channel"></param>
 /// <param name="model"></param>
 public void QueueDeclare(IModel channel, QueueDeclareModel model)
 {
     if (model == null)
     {
         return;
     }
     channel.QueueDeclare(queue: model.Queue, durable: model.Durable, exclusive: model.Exclusive, autoDelete: model.AutoDelete, arguments: model.Arguments);
 }
예제 #2
0
 /// <summary>
 /// Create Channel
 /// </summary>
 /// <param name="queue">queue name</param>
 /// <param name="body"></param>
 /// <param name="queueDeclare"></param>
 public void CreateChannel(string queue, byte[] body, QueueDeclareModel queueDeclare = null)
 {
     using (var connection = factory.CreateConnection())
         using (var channel = connection.CreateModel())
         {
             QueueDeclare(channel, queueDeclare);
             channel.BasicPublish(exchange: "", routingKey: queue, basicProperties: null, body: body);
         }
 }
예제 #3
0
 /// <summary>
 /// Basic consume
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="received"></param>
 /// <returns>connection key of consume</returns>
 public string BasicConsume(QueueDeclareModel queue, Func<BasicDeliverEventArgsModel, bool> received)
 {
     return helper.BasicConsume(queue.Queue, received,
         queueDeclare: queue,
         consume: new BasicConsumeModel
         {
             AutoAck = false
         }, qos: new BasicQosModel
         {
             PrefetchCount = 1
         });
 }
예제 #4
0
 /// <summary>
 /// send data
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="body"></param>
 public void Send(QueueDeclareModel queue, object body)
 {
     helper.Send(queue.Queue, ConversionHelper.ObjectToByteArray(body), queue);
 }
예제 #5
0
 /// <summary>
 /// Send message
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="message"></param>
 public void Send(QueueDeclareModel queue, string message)
 {
     helper.Send(queue.Queue, message, queue);
 }
예제 #6
0
        /// <summary>
        /// Basic Consume
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="received"></param>
        /// <param name="consume"></param>
        /// <param name="qos"></param>
        /// <param name="queueDeclare"></param>
        /// <returns></returns>
        public string BasicConsume(string queue, Func <BasicDeliverEventArgsModel, bool> received, BasicConsumeModel consume = null, BasicQosModel qos = null, QueueDeclareModel queueDeclare = null)
        {
            string connectionKey = Guid.NewGuid().ToString();

            var connection = factory.CreateConnection();
            var channel    = connection.CreateModel();

            connections.Add(connectionKey, connection);

            QueueDeclare(channel, queueDeclare);
            BasicQos(channel, qos);

            Console.WriteLine("RabbitMQ: Waiting for messages.");

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, e) =>
            {
                var res = received(new BasicDeliverEventArgsModel
                {
                    Body        = e.Body,
                    ConsumerTag = e.ConsumerTag,
                    DeliveryTag = e.DeliveryTag,
                    Exchange    = e.Exchange,
                    Redelivered = e.Redelivered,
                    RoutingKey  = e.RoutingKey,
                });
                if (!res)
                {
                    return;
                }
                channel.BasicAck(deliveryTag: e.DeliveryTag, multiple: qos?.PrefetchCount > 1);
            };
            if (consume == null)
            {
                consume = new BasicConsumeModel();
            }
            channel.BasicConsume(queue, consume.AutoAck, consume.ConsumerTag, consume.NoLocal, consume.Exclusive, consume.Arguments, consumer);

            return(connectionKey);
        }
예제 #7
0
 public void Send(string queue, byte[] body, QueueDeclareModel queueDeclare = null)
 {
     CreateChannel(queue, body, queueDeclare);
 }
예제 #8
0
 /// <summary>
 /// Send message to queue
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="message"></param>
 /// <param name="queueDeclare"></param>
 public void Send(string queue, string message, QueueDeclareModel queueDeclare = null)
 {
     CreateChannel(queue, Encoding.UTF8.GetBytes(message), queueDeclare);
 }