Ejemplo n.º 1
0
        public MessageBatch AddToBatch(string exch, string routKey, Message message)
        {
            if (_exchange != null && _exchange != exch)
            {
                throw new ArgumentException("Cannot send to different exchanges in the same batch");
            }

            _exchange = exch;

            if (_routingKey != null && _routingKey != routKey)
            {
                throw new ArgumentException("Cannot send with different routing keys in the same batch");
            }

            _routingKey = routKey;

            var          bufferUse = 4 + message.Body.Length;
            MessageBatch batch     = null;

            if (_messages.Count > 0 && _currentSize + bufferUse > _bufferLimit)
            {
                batch       = DoReleaseBatch();
                _exchange   = exch;
                _routingKey = routKey;
            }

            _currentSize += bufferUse;
            _messages.Add(message);
            if (batch == null && (_messages.Count >= _batchSize || _currentSize >= _bufferLimit))
            {
                batch = DoReleaseBatch();
            }

            return(batch);
        }
Ejemplo n.º 2
0
        private MessageBatch?DoReleaseBatch()
        {
            if (_messages.Count < 1)
            {
                return(null);
            }

            var message      = AssembleMessage();
            var messageBatch = new MessageBatch(_exchange, _routingKey, message);

            _messages.Clear();
            _currentSize = 0;
            _exchange    = null;
            _routingKey  = null;
            return(messageBatch);
        }