Exemple #1
0
        /// <summary>
        /// Transmits a message.
        /// </summary>
        /// <param name="msg">The message to transmit.</param>
        public void TransmitMessage(MultiPartMessage msg)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!_isRunning)
            {
                throw new InvalidOperationException("Message Engine is not running.");
            }

            CheckMetadata(msg);

            AdapterBase adapter = AdapterProxy.GetAdapterById(msg.Metadata.ReadAsString("SendAdapterId"));

            ComponentPipeline pipeline = _sendPipelineDictionary[adapter];

            Collection <MultiPartMessage> resultCollection = pipeline.Execute(msg);

            if (resultCollection != null)
            {
                //Check if message should be part of a transaction
                if (TransactionScope.Current != null)
                {
                    if (adapter is ITransactional)
                    {
                        ITransactional transactionalAdapter = adapter as ITransactional;

                        AdapterTransaction transaction = transactionalAdapter.StartTransaction(resultCollection[0]);
                        transaction.State = TransactionState.Started;

                        TransactionScope.Current.EnlistTransaction(transaction);

                        transactionalAdapter.TransmitMessage(resultCollection[0], transaction);
                    }
                    else
                    {
                        throw new AdapterException("Adapter: \"" + adapter.AdapterId + "\" is not transactional.");
                    }
                }
                else
                {
                    adapter.TransmitMessage(resultCollection[0]);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Binds a pipeline to an adapter.
        /// </summary>
        /// <param name="adapter">The <see cref="AdapterBase"/> to bind to.</param>
        /// <param name="pipeline">The <see cref="ComponentPipeline"/> to bind.</param>
        /// <remarks>
        /// When a pipeline has ben bound, all messages sent or received by the adapter will be
        /// passed through the pipeline for processing.
        /// </remarks>
        public void Bind(AdapterBase adapter, ComponentPipeline pipeline)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Information) == SourceLevels.Information)
            {
                MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Information, 0, "Binding adapter: \"{0}\".", adapter.AdapterId);
            }

            if (adapter == null)
            {
                throw new ArgumentNullException("adapter");
            }
            else if (pipeline == null)
            {
                throw new ArgumentNullException("pipeline");
            }

            if (pipeline.PipelineType == PipelineType.Receive)
            {
                _receivePipelineDictionary[adapter] = pipeline;
            }
            else
            {
                _sendPipelineDictionary[adapter] = pipeline;
            }

            if (!_adapterCollection.Contains(adapter))
            {
                adapter.MessageReceived += new EventHandler <AdapterReceiveEventArgs>(MessageReceivedEventHandler);
                _adapterCollection.Add(adapter);
                _adapterProxy.RegisterAdapter(adapter);
            }
        }
Exemple #3
0
        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);
                    }
                }
            }
        }