/// <summary> /// Sends a message /// </summary> /// <param name="message"></param> /// <param name="queueName"></param> /// <param name="applicationId"></param> /// <param name="basicProperties"></param> /// <returns>Null on success or the text of an error message</returns> public string Send(IRawMessage message, string queueName, string applicationId, IBasicProperties basicProperties) { Verify.RequireNotNull(message, "message"); Verify.RequireStringNotNullOrWhitespace(queueName, "queueName"); Verify.RequireStringNotNullOrWhitespace(applicationId, "applicationId"); try { var application = settings.Applications.FirstOrDefault(x => x.ApplicationId == applicationId); if (application == null) { return(string.Format("Could not find configuration for application id {0}", applicationId)); } using ( var connection = rabbitConnectionFactory.Create(application.RabbitConnectionString).CreateConnection()) { using (var channel = connection.CreateModel()) { var sendData = message.GetEelementsForRabbitPublish(); basicProperties.Headers = sendData.Item2; channel.BasicPublish(queueName, "", basicProperties, sendData.Item1); return(null); } } } catch (Exception err) { logger.ErrorException("Failed to send message for retry", err); return(string.Format("Failed to send message for retry. Error is {0}. See error log for details", err.Message)); } }
private void InnerPoll() { if (cancellationToken.IsCancellationRequested) { return; } logger.Info("Started queue poller for {0} with audit expiration of {1} hours and error expiration of {2} hours", QueueSettings.LogInfo, QueueSettings.DocumentExpirationInHours, QueueSettings.ErrorDocumentExpirationInHours); try { using ( var connection = rabbitConnectionFactory.Create(QueueSettings.RabbitConnectionString, (ushort)QueueSettings.HeartbeatIntervalSeconds).CreateConnection()) { using (var channel = connection.CreateModel()) { channel.BasicQos(0, QueueSettings.Prefetch, false); var consumer = new QueueingBasicConsumer(channel); channel.BasicConsume(QueueSettings.QueueName, false, consumer); logger.Info("Begin polling {0}{1}", QueueSettings.LogInfo, QueueSettings.MaxMessagesPerRun > 0 ? string.Format(" to read a maximum of {0} messages", QueueSettings.MaxMessagesPerRun) : ""); long messageCount = 0; while (!cancellationToken.IsCancellationRequested) { BasicDeliverEventArgs ea = null; try { YieldToOtherPollers(); consumer.Queue.Dequeue(QueueSettings.PollingTimeoutMilliseconds, out ea); } catch (Exception err) { logger.ErrorException("Dequeue error ", err); throw; } logger.Trace("Dequeue completed for {0}{1}", QueueSettings.LogInfo, ea == null ? " without a message (timeout)" : " with a message"); if (ea != null) { try { HandleMessage(new RawMessage(ea)); channel.BasicAck(ea.DeliveryTag, false); messageCount++; if (QueueSettings.MaxMessagesPerRun > 0 && messageCount >= QueueSettings.MaxMessagesPerRun) { break; } } catch (Exception err) { channel.BasicNack(ea.DeliveryTag, false, true); logger.Error("Error on {0} with details {1}", QueueSettings.LogInfo, err); //todo: move the message out of the way! //todo: make this a bit more resilient (retries etc.) throw; } } consecutiveRetryCount = 0; } } } } catch (EndOfStreamException err) { logger.ErrorException($"Error on {QueueSettings.LogInfo}", err); throw; } }