Utility class for Tracking Memory Usage with an imposed limit on the amount available. Provides methods for objects to wait on more space to become available if the memory limit is reached.
Example #1
0
        public void TestUsage()
        {
            MemoryUsage usage1 = new MemoryUsage( 2048 );

            Assert.That( !usage1.IsFull() );
            Assert.That( usage1.Usage == 0 );

            usage1.IncreaseUsage( 1024 );

            Assert.That( !usage1.IsFull() );
            Assert.That( usage1.Usage == 1024 );

            usage1.DecreaseUsage( 512 );

            Assert.That( !usage1.IsFull() );
            Assert.That( usage1.Usage == 512 );

            usage1.Usage = 2048;

            Assert.That( usage1.IsFull() );
            Assert.That( usage1.Usage == 2048 );

            usage1.IncreaseUsage( 1024 );
            Assert.That( usage1.IsFull() );
            Assert.That( usage1.Usage == 3072 );
        }
Example #2
0
        public MessageProducer(Session session, ProducerId id, ActiveMQDestination destination, TimeSpan requestTimeout)
        {
            this.session = session;
            this.RequestTimeout = requestTimeout;

            this.info = new ProducerInfo();
            this.info.ProducerId = id;
            this.info.Destination = destination;
            this.info.WindowSize = session.Connection.ProducerWindowSize;

            this.messageTransformation = session.Connection.MessageTransformation;

            // If the destination contained a URI query, then use it to set public
            // properties on the ProducerInfo
            if(destination != null && destination.Options != null)
            {
                URISupport.SetProperties(this.info, destination.Options, "producer.");
            }

            // Version Three and higher will send us a ProducerAck, but only if we
            // have a set producer window size.
            if(session.Connection.ProtocolVersion >= 3 && this.info.WindowSize > 0)
            {
                Tracer.Debug("MessageProducer created with a Window Size of: " + this.info.WindowSize);
                this.usage = new MemoryUsage(this.info.WindowSize);
            }
        }
Example #3
0
        public void TestConstructors()
        {
            MemoryUsage usage = new MemoryUsage();

            Assert.That(usage.Limit == 0);
            Assert.That(usage.Usage == 0);

            usage = new MemoryUsage(1024);

            Assert.That(usage.Limit == 1024);
            Assert.That(usage.Usage == 0);
        }
Example #4
0
        public void TestTimedWait()
        {
            MemoryUsage usage = new MemoryUsage( 2048 );
            usage.IncreaseUsage( 5072 );

            DateTime start = DateTime.Now;

            usage.WaitForSpace( TimeSpan.FromMilliseconds(150) );

            DateTime end = DateTime.Now;

            TimeSpan timePassed = end - start;

            Assert.That( timePassed.TotalMilliseconds >= 125 );
        }
Example #5
0
        internal void DoSend(ActiveMQDestination destination, ActiveMQMessage message,
                             MessageProducer producer, MemoryUsage producerWindow, TimeSpan sendTimeout)
        {
            ActiveMQMessage msg = message;

            if(destination.IsTemporary && !connection.IsTempDestinationActive(destination as ActiveMQTempDestination))
            {
                throw new InvalidDestinationException("Cannot publish to a deleted Destination: " + destination);
            }

            if(IsTransacted)
            {
                DoStartTransaction();
                msg.TransactionId = TransactionContext.TransactionId;
            }

            msg.RedeliveryCounter = 0;
            msg.BrokerPath = null;

            if(this.connection.CopyMessageOnSend)
            {
                msg = (ActiveMQMessage)msg.Clone();
            }

            msg.OnSend();
            msg.ProducerId = msg.MessageId.ProducerId;

            if(sendTimeout.TotalMilliseconds <= 0 && !msg.ResponseRequired && !connection.AlwaysSyncSend &&
               (!msg.Persistent || connection.AsyncSend || msg.TransactionId != null))
            {
                this.connection.Oneway(msg);

                if(producerWindow != null)
                {
                    // Since we defer lots of the marshaling till we hit the wire, this
                    // might not provide and accurate size. We may change over to doing
                    // more aggressive marshaling, to get more accurate sizes.. this is more
                    // important once users start using producer window flow control.
                    producerWindow.IncreaseUsage(msg.Size());
                }
            }
            else
            {
                if(sendTimeout.TotalMilliseconds > 0)
                {
                    this.connection.SyncRequest(msg, sendTimeout);
                }
                else
                {
                    this.connection.SyncRequest(msg);
                }
            }
        }
Example #6
0
        public void TestWait()
        {
            MemoryUsage usage = new MemoryUsage( 2048 );
            usage.IncreaseUsage( 5072 );

            Thread thread1 = new Thread(delegate ()
            {
                Thread.Sleep( 100 );
                usage.DecreaseUsage( usage.Usage );
            });

            thread1.Start();

            usage.WaitForSpace();
            Assert.That( usage.Usage == 0 );

            thread1.Join();
        }