MessageBus instance handles publishers and subscribers registering and unregistering, hosts sequences for each message name and executes message when publisher requests.
To use MessageBus, an instance of MessageBus need to be created for registering publishers and subscribers, hosting sequences for each message name and executing message when publisher requests.
using var bus = new MessageBus();
Using bus As New MessageBus
'Code
End Using
MessageBus implements IDisposable. When disposing, all publishers and subscribers are unregistered. No unmanaged resource is used.
By default, AutoShrink is on. When a publisher is unregistered, MessageBus checks to see if there are still publishers associated with the same message name. If not, related resources will be released. When the publishers are frequently unregistered and registered, the related checking and recycling work may affect the efficiency. In this case, turn off AutoShrink and call ShrinkSequencers manually when required.
Uses MessageBus to execute subscriber with parameter when publisher requests.
In code below, one publisher and one subscriber is registered with the name Hello
. The method of subscriber is called when publisher requests.
Subscribers have a parameter to receive data from Publisher as argument.
Here is the full code.
public void TestMethod()
{
using var bus = new MessageBus();
var subscriberTicket = bus.RegisterSubscriber<string>("Hello", SubscriberMethod);
var text = "Hello World";
var publisherTicket = bus.RegisterPublisher<string>("Hello");
publisherTicket.Executor.Execute(text);
bus.UnregisterPublisher(publisherTicket);
bus.UnregisterSubscriber(subscriberTicket);
}
private string? _received;
public void SubscriberMethod(string? argument)
{
_received = argument;
}
Sub TestMethod()
Using bus As New MessageBus
Dim subscriberTicket = bus.RegisterSubscriber(Of String)("Hello", AddressOf SubscriberMethod)
Dim text = "Hello World"
Dim publisherTicket = bus.RegisterPublisher(Of String)("Hello")
publisherTicket.Executor.Execute(text)
bus.UnregisterPublisher(publisherTicket)
bus.UnregisterSubscriber(subscriberTicket)
End Using
End Sub
Private _received As String
Public Sub SubscriberMethod(argument As String)
_received = argument
End Sub
To register a subscriber, uses the code below. Check Subscriber for details.
var subscriberTicket = bus.RegisterSubscriber<string>("Hello", SubscriberMethod);
Dim subscriberTicket = bus.RegisterSubscriber(Of String)("Hello", AddressOf SubscriberMethod)
To register a publisher, uses the code below. Check Publisher for details.
var publisherTicket = bus.RegisterPublisher<string>("Hello");
publisherTicket.Executor.Execute(text);
Dim publisherTicket = bus.RegisterPublisher(Of String)("Hello")
publisherTicket.Executor.Execute(text)
When Execute calling, SubscriberMethod is executed.
Unlike many publisher subscribe model, MessageBus supports return data from subscriber to publisher.
Here is the full code.
public void TestMethod()
{
using var bus = new MessageBus();
var subscriberTicket = bus.RegisterSubscriber<string, int>("Hello", SubscriberMethod);
var text = "Hello World";
var publisherTicket = bus.RegisterPublisher<string, int>("Hello");
var returnValue = publisherTicket.Executor.Execute(text);
//after being executed: returnValue == 100
bus.UnregisterPublisher(publisherTicket);
bus.UnregisterSubscriber(subscriberTicket);
}
private string? _received;
public int SubscriberMethod(string? argument)
{
_received = argument;
return 100;
}
Sub TestMethod()
Using bus As New MessageBus
Dim subscriberTicket = bus.RegisterSubscriber(Of String, Integer)("Hello", AddressOf SubscriberMethod)
Dim text = "Hello World"
Dim publisherTicket = bus.RegisterPublisher(Of String, Integer)("Hello")
Dim returnValue = publisherTicket.Executor.Execute(text)
'after being executed: returnValue = 100
bus.UnregisterPublisher(publisherTicket)
bus.UnregisterSubscriber(subscriberTicket)
End Using
End Sub
Private _received As String
Public Function SubscriberMethod(argument As String) As Integer
_received = argument
Return 100
End Function
To register a subscriber, uses the code below. Check Subscriber for details.
var subscriberTicket = bus.RegisterSubscriber<string, int>("Hello", SubscriberMethod);
Dim subscriberTicket = bus.RegisterSubscriber(Of String, Integer)("Hello", AddressOf SubscriberMethod)
To register a publisher, uses the code below. Check Publisher for details.
var publisherTicket = bus.RegisterPublisher<string, int>("Hello");
var returnValue = publisherTicket.Executor.Execute(text);
Dim publisherTicket = bus.RegisterPublisher(Of String, Integer)("Hello")
Dim returnValue = publisherTicket.Executor.Execute(text)
When Execute calling, SubscriberMethod is executed and the return value is sent back to the caller.