示例#1
0
        /// <summary>
        /// Sends the messages.
        /// </summary>
        /// <param name="messages">The messages.</param>
        /// <param name="meta">The meta.</param>
        /// <returns></returns>
        /// <exception cref="DotNetWorkQueueException"></exception>
        private IEnumerable <QueueOutputMessage> SendMessages(
            IReadOnlyCollection <QueueMessage <IMessage, IAdditionalMessageData> > messages, byte[] meta)
        {
            var messagesToSend = BatchMessageShared.CreateMessagesToSend(_redisHeaders, messages, meta,
                                                                         _unixTimeFactory, _messageIdFactory, _serializer);

            try
            {
                var result = _enqueue.Execute(messagesToSend);
                if (result != null)
                {
                    return(BatchMessageShared.ProcessSentMessages(result, messages.Count, _sentMessageFactory));
                }
                throw new DotNetWorkQueueException("Returned no data when enqueue messages");
            }
            catch (Exception error)
            {
                var output = new List <QueueOutputMessage>(messages.Count);
                output.AddRange(
                    messages.Select(
                        message =>
                        new QueueOutputMessage(_sentMessageFactory.Create(null, message.MessageData.CorrelationId),
                                               error)));
                return(output);
            }
        }
示例#2
0
 /// <summary>
 /// Sends a new message to an existing queue
 /// </summary>
 /// <param name="messageToSend">The message to send.</param>
 /// <param name="data">The additional data.</param>
 /// <returns></returns>
 /// <exception cref="DotNetWorkQueueException"></exception>
 /// <exception cref="System.Exception">Failed to insert record</exception>
 /// <exception cref="System.ApplicationException"></exception>
 public IQueueOutputMessage Send(IMessage messageToSend, IAdditionalMessageData data)
 {
     try
     {
         var id = _dataStorage.SendMessage(messageToSend, data);
         return(new QueueOutputMessage(_sentMessageFactory.Create(new MessageQueueId(id), data.CorrelationId)));
     }
     catch (Exception exception)
     {
         return(new QueueOutputMessage(_sentMessageFactory.Create(null, data.CorrelationId), exception));
     }
 }
        /// <summary>
        /// Processes the sent messages.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <param name="messageCount">The message count.</param>
        /// <param name="sentMessageFactory">The sent message factory.</param>
        /// <returns></returns>
        /// <exception cref="DotNetWorkQueueException">$Failed to enqueue record(s). The LUA enqueue script returned the wrong count of items {result.Count}, expected {messageCount * 2}</exception>
        public static List <QueueOutputMessage> ProcessSentMessages(List <string> result, int messageCount, ISentMessageFactory sentMessageFactory)
        {
            var rc = new List <QueueOutputMessage>(messageCount);

            if (result.Count != messageCount * 2) //we get back 2 id's for each request - return count should be double
            {
                throw new DotNetWorkQueueException(
                          $"Failed to enqueue record(s). The LUA enqueue script returned the wrong count of items {result.Count}, expected {messageCount * 2}");
            }

            for (var i = 0; i < result.Count; i = i + 2) //note that we pull items as pairs
            {
                rc.Add(new QueueOutputMessage(sentMessageFactory.Create(new RedisQueueId(result[i]),
                                                                        new RedisQueueCorrelationId(new Guid(result[i + 1])))));
            }
            return(rc);
        }
示例#4
0
        /// <summary>
        /// Sends the messages.
        /// </summary>
        /// <param name="messages">The messages.</param>
        /// <param name="meta">The meta.</param>
        /// <returns></returns>
        private async Task <IEnumerable <QueueOutputMessage> > SendMessagesAsync(IReadOnlyCollection <QueueMessage <IMessage, IAdditionalMessageData> > messages, byte[] meta)
        {
            var messagesToSend = BatchMessageShared.CreateMessagesToSend(_redisHeaders, messages, meta,
                                                                         _unixTimeFactory, _messageIdFactory, _serializer);

            try
            {
                var result = await _enqueue.ExecuteAsync(messagesToSend).ConfigureAwait(false);

                return(BatchMessageShared.ProcessSentMessages(result, messages.Count, _sentMessageFactory));
            }
            catch (Exception error)
            {
                var output = new List <QueueOutputMessage>(messages.Count);
                output.AddRange(messages.Select(message => new QueueOutputMessage(_sentMessageFactory.Create(null, message.MessageData.CorrelationId), error)));
                return(output);
            }
        }
示例#5
0
        /// <summary>
        /// Sends the specified message.
        /// </summary>
        /// <param name="messageToSend">The message to send.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        /// <exception cref="DotNetWorkQueueException">An error occurred while sending a message</exception>
        public IQueueOutputMessage Send(IMessage messageToSend, IAdditionalMessageData data)
        {
            try
            {
                //correlationID must be stored as a message header
                messageToSend.SetHeader(_headers.CorrelationId, new RedisQueueCorrelationIdSerialized((Guid)data.CorrelationId.Id.Value));

                var messageId = _sendMessage.Handle(new SendMessageCommand(messageToSend, data));
                if (messageId == "JobAlreadyExists")
                {
                    return(new QueueOutputMessage(_sentMessageFactory.Create(null, data.CorrelationId), new DotNetWorkQueueException("Failed to enqueue a record. The job already exists")));
                }
                return(new QueueOutputMessage(_sentMessageFactory.Create(new RedisQueueId(messageId), data.CorrelationId)));
            }
            catch (Exception exception)
            {
                return(new QueueOutputMessage(_sentMessageFactory.Create(null, data.CorrelationId), exception));
            }
        }
        /// <summary>
        /// Processes the sent messages.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <param name="messageCount">The message count.</param>
        /// <param name="sentMessageFactory">The sent message factory.</param>
        /// <returns></returns>
        /// <exception cref="DotNetWorkQueueException">$Failed to enqueue record(s). The LUA enqueue script returned the wrong count of items {result.Count}, expected {messageCount * 2}</exception>
        public static List<QueueOutputMessage> ProcessSentMessages(List<string> result, int messageCount, ISentMessageFactory sentMessageFactory)
        {
            var rc = new List<QueueOutputMessage>(messageCount);
            if (result.Count != messageCount * 2) //we get back 2 id's for each request - return count should be double
            {
                throw new DotNetWorkQueueException(
                    $"Failed to enqueue record(s). The LUA enqueue script returned the wrong count of items {result.Count}, expected {messageCount * 2}");
            }

            for (var i = 0; i < result.Count; i = i + 2) //note that we pull items as pairs
            {
                rc.Add(new QueueOutputMessage(sentMessageFactory.Create(new RedisQueueId(result[i]),
                     new RedisQueueCorrelationId(new Guid(result[i + 1])))));
            }
            return rc;
        }