Skip to content

Commit 1bb86ac

Browse files
committed
New mode that allows you to disable all external message listeners. Closes GH-2007
1 parent 8a39df9 commit 1bb86ac

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

docs/guide/messaging/listeners.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,15 @@ This option does a couple things:
162162
* Sets any local execution of the listener's internal, local queue to be strictly sequential and only process messages with
163163
a single thread
164164

165+
## Disabling All External Listeners
165166

167+
In some cases, you may want to disable all message processing for messages received from external
168+
transports like Rabbit MQ or AWS SQS. To do that, simply set:
169+
170+
snippet: sample_disable_all_listeners
171+
172+
The original use case for this flag was a command line tool that needed to publish messages to
173+
a system through Rabbit MQ then exit. Having that process also trying to publish messages received
174+
from Rabbit MQ kept the command line tool from quitting quickly as Wolverine had to "drain" ongoing
175+
work. For that kind of tool, we recommend this setting.
166176

src/Testing/CoreTests/WolverineOptionsTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public void publish_agent_events_should_be_false_by_default()
2727
new WolverineOptions().Policies.PublishAgentEvents.ShouldBeFalse();
2828
}
2929

30+
[Fact]
31+
public void do_not_disable_external_listeners_by_default()
32+
{
33+
new WolverineOptions().DisableAllExternalListeners.ShouldBeFalse();
34+
}
35+
3036
[Fact]
3137
public void default_dead_letter_queue_behavior_is_discard()
3238
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Shouldly;
3+
using Wolverine.ComplianceTests;
4+
using Wolverine.Tracking;
5+
using Xunit;
6+
7+
namespace Wolverine.RabbitMQ.Tests;
8+
9+
public class disable_external_listeners
10+
{
11+
[Fact]
12+
public async Task listeners_are_not_active()
13+
{
14+
using var host = await Host.CreateDefaultBuilder()
15+
16+
#region sample_disable_all_listeners
17+
18+
.UseWolverine(opts =>
19+
{
20+
// This will disable all message listening to
21+
// external message brokers
22+
opts.DisableAllExternalListeners = true;
23+
24+
opts.DisableConventionalDiscovery();
25+
26+
// This could never, ever work
27+
opts.UseRabbitMq().AutoProvision();
28+
opts.ListenToRabbitQueue("incoming");
29+
}).StartAsync();
30+
31+
#endregion
32+
33+
var activeListeners = host.GetRuntime().Endpoints.ActiveListeners().ToArray();
34+
activeListeners
35+
.Any().ShouldBeFalse();
36+
}
37+
}

src/Wolverine/Configuration/EndpointCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ public IReadOnlyList<Endpoint> LeaderPinnedListeners()
237237

238238
public async Task StartListenersAsync()
239239
{
240+
if (_options.DisableAllExternalListeners) return;
241+
240242
var listeningEndpoints = _options.Transports.SelectMany(x => x.Endpoints())
241243
.Where(x => x is not LocalQueue)
242244
.Where(x => x.ShouldAutoStartAsListener(_options.Durability));

src/Wolverine/WolverineOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ public AutoCreate AutoBuildMessageStorageOnStartup
294294
/// to latch all outgoing message sending
295295
/// </summary>
296296
internal bool ExternalTransportsAreStubbed { get; set; }
297+
298+
/// <summary>
299+
/// Should all listeners for external transports be disabled in
300+
/// this process? You may want to use this for command line applications
301+
/// that publish outbound messages
302+
/// </summary>
303+
public bool DisableAllExternalListeners { get; set; }
297304

298305
[IgnoreDescription]
299306
internal LocalTransport LocalRouting => Transports.GetOrCreate<LocalTransport>();

0 commit comments

Comments
 (0)