Skip to content

Commit dbcb5c3

Browse files
authored
Merge pull request #7581 from Particular/startup-shutdown-sequence-v10
Upgrade startup-shutdown-sequence to v10
2 parents 8ec014d + c1a922d commit dbcb5c3

File tree

11 files changed

+179
-0
lines changed

11 files changed

+179
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NServiceBus.Features;
2+
3+
public class MyFeature :
4+
Feature
5+
{
6+
protected override void Setup(FeatureConfigurationContext context)
7+
{
8+
Logger.WriteLine("Inside Feature.Setup");
9+
context.RegisterStartupTask(new MyFeatureStartupTask());
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
using NServiceBus.Features;
5+
6+
public class MyFeatureStartupTask :
7+
FeatureStartupTask
8+
{
9+
protected override Task OnStart(IMessageSession session, CancellationToken token)
10+
{
11+
Logger.WriteLine("Inside FeatureStartupTask.OnStart");
12+
return Task.CompletedTask;
13+
}
14+
15+
protected override Task OnStop(IMessageSession session, CancellationToken token)
16+
{
17+
Logger.WriteLine("Inside FeatureStartupTask.OnStop");
18+
return Task.CompletedTask;
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using NServiceBus;
2+
3+
public class NeedInitialization :
4+
INeedInitialization
5+
{
6+
public void Customize(EndpointConfiguration endpointConfiguration)
7+
{
8+
Logger.WriteLine("Inside INeedInitialization.Customize");
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using NServiceBus.Installation;
4+
5+
public class NeedToInstallSomething :
6+
INeedToInstallSomething
7+
{
8+
public Task Install(string identity, CancellationToken token)
9+
{
10+
Logger.WriteLine("Inside INeedToInstallSomething.Install");
11+
return Task.CompletedTask;
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NServiceBus;
2+
using NServiceBus.Settings;
3+
4+
public class WantToRunBeforeConfigurationIsFinalized :
5+
IWantToRunBeforeConfigurationIsFinalized
6+
{
7+
public void Run(SettingsHolder settings)
8+
{
9+
Logger.WriteLine("Inside WantToRunBeforeConfigurationIsFinalized.Run");
10+
}
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using NServiceBus.Logging;
5+
6+
#region Logger
7+
static class Logger
8+
{
9+
static ILog log = LogManager.GetLogger(typeof(Logger));
10+
public static string OutputFilePath;
11+
static object locker = new object();
12+
13+
static Logger()
14+
{
15+
OutputFilePath = Path.GetFullPath("StartupShutdownSequence.txt");
16+
AppDomain.CurrentDomain.ProcessExit += Exit;
17+
File.Delete(OutputFilePath);
18+
File.AppendAllText(OutputFilePath, "startcode");
19+
File.AppendAllText(OutputFilePath, " StartupShutdownSequence\r\n");
20+
}
21+
22+
static void Exit(object sender, EventArgs e)
23+
{
24+
File.AppendAllText(OutputFilePath, "endcode");
25+
}
26+
27+
public static void WriteLine(string message)
28+
{
29+
message = $"Thread:{Thread.CurrentThread.ManagedThreadId} {message}\r\n";
30+
log.Info(message);
31+
lock (locker)
32+
{
33+
File.AppendAllText(OutputFilePath, message);
34+
}
35+
}
36+
37+
}
38+
#endregion
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
using NServiceBus.Logging;
5+
6+
class Program
7+
{
8+
static async Task Main()
9+
{
10+
Console.Title = "StartupShutdown";
11+
LogManager.Use<DefaultFactory>().Level(LogLevel.Error);
12+
#region Program
13+
Logger.WriteLine("Starting configuration");
14+
var endpointConfiguration = new EndpointConfiguration("Samples.StartupShutdown");
15+
endpointConfiguration.EnableInstallers();
16+
endpointConfiguration.EnableFeature<MyFeature>();
17+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
18+
endpointConfiguration.UseTransport(new LearningTransport());
19+
20+
Logger.WriteLine("Calling Endpoint.Start");
21+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
22+
// simulate some activity
23+
await Task.Delay(500);
24+
25+
Logger.WriteLine("Endpoint is processing messages");
26+
Logger.WriteLine("Calling IEndpointInstance.Stop");
27+
await endpointInstance.Stop();
28+
Logger.WriteLine("Finished");
29+
#endregion
30+
Console.WriteLine($"Logged information to {Logger.OutputFilePath}");
31+
Console.WriteLine("Press any key to exit");
32+
Console.ReadKey();
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.2" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29728.190
5+
MinimumVisualStudioVersion = 15.0.26730.12
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(SolutionProperties) = preSolution
17+
HideSolutionNode = FALSE
18+
EndGlobalSection
19+
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
startcode StartupShutdownSequence
2+
Thread:1 Starting configuration
3+
Thread:1 Calling Endpoint.Start
4+
Thread:1 Inside INeedInitialization.Customize
5+
Thread:1 Inside WantToRunBeforeConfigurationIsFinalized.Run
6+
Thread:1 Inside Feature.Setup
7+
Thread:1 Inside INeedToInstallSomething.Install
8+
Thread:1 Inside FeatureStartupTask.OnStart
9+
Thread:11 Endpoint is processing messages
10+
Thread:11 Calling IEndpointInstance.Stop
11+
Thread:7 Inside FeatureStartupTask.OnStop
12+
Thread:7 Finished
13+
endcode

0 commit comments

Comments
 (0)