MessageChannel

Message channel is responsible for the message delivery. Subscribers identify message channel by its name. The name is set in the message channel constructor.

Components broadcast messages using the Post() method. Posted messages are internally queued by the message channel. As default the message channel uses the built-in .NET Queue<> for queueing messages. It's simple and quick but the messages are not persisted in case the system collapses. You can inject your own IQueue (eg. a MSMQ wrapper) in the constructor of the message channel.

    public class MessageChannel : IDisposable
    {
        public MessageChannel(string name) {...}
        public MessageChannel(string name, IQueue queue) {...}

        public string Name { get;}

        public void Post(object message) {...}
        public event MessageEventHandler MessageArrived;
    }
The Post() method is asynchronous. A parameter message is queued and the Post() method ends immediately. Each message channel has its own working thread for dequeuing messages. All messages are dequeued one after another. The working thread checks if there are new messages arrived. Are there new messages in the queue, the oldest one is dequeued (FIFO) and the event MessageArrived is invoked. The dequeuing is repeated until there are no more queued messages. Then the working thread goes to sleep until further messages are posted to the channel.

MessageChannel is thread safe. You can attach and detach new subscribers during the application work. Messages are broadcasted in the order they were posted. However the order in which subscribers are notified is not predictable.

The MessageArrived event should not be blocked by long lasting operations and it should not throw exceptions.

Last edited Jan 13, 2013 at 8:45 AM by polo, version 2

Comments

No comments yet.