A distributed contracts-based sender/handler messaging system built on RabbitMQ and BearBones-Messaging
Windows
- Install Erlang and RabbitMQ server
Linux
- As Windows, or use your distro's package manager to get RabbitMQ (you should get Erlang automatically)
- (example, for Ubuntu:
sudo apt-get install rabbitmq-server
)
- (example, for Ubuntu:
To check your installation is working, try the SimpleSample
console app included
- Add NuGet reference from SevenDigital.Messaging
Configure messaging:
MessagingSystem.Configure.WithDefaults();
Define a message:
// Message contract (defined in both sender and receiver)
public interface IExampleMessage : IMessage {
string Hello {get;set;}
}
// A specific instance (only needs to be defined in sender)
class MyExample : IExampleMessage {
public string Hello {get;set;}
}
Setup a message handler:
public class MyHandler : IHandle<IExampleMessage> {
public void Handle(IExampleMessage msg) {
Console.WriteLine("Hello, "+msg.Hello);
}
}
Register handler with listener:
// Spawns sets of background threads to handle incoming messages
using (MessagingSystem.Receiver().Listen(_=>_.Handle<IExampleMessage>().With<MyHandler>())) {
while (true) {Thread.Sleep(1000);}
}
MessagingSystem.Control.Shutdown();
Register for load balanced: (if the named queue doesn't exist, it will be created automatically)
using (MessagingSystem.Receiver()
.TakeFrom("MessageQueueName", _=>_
.Handle<IExampleMessage>().With<MyHandler>())) {
.
.
.
Send some messages:
MessagingSystem.Sender().SendMessage(new MyExample{Hello = "World"});
- Messaging.Receiver() to get a new
IReceiver
instance (this uses StructureMap under the hood) - Creating listener nodes takes time and resources. Do it infrequently -- usually once at the start of your app.
- Your handler will get
new()
'd for every message. Don't do anything complex in the handler constructor! - To listen to messages,
factory.Listener(binder=>binder.Handle<IMyMessageInterface>().With<MyHandlerType>()
- Each listener can handle any number of message => handler pairs, and a message can have more than one handler (they all fire in parallel)
See further simple examples in the Integration Tests.