예제 #1
0
        private void StartAsyncMessageDemo(int count, int interval)
        {
            _messageIndex = 0;
            _processing   = true;
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    Task.Delay(TimeSpan.FromMilliseconds(interval)).Wait();
                    if (!_processing)
                    {
                        break;
                    }
                    _messageIndex++;

                    AsyncMessageHelper.SafeUpdateUi("message " + _messageIndex);
                }
                _processing = false;
            });
        }
예제 #2
0
        public IMessage SendRequestResponse(IMessage message, int timeoutInMilliseconds = 15000)
        {
            var now = DateTime.Now;
            if (!this.isInitialized)
            {
                this.asr.WaitOne(timeoutInMilliseconds);
                if (!this.isInitialized)
                    throw new TimeoutException("Could not send the message because no connections were available within the specified timeout period.");
            }

            this.InitializeForSynchronous();

            // Create a unique correlation ID which we will use to map response messages to request messages.
            var correlationID = Guid.NewGuid().ToString();
            message.NMSCorrelationID = correlationID;

            // Set the reply-to header to the temporary queue that we created so that the server knows where to return messages.
            message.NMSReplyTo = this.temporaryQueue;

            // Create a new AsyncMessageHelper.  This class is a helper class to make it easier for us to map response messages to request messages.
            using (var asyncMessageHelper = new AsyncMessageHelper())
            {
                // Add the async helper to the response buffer.
                lock (this.responseBuffer)
                    this.responseBuffer[correlationID] = asyncMessageHelper;

                // Send the message to the queue.
                this.producer.Send(message);

                // Calculate the remaining timeout time since we may have had to wait.
                int remainingTime = (int)((DateTime.Now - now).TotalMilliseconds);
                remainingTime = timeoutInMilliseconds - remainingTime;
                remainingTime = (remainingTime < 0) ? 0 : remainingTime;

                // Wait for a response for up to [timeout] seconds.  This blocks until the timeout expires or a message is received (.Set() is called on the trigger then, allowing execution to continue).
                asyncMessageHelper.Trigger.WaitOne(remainingTime, true);

                // Either the timeout has expired, or a message was received with the same correlation ID as the request message.
                IMessage responseMessage;
                try
                {
                    // The Message property on the async helper will not have been set if no message was received within the timeout period.
                    if (asyncMessageHelper.Message == null)
                        throw new TimeoutException("Timed out while waiting for a response.");

                    // We got the response message, cool!
                    responseMessage = asyncMessageHelper.Message;
                }
                finally
                {
                    // Remove the async helper from the response buffer.
                    lock (this.responseBuffer)
                        this.responseBuffer.Remove(correlationID);
                }

                // Return the response message.
                return responseMessage;
            }
        }