/// <summary> /// Gets the RabbitMQ queue name for the specified queue. /// </summary> /// <param name="queue">Queue to get the name of.</param> /// <returns>The name of the queue.</returns> private static string GetQueueNameFromEnum(RabbitMqQueue queue) { const string manta_queue_prefix = "manta_mta_"; switch (queue) { case RabbitMqQueue.Inbound: return(manta_queue_prefix + "inbound"); case RabbitMqQueue.InboundStaging: return(manta_queue_prefix + "inbound_staging"); case RabbitMqQueue.OutboundWaiting: return(manta_queue_prefix + "outbound_waiting"); case RabbitMqQueue.OutboundWait1: return(manta_queue_prefix + "outbound_wait_____1"); case RabbitMqQueue.OutboundWait10: return(manta_queue_prefix + "outbound_wait____10"); case RabbitMqQueue.OutboundWait60: return(manta_queue_prefix + "outbound_wait____60"); case RabbitMqQueue.OutboundWait300: return(manta_queue_prefix + "outbound_wait___300"); default: throw new Exception("Cannot get name for RabbitMqQueue"); } }
protected override void TestSetUp() { base.TestSetUp(); _factory = new RabbitMqQueueFactory(); inboxQueue = _factory.Create(new Uri("rabbitmq://localhost/rmq_inbox")) as RabbitMqQueue; outboxQueue = _factory.Create(new Uri("rabbitmq://./rmq_outbox")) as RabbitMqQueue; inboxQueue.Create(); outboxQueue.Create(); }
/// <summary> /// Publishes the specified message to the specified queue. /// </summary> /// <param name="message">Message to queue.</param> /// <param name="queue">Queue to place message in.</param> /// <param name="priority">Priority of message.</param> public static bool Publish(byte[] message, RabbitMqQueue queue, bool noConfirm, RabbitMqPriority priority) { var pChannel = GetPublishChannel(queue, noConfirm); lock (pChannel.Lock) { var channel = pChannel.Channel; var msgProps = channel.CreateBasicProperties(); msgProps.Persistent = true; msgProps.Priority = (byte)priority; channel.BasicPublish(string.Empty, GetQueueNameFromEnum(queue), true, msgProps, message); return(noConfirm ? true : channel.WaitForConfirms()); } }
private void ProcessQueue(RabbitMqConnectionSettings connectionSettings, RabbitMqQueue queue) { var queueSettings = TryGetQueueSettings(connectionSettings, queue); var maxMessagesCount = queueSettings?.MaxMessagesCount ?? connectionSettings.MaxMessagesCount ?? _maxMessagesCount; if (queue.Messages >= maxMessagesCount) { var title = connectionSettings.Title ?? new Uri(connectionSettings.Url).Host; _log.WriteMonitor( title, string.Empty, $"Queue '{queue.Name}' contains {queue.Messages} messages"); } }
/// <summary> /// Dequeues the specified amount of messages from the queue. If there are less messages queued than request will give less. /// </summary> /// <param name="queue">The queue to dequeue from.</param> /// <param name="maxItems">The maximum amount of messages to dequeue.</param> /// <param name="millisecondsTimeout">If queue is empty the max time to wait for more to appear.</param> /// <returns>List of BasicDeliverEventArgs.</returns> public static List <BasicDeliverEventArgs> Dequeue(RabbitMqQueue queue, int maxItems, int millisecondsTimeout) { GetChannel(queue); List <BasicDeliverEventArgs> items = new List <BasicDeliverEventArgs>(); QueueingBasicConsumer consumer = GetQueueingBasicConsumer(queue); while (items.Count < maxItems) { BasicDeliverEventArgs ea = null; if (!consumer.Queue.Dequeue(millisecondsTimeout, out ea)) { break; } items.Add(ea); } return(items); }
/// <summary> /// Gets the Common AMQP model for the specified queue, using the the specified connection. /// </summary> /// <param name="queue">The queue to get the AMQP model for.</param> /// <returns>Common AMQP model.</returns> private static IModel GetChannel(RabbitMqQueue queue) { lock (_GetChannelLock) { if (!_Channels.ContainsKey(queue)) { _Channels[queue] = null; } IModel channel = _Channels[queue]; // If the channel to the specified queue doesn't exist then we need to create it. if (channel == null) { if (!LocalhostConnection.IsOpen) { LocalhostConnection = new ConnectionFactory { HostName = MtaParameters.RabbitMQ.Hostname, UserName = MtaParameters.RabbitMQ.Username, Password = MtaParameters.RabbitMQ.Password } }
/// <summary> /// Publishes the specified message to the specified queue. /// </summary> /// <param name="message">Message to queue.</param> /// <param name="queue">Queue to place message in.</param> public static bool Publish(byte[] message, RabbitMqQueue queue, bool noConfirm) { PublishChannel pChannel = GetPublishChannel(queue, noConfirm); lock (pChannel.Lock) { IModel channel = pChannel.Channel; IBasicProperties msgProps = channel.CreateBasicProperties(); msgProps.Persistent = true; channel.BasicPublish(string.Empty, GetQueueNameFromEnum(queue), true, msgProps, message); if (noConfirm) { return(true); } if (!channel.WaitForConfirms()) { return(false); } return(true); } }
/// <summary> /// /// </summary> /// <param name="queue"></param> /// <returns></returns> public static PublishChannel GetPublishChannel(RabbitMqQueue queue, bool noConfirm) { IModel channel = GetChannel(queue); // Ensure the Queue exists. if (noConfirm) { return(new PublishChannel(channel)); } int threadID = Thread.CurrentThread.ManagedThreadId; lock (_PublishChannelsLock) { if (!_PublishChannels.ContainsKey(threadID)) { PublishChannel pChannel = new PublishChannel(LocalhostConnection.CreateModel()); pChannel.Channel.ConfirmSelect(); _PublishChannels.Add(threadID, pChannel); } } return(_PublishChannels[threadID]); }
protected TestFixture(HostFixture hostFixture, ITestOutputHelper output) { Host = hostFixture.Host; HostAddress = hostFixture.HostAddress; SetupDatabase(hostFixture.AppConfiguration); var rabbitMqConfig = hostFixture.AppConfiguration .GetSection(ConfigKeys.RabbitMq) .Get <RabbitMqConfig>(); _rabbitMqQueue = new RabbitMqQueue( rabbitMqConfig.HostUrl, rabbitMqConfig.User, rabbitMqConfig.Password); _rabbitMqQueue.ConfigureListeningQueue( _integrationTestsExpenseQueue, typeof(V1.Messages.ExpenseCreatedEvent)); ExchangeServiceApi = new ExchangeServiceApi(hostFixture.AppConfiguration .GetSection(ConfigKeys.ExchangeService) .Get <ExchangeServiceConfig>()); }
/// <summary> /// (Spec method) Acknowledge one or more delivered message(s). /// </summary> /// <param name="queue">Queue the message or messages are from.</param> /// <param name="deliveryTag">ID of the message delivery.</param> /// <param name="multiple">Ack all deliverys upto and including specified.</param> public static void Ack(RabbitMqQueue queue, ulong deliveryTag, bool multiple) { IModel channel = GetChannel(queue); channel.BasicAck(deliveryTag, multiple); }
/// <summary> /// Publishes the specified message to the specified queue. /// </summary> /// <param name="message">Message to queue.</param> /// <param name="queue">Queue to place message in.</param> /// <param name="priority">Priority of message.</param> public static async Task <bool> Publish(object obj, RabbitMqQueue queue, bool confirm = true, RabbitMqPriority priority = RabbitMqPriority.Low) { byte[] bytes = await Serialisation.Serialise(obj); return(Publish(bytes, queue, !confirm, priority)); }
private static RabbitMqQueueSettings TryGetQueueSettings(RabbitMqConnectionSettings connectionSettings, RabbitMqQueue queue) { if (connectionSettings.Queues != null) { connectionSettings.Queues.TryGetValue(queue.Name, out var queueSettings); if (queueSettings == null) { queueSettings = CheckByRegExp(connectionSettings, queue.Name); } return(queueSettings); } return(null); }
/// <summary> /// Publishes the specified message to the specified queue. /// </summary> /// <param name="message">Message to queue.</param> /// <param name="queue">Queue to place message in.</param> public static bool Publish(object obj, RabbitMqQueue queue, bool confirm = true) { byte[] bytes = Serialisation.Serialise(obj); return(Publish(bytes, queue, !confirm)); }