Broadcast Messages to a Specific Topic
If you’re using a transport endpoint that supports publishing messages by topic such as this example using Rabbit MQ from the Wolverine tests:
theSender = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.UseRabbitMq("host=localhost;port=5672").AutoProvision();
opts.PublishAllMessages().ToRabbitTopics("wolverine.topics", exchange =>
{
exchange.BindTopic("color.green").ToQueue("green");
exchange.BindTopic("color.blue").ToQueue("blue");
exchange.BindTopic("color.*").ToQueue("all");
// Need this to be able to go to ONLY the green receiver for a test
exchange.BindTopic("special").ToQueue("green");
});
opts.Discovery.DisableConventionalDiscovery()
.IncludeType<TriggerTopicMessageHandler>();
opts.ServiceName = "TheSender";
opts.PublishMessagesToRabbitMqExchange<RoutedMessage>("wolverine.topics", m => m.TopicName);
}).StartAsync();snippet source | anchor
You can explicitly publish a message to a topic through this syntax:
var publisher = theSender.Services
.GetRequiredService<IMessageBus>();
await publisher.BroadcastToTopicAsync("color.purple", new Message1());snippet source | anchor
var publisher = theSender.Services
.GetRequiredService<IMessageBus>();
await publisher.BroadcastToTopicAsync("color.purple", new Message1());snippet source | anchor
::: warning If you wish to use this functionality, you have to configure at least one sending endpoint subscription like a Rabbit MQ topic exchange in your application. Wolverine has to know how to send messages with your topic. :::
Topic Sending as Cascading Message
Wolverine is pretty serious about enabling as many message handlers or HTTP endpoints as possible to be pure functions where the unit testing is easier, so there’s an option to broadcast messages to a particular topic as a cascaded message:
public class ManuallyRoutedTopicResponseHandler
{
public IEnumerable<object> Consume(MyMessage message, Envelope envelope)
{
// Go North now at the "direction" queue
yield return new GoNorth().ToTopic($"direction/{envelope.TenantId}");
}
}snippet source | anchor