예제 #1
0
        /// <summary>
        /// Starts the queue.
        /// </summary>
        /// <remarks>
        /// This must be called after setting any configuration options, and before sending any messages.
        /// </remarks>
        public void Start()
        {
            ThrowIfDisposed();
            if (Started)
            {
                throw new DotNetWorkQueueException("Start must only be called 1 time");
            }

            _clearQueue.Start();
            _configurationRpc.SetReadOnly();
            _configurationReceive.SetReadOnly();
            _queueWait = _queueWaitFactory.CreateQueueDelay();
            Started    = true;
        }
예제 #2
0
        /// <summary>
        /// Handles the specified message.
        /// </summary>
        /// <param name="messageId">The message identifier.</param>
        /// <param name="timeOut">The time out.</param>
        /// <param name="queueWait">The queue wait.</param>
        /// <returns></returns>
        /// <exception cref="System.TimeoutException"></exception>
        public IReceivedMessage <TReceivedMessage> Handle(IMessageId messageId, TimeSpan timeOut, IQueueWait queueWait)
        {
            Guard.NotNull(() => messageId, messageId);
            Guard.NotNull(() => queueWait, queueWait);

            //use a message context, and talk to the transport directly
            //we are not going to use the consumer queue, because we are going to re-use the calling thread for all of the work below
            using (var context = _messageContextFactory.Create())
            {
                var recMessage = _receiveMessagesFactory.Create();

                //set message Id on the context, so that the transport knows we want a particular message
                context.Set(_configurationReceive.HeaderNames.StandardHeaders.RpcContext, _rpcContextFactory.Create(messageId, timeOut));

                //use a stop watch to determine when we have timed out
                var sw = new Stopwatch();
                sw.Start();
                while (true)
                {
                    var messageRec = recMessage.ReceiveMessage(context);
                    if (messageRec != null)
                    {
                        _commitMessage.Commit(context);
                        return((IReceivedMessage <TReceivedMessage>)_messageHandler.GenerateMessage(messageRec));
                    }
                    if (sw.ElapsedMilliseconds >= timeOut.TotalMilliseconds)
                    {
                        throw new TimeoutException();
                    }
                    queueWait.Wait();
                }
            }
        }