A service bus is a technique to offload notifications from the application into a separate system that is designed to keep track of the notifications and deliver them to all subscribing applications.
Notifications
A notification can be one of the following types:
- Single queue - Multiple applications subscribe to the same queue. All subscribers will process the queue together.
- Topic - Each application subscribes to a separate queue and the notification will be delivered to all application queues. All subscribers will individually process its queue.
When you create a new service bus you need to define if the queue should be a single queue or topic, and provide a listener method that can process the notifications.
public class ServiceBusExample
{
private readonly Litium.ServiceBus.ServiceBusQueue<MyMessage> _queue;
private readonly Litium.ServiceBus.ServiceBusTopic<MyMessage> _topic;
public ServiceBusExample(Litium.ServiceBus.ServiceBusFactory serviceBusFactory)
{
_queue = serviceBusFactory.CreateQueue(new Litium.ServiceBus.ServiceBusOptions<MyMessage>("My queue name", ProcessQueueMessage));
_topic = serviceBusFactory.CreateTopic(new Litium.ServiceBus.TopicServiceBusOptions<MyMessage>("My topic name", ProcessTopicMessage));
}
public void SendToQueue(MyMessage message)
{
_queue.Send(message);
}
public void SendToTopic(MyMessage message)
{
_topic.Send(message);
}
private void ProcessQueueMessage(ServiceBusMessage<MyMessage> myMessage)
{
// Process queue message. This is distributed to all applications that are started and attached to the queue with the name "My queue name"
}
private void ProcessTopicMessage(ServiceBusMessage<MyMessage> myMessage)
{
// Process topic message. This is processed by all applications that are started and attached to the topic with the name "My topic name"
}
public class MyMessage
{
public string MySpecialValue { get; set; }
}
}
Service bus implementations
A service bus can have different implementations and can be replaced in the dependency injection container. The default implementation uses Azure Service Bus that can be installed on premises or used from the cloud.
To extend with your own implementation you should implement Litium.ServiceBus.ServiceBusFactory and dependency injection will automatically find and use that implementation.
Read more about multi-server installation here.
|