Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SendMessageCommandBatchHandler" /> class.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <param name="enqueue">The enqueue.</param>
        /// <param name="unixTimeFactory">The unix time factory.</param>
        /// <param name="messageIdFactory">The message identifier factory.</param>
        /// <param name="sentMessageFactory">The sent message factory.</param>
        /// <param name="redisHeaders">The redis headers.</param>
        /// <param name="sendBatchSize">Size of the send batch.</param>
        public SendMessageCommandBatchHandler(
            ICompositeSerialization serializer,
            EnqueueBatchLua enqueue,
            IUnixTimeFactory unixTimeFactory,
            IGetMessageIdFactory messageIdFactory,
            ISentMessageFactory sentMessageFactory,
            RedisHeaders redisHeaders,
            ISendBatchSize sendBatchSize)
        {
            Guard.NotNull(() => serializer, serializer);
            Guard.NotNull(() => enqueue, enqueue);
            Guard.NotNull(() => unixTimeFactory, unixTimeFactory);
            Guard.NotNull(() => messageIdFactory, messageIdFactory);
            Guard.NotNull(() => sentMessageFactory, sentMessageFactory);
            Guard.NotNull(() => redisHeaders, redisHeaders);
            Guard.NotNull(() => sendBatchSize, sendBatchSize);

            _serializer         = serializer;
            _enqueue            = enqueue;
            _messageIdFactory   = messageIdFactory;
            _sentMessageFactory = sentMessageFactory;
            _redisHeaders       = redisHeaders;
            _sendBatchSize      = sendBatchSize;
            _unixTimeFactory    = unixTimeFactory;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SendMessageCommandHandler" /> class.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="enqueueLua">The enqueue.</param>
        /// <param name="enqueueDelayedLua">The enqueue delayed.</param>
        /// <param name="enqueueExpirationLua">The enqueue expiration.</param>
        /// <param name="enqueueDelayedAndExpirationLua">The enqueue delayed and expiration.</param>
        /// <param name="unixTimeFactory">The unix time factory.</param>
        /// <param name="messageIdFactory">The message identifier factory.</param>
        /// <param name="jobSchedulerMetaData">The job scheduler meta data.</param>
        public SendMessageCommandHandlerAsync(
            ICompositeSerialization serializer,
            IHeaders headers,
            EnqueueLua enqueueLua,
            EnqueueDelayedLua enqueueDelayedLua,
            EnqueueExpirationLua enqueueExpirationLua,
            EnqueueDelayedAndExpirationLua enqueueDelayedAndExpirationLua,
            IUnixTimeFactory unixTimeFactory,
            IGetMessageIdFactory messageIdFactory,
            IJobSchedulerMetaData jobSchedulerMetaData)
        {
            Guard.NotNull(() => serializer, serializer);
            Guard.NotNull(() => headers, headers);
            Guard.NotNull(() => enqueueLua, enqueueLua);
            Guard.NotNull(() => enqueueDelayedLua, enqueueDelayedLua);
            Guard.NotNull(() => enqueueExpirationLua, enqueueExpirationLua);
            Guard.NotNull(() => enqueueDelayedAndExpirationLua, enqueueDelayedAndExpirationLua);
            Guard.NotNull(() => unixTimeFactory, unixTimeFactory);
            Guard.NotNull(() => messageIdFactory, messageIdFactory);

            _serializer           = serializer;
            _headers              = headers;
            _enqueueLua           = enqueueLua;
            _messageIdFactory     = messageIdFactory;
            _jobSchedulerMetaData = jobSchedulerMetaData;

            _enqueueDelayedLua = enqueueDelayedLua;
            _enqueueDelayedAndExpirationLua = enqueueDelayedAndExpirationLua;
            _unixTimeFactory      = unixTimeFactory;
            _enqueueExpirationLua = enqueueExpirationLua;
        }
        /// <summary>
        /// Creates the messages to send.
        /// </summary>
        /// <param name="redisHeaders">The redis headers.</param>
        /// <param name="messages">The messages.</param>
        /// <param name="meta">The meta data, already serialized.</param>
        /// <param name="unixTimeFactory">The unix time factory.</param>
        /// <param name="messageIdFactory">The message identifier factory.</param>
        /// <param name="serializer">The composite serializer.</param>
        /// <returns></returns>
        public static List <EnqueueBatchLua.MessageToSend> CreateMessagesToSend(RedisHeaders redisHeaders,
                                                                                IReadOnlyCollection <QueueMessage <IMessage, IAdditionalMessageData> > messages,
                                                                                byte[] meta,
                                                                                IUnixTimeFactory unixTimeFactory,
                                                                                IGetMessageIdFactory messageIdFactory,
                                                                                ICompositeSerialization serializer)
        {
            var messagesToSend = new ConcurrentBag <EnqueueBatchLua.MessageToSend>();

            Parallel.ForEach(messages, m =>
            {
                //the correlation ID must be saved as a message header
                m.Message.SetHeader(redisHeaders.CorrelationId, new RedisQueueCorrelationIdSerialized((Guid)m.MessageData.CorrelationId.Id.Value));

                //check for delay and expiration
                long unixTimeStampDelay      = 0;
                long unixTimeStampExpiration = 0;
                if (m.MessageData.GetDelay().HasValue)
                {
                    // ReSharper disable once PossibleInvalidOperationException
                    unixTimeStampDelay = unixTimeFactory.Create().GetAddDifferenceMilliseconds(m.MessageData.GetDelay().Value);
                }
                // ReSharper disable once PossibleInvalidOperationException
                if (m.MessageData.GetExpiration().HasValue&& m.MessageData.GetExpiration().Value != TimeSpan.Zero)
                {
                    var unixTime = unixTimeFactory.Create();
                    var timeSpan = m.MessageData.GetExpiration();
                    if (timeSpan != null)
                    {
                        var timespan            = timeSpan.Value;
                        unixTimeStampExpiration = unixTime.GetAddDifferenceMilliseconds(timespan);
                    }
                }

                var serialized = serializer.Serializer.MessageToBytes(new MessageBody {
                    Body = m.Message.Body
                }, m.Message.Headers);
                m.Message.SetHeader(redisHeaders.Headers.StandardHeaders.MessageInterceptorGraph, serialized.Graph);

                messagesToSend.Add(new EnqueueBatchLua.MessageToSend
                {
                    Message         = serialized.Output,
                    Headers         = serializer.InternalSerializer.ConvertToBytes(m.Message.Headers),
                    MessageId       = messageIdFactory.Create().Create().ToString(),
                    MetaData        = meta,
                    CorrelationId   = m.MessageData.CorrelationId.ToString(),
                    TimeStamp       = unixTimeStampDelay,
                    ExpireTimeStamp = unixTimeStampExpiration,
                    Route           = m.MessageData.Route
                });
            });
            return(messagesToSend.ToList());
        }
        /// <summary>
        /// Creates the messages to send.
        /// </summary>
        /// <param name="redisHeaders">The redis headers.</param>
        /// <param name="messages">The messages.</param>
        /// <param name="meta">The meta data, already serialized.</param>
        /// <param name="unixTimeFactory">The unix time factory.</param>
        /// <param name="messageIdFactory">The message identifier factory.</param>
        /// <param name="serializer">The composite serializer.</param>
        /// <returns></returns>
        public static List<EnqueueBatchLua.MessageToSend> CreateMessagesToSend(RedisHeaders redisHeaders, 
            IReadOnlyCollection<QueueMessage<IMessage, IAdditionalMessageData>> messages,
            byte[] meta, 
            IUnixTimeFactory unixTimeFactory,
            IGetMessageIdFactory messageIdFactory,
            ICompositeSerialization serializer)
        {
            var messagesToSend = new ConcurrentBag<EnqueueBatchLua.MessageToSend>();
            Parallel.ForEach(messages, m =>
            {
                //the correlation ID must be saved as a message header
                m.Message.SetHeader(redisHeaders.CorelationId, new RedisQueueCorrelationIdSerialized((Guid)m.MessageData.CorrelationId.Id.Value));

                //check for delay and expiration
                long unixTimeStampDelay = 0;
                long unixTimeStampExpiration = 0;
                if (m.MessageData.GetDelay().HasValue)
                {
                    // ReSharper disable once PossibleInvalidOperationException
                    unixTimeStampDelay = unixTimeFactory.Create().GetAddDifferenceMilliseconds(m.MessageData.GetDelay().Value);
                }
                // ReSharper disable once PossibleInvalidOperationException
                if (m.MessageData.GetExpiration().HasValue && m.MessageData.GetExpiration().Value != TimeSpan.Zero)
                {
                    var unixTime = unixTimeFactory.Create();
                    var timeSpan = m.MessageData.GetExpiration();
                    if (timeSpan != null)
                    {
                        var timespan = timeSpan.Value;
                        unixTimeStampExpiration = unixTime.GetAddDifferenceMilliseconds(timespan);
                    }
                }

                var serialized = serializer.Serializer.MessageToBytes(new MessageBody { Body = m.Message.Body });
                m.Message.SetHeader(redisHeaders.Headers.StandardHeaders.MessageInterceptorGraph, serialized.Graph);

                messagesToSend.Add(new EnqueueBatchLua.MessageToSend()
                {
                    Message = serialized.Output,
                    Headers = serializer.InternalSerializer.ConvertToBytes(m.Message.Headers),
                    MessageId = messageIdFactory.Create().Create().ToString(),
                    MetaData = meta,
                    CorrelationId = m.MessageData.CorrelationId.ToString(),
                    TimeStamp = unixTimeStampDelay,
                    ExpireTimeStamp = unixTimeStampExpiration
                });
            });
            return messagesToSend.ToList();
        }