/// <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); }
/// <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); } }
/// <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 }); }
/// <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); }
/// <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); }
/// <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); }
public void Send(string queue, byte[] body, QueueDeclareModel queueDeclare = null) { CreateChannel(queue, body, queueDeclare); }
/// <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); }