Service bus

A technique to offload notifications from the application into a separate system.

A service bus is a method used to offload notifications from an application into a separate system. This system is designed to keep track of notifications and deliver them to all subscribing applications.

Notification Types:

  • Single Queue: Multiple applications subscribe to the same queue, and all subscribers process the queue together.
  • Topic: Each application subscribes to a separate queue. Notifications are sent to all subscribers individually, and each subscriber processes its own queue.

When creating a new service bus, you need to define whether the queue should be a single queue or a topic and provide a listener method to process the notifications. If the OnMessage option is unassigned, the application will not process the messages added to the queue. This option should not be used for topics where the purpose is to execute on all instances in a multi-server environment.

The following example demonstrates how to use the application's capabilities to ensure that the service bus queue processes messages only on the worker instance in Litium Serverless Cloud.

public class ServiceBusExample
{
    private readonly Litium.ServiceBus.ServiceBusQueue<MyMessage> _queue;
    private readonly Litium.ServiceBus.ServiceBusTopic<MyMessage> _topic;

    public ServiceBusExample(
        Litium.ServiceBus.ServiceBusFactory serviceBusFactory,
        Litium.Runtime.ApplicationCapabilityService applicationCapabilityService)
    {
        var executingOnWorker = applicationCapabilityService.HasCapability(Litium.Runtime.SystemCapabilities.Worker);
        _queue = serviceBusFactory.CreateQueue(new Litium.ServiceBus.ServiceBusOptions<MyMessage>("My queue name", executingOnWorker ? ProcessQueueMessage : default));
        _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(Litium.ServiceBus.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(Litium.ServiceBus.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

To extend with your own implementation you should implement Litium.ServiceBus.ServiceBusFactory and dependency injection will automatically find and use that implementation.

Please read more about multi-server installation here.

Was this page helpful?
Thank you for your feedback!