Skip to content

Commit 4dfdd53

Browse files
committed
Refactor LoadSimulator to use IMessageSession and update Program.cs for host-based endpoint initialization
1 parent 0cf7d02 commit 4dfdd53

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

samples/logging/new-relic/Metrics_6/Endpoint/LoadSimulator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using NServiceBus;
55

66
// Simulates busy (almost no delay) / quiet time in a sine wave
7-
class LoadSimulator(IEndpointInstance endpointInstance, TimeSpan minimumDelay, TimeSpan idleDuration)
7+
class LoadSimulator(IMessageSession messageSession, TimeSpan minimumDelay, TimeSpan idleDuration)
88
{
9-
CancellationTokenSource tokenSource = new CancellationTokenSource();
9+
CancellationTokenSource tokenSource = new();
1010
TimeSpan idleDuration = TimeSpan.FromTicks(idleDuration.Ticks / 2);
1111
Task fork;
1212

@@ -42,7 +42,7 @@ TimeSpan NextDelay()
4242
return delay;
4343
}
4444

45-
Task Work() => endpointInstance.SendLocal(new SomeCommand());
45+
Task Work() => messageSession.SendLocal(new SomeCommand());
4646

4747
public Task Stop()
4848
{
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
using System;
2+
using Microsoft.Extensions.Hosting;
23
using Endpoint;
4+
using Microsoft.Extensions.DependencyInjection;
35
using NServiceBus;
46

5-
var endpointConfiguration = new EndpointConfiguration(Console.Title = "TracingEndpoint");
7+
Console.Title = "TracingEndpoint";
68

7-
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
8-
endpointConfiguration.UseTransport<LearningTransport>();
9+
var host = Host.CreateDefaultBuilder(args)
10+
.UseConsoleLifetime()
11+
.UseNServiceBus(_ =>
12+
{
13+
var endpointConfiguration = new EndpointConfiguration("TracingEndpoint");
14+
15+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
16+
endpointConfiguration.UseTransport<LearningTransport>();
17+
18+
NewRelicMetrics.Setup(endpointConfiguration);
19+
20+
return endpointConfiguration;
21+
})
22+
.Build();
923

10-
NewRelicMetrics.Setup(endpointConfiguration);
24+
await host.StartAsync();
1125

12-
var endpointInstance = await NServiceBus.Endpoint.Start(endpointConfiguration);
26+
var endpointInstance = host.Services.GetRequiredService<IMessageSession>();
1327

1428
#region newrelic-load-simulator
1529

@@ -22,13 +36,13 @@
2236
{
2337
Console.WriteLine("Endpoint started.");
2438
Console.WriteLine("Press [ENTER] to send additional messages.");
25-
Console.WriteLine("Press [ESC] to quit.");
39+
Console.WriteLine("Press [Q] to quit.");
2640

2741
while (true)
2842
{
2943
switch (Console.ReadKey(true).Key)
3044
{
31-
case ConsoleKey.Escape:
45+
case ConsoleKey.Q:
3246
return;
3347
case ConsoleKey.Enter:
3448
await endpointInstance.SendLocal(new SomeCommand());
@@ -39,5 +53,5 @@
3953
finally
4054
{
4155
await simulator.Stop();
42-
await endpointInstance.Stop();
56+
await host.StopAsync();
4357
}

0 commit comments

Comments
 (0)