Exemplo n.º 1
0
        public int PublishBatch(IDictionary <string, object> extraHeaders, IEnumerable <SimpleMessage> messages,
                                AndThenDeliveryMode mode)
        {
            var batch = _channel.CreateBasicPublishBatch();
            int messageSentCounter = 0;

            foreach (var message in messages)
            {
                var basicProperties = ToBasicProperties(_channel.CreateBasicProperties(), message);
                ApplyHeaders(extraHeaders, basicProperties);
                batch.Add(message.Exchange, message.RoutingKey ?? "", true, basicProperties, message.Body);
                messageSentCounter++;
            }
            batch.Publish();
            return(messageSentCounter);
        }
Exemplo n.º 2
0
        public void PublishBatch(IReadOnlyList <SimpleMessage> messages, SimpleMessage andThen, AndThenDeliveryMode mode)
        {
            var groupId = Guid.NewGuid();
            var headers = new Dictionary <string, object>
            {
                { "MessageGroupId", groupId.ToByteArray() },
                { "AndThenMode", mode.ToString() }
            };

            //initialize the Redis List and Hash
            if (mode != AndThenDeliveryMode.None)
            {
                _messageGroupHandler.StartMessageGroup(groupId);
            }

            var messageIds = InitializeMessageIds(messages);

            //store message ids in the redis Redis List then
            //set the MsgCount, Handled, DateStamp and the AndThen message
            if (mode != AndThenDeliveryMode.None)
            {
                _messageGroupHandler.MessagesPrepared(groupId, messageIds);
                _messageGroupHandler.SetMessageGroupCount(groupId, messageIds.Count, andThen);
            }

            _messagePublisher.PublishBatch(headers, messages, mode);

            //finalize the redis structure by setting the Transmitted date stamp
            if (mode != AndThenDeliveryMode.None)
            {
                _messageGroupHandler.CompleteMessageGroupTransmission(groupId);
            }

            if (andThen?.Body == null && mode == AndThenDeliveryMode.FromLastClient)
            {
                throw new Exception($"The AndThen body must be specified in the {AndThenDeliveryMode.FromLastClient} mode.");
            }
            if (andThen == null)
            {
                andThen = new SimpleMessage()
                {
                    Exchange   = "AndThen",
                    RoutingKey = messages.First().RoutingKey,
                    Body       = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(messageIds)
                };
            }
            else if (andThen?.Body == null)
            {
                andThen.Body = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(messageIds);
            }

            headers["MessagesSent"] = messageIds.Count;
            switch (mode)
            {
            case AndThenDeliveryMode.FromLastClient:
                //client will send the andThen message that it read from IMessageGroupHandler (redis).
                break;

            case AndThenDeliveryMode.FromEventService:
                //client will send an event to the eventService after it handles each message.
                _messagePublisher.Publish(headers, andThen);
                break;

            case AndThenDeliveryMode.FromPollingService:
                //send the andThen message to the service that will now poll redis
                _messagePublisher.Publish(headers, andThen);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
            }
        }
Exemplo n.º 3
0
 public void PublishSingle(SimpleMessage message, SimpleMessage andThen, AndThenDeliveryMode mode)
 {
     PublishBatch(new[] { message }, andThen, mode);
 }