private CorrelationContext CorrelateMessage(MultiPartMessage msg) { _correlationSyncLock.AcquireReaderLock(LockMillisecondsTimeout); try { if (msg.Metadata.Contains("CorrelationId")) { string correlationId = (string)msg.Metadata.Read("CorrelationId"); if (_correlationDictionary.ContainsKey(correlationId)) { CorrelationContext context = _correlationDictionary[correlationId]; context.ResponseMessages.Add(msg); return(context); } } } finally { _correlationSyncLock.ReleaseReaderLock(); } return(null); }
/// <summary> /// Transmits a request and blocks until a response has been received. /// </summary> /// <param name="msg">The request message to transmit.</param> /// <param name="correlationId">A string used to correlate the request with the response.</param> /// <param name="context"> /// The <see cref="CorrelationContext"/> containing one or more response messages. /// </param> public void TransmitRequestMessage(MultiPartMessage msg, string correlationId, out CorrelationContext context) { TransmitRequestMessage(msg, correlationId, Timeout.Infinite, out context); }
/// <summary> /// Transmits a request and blocks until a response has been received, /// or until the specified timeout has elapsed. /// </summary> /// <param name="msg">The request message to transmit.</param> /// <param name="correlationId">A string used to correlate the request with the response.</param> /// <param name="millisecondsTimeout">Timeout in milliseconds.</param> /// <param name="context"> /// The <see cref="CorrelationContext"/> containing one or more response messages. /// </param> public void TransmitRequestMessage(MultiPartMessage msg, string correlationId, int millisecondsTimeout, out CorrelationContext context) { if (_isDisposed) { throw new ObjectDisposedException(this.GetType().Name); } if (!_isRunning) { throw new InvalidOperationException("Message Engine is not running."); } if (correlationId == null) { throw new ArgumentNullException("correlationId"); } msg.Metadata.Write("CorrelationId", correlationId); context = new CorrelationContext(); _correlationSyncLock.AcquireWriterLock(LockMillisecondsTimeout); try { _correlationDictionary.Add(correlationId, context); } finally { _correlationSyncLock.ReleaseWriterLock(); } try { TransmitMessage(msg); if (!context.WaitForResponse(millisecondsTimeout)) { Uri sendUri = (Uri)msg.Metadata.Read("SendUri"); throw new RequestTimeoutException("Request timed out: \"" + sendUri + "\".", msg); } } finally { try { context.Dispose(); } finally { _correlationSyncLock.AcquireWriterLock(LockMillisecondsTimeout); try { _correlationDictionary.Remove(correlationId); } finally { _correlationSyncLock.ReleaseWriterLock(); } } } }
private void MessageReceivedEventHandler(object sender, AdapterReceiveEventArgs e) { if (!_isRunning) { return; } MultiPartMessage msg = e.Message; try { //Block incoming messages if startup in progress _blockThreadsWaitEvent.WaitOne(); //Create message metadata msg.Metadata.Write("ReceiveUri", e.ReceiveEndPoint.Uri); msg.Metadata.Write("ReceiveAdapterId", e.ReceiveEndPoint.Adapter.AdapterId); ComponentPipeline pipeline = _receivePipelineDictionary[e.ReceiveEndPoint.Adapter]; Collection <MultiPartMessage> resultCollection = pipeline.Execute(msg); if (resultCollection != null) { List <CorrelationContext> contextCollection = new List <CorrelationContext>(); foreach (MultiPartMessage resultMsg in resultCollection) { CorrelationContext context = CorrelateMessage(resultMsg); if (context == null) { SubscriptionManager.DistributeMessage(resultMsg); } else { contextCollection.Add(context); } } foreach (CorrelationContext context in contextCollection) { //Signal the requestor thread that a response has been received context.CompleteRequest(); } } } catch (Exception ex) { if (ExceptionHelper.IsCritical(ex)) { throw; } else if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Error) == SourceLevels.Error && _isRunning) { if (msg != null) { MessageEngine.Instance.Tracing.TraceData(TraceEventType.Error, msg.GetHashCode(), ex); } else { MessageEngine.Instance.Tracing.TraceData(TraceEventType.Error, 0, ex); } } } }