Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Adding an option to disable monitoring of the route file (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
HEskandari authored and timbussmann committed Jul 7, 2017
1 parent 981bfb4 commit 97c3b30
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ public static void UseFileBasedRouting(this RoutingSettings config)
config.GetSettings().EnableFeatureByDefault<FileBasedRoutingFeature>();
}

/// <summary>
/// Enables routing configured with the routing configuration file.
/// </summary>
/// <param name="config">The configuration object.</param>
/// <param name="updateInterval">The interval the route file should be checked for changes.</param>
public static void UseFileBasedRouting(this RoutingSettings config, TimeSpan updateInterval)
{
if (updateInterval < TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update interval cannot be negative.");

config.GetSettings().Set(FileBasedRoutingFeature.RouteFileUpdateInterval, updateInterval);
config.GetSettings().EnableFeatureByDefault<FileBasedRoutingFeature>();
}

/// <summary>
/// Enables routing configured with the routing configuration file under <paramref name="configurationFilePath"/>
/// </summary>
Expand All @@ -26,6 +40,17 @@ public static void UseFileBasedRouting(this RoutingSettings config, string confi
config.UseFileBasedRouting(UriHelper.FilePathToUri(configurationFilePath));
}

/// <summary>
/// Enables routing configured with the routing configuration file under <paramref name="configurationFilePath"/>
/// </summary>
/// <param name="config">The configuration object.</param>
/// <param name="configurationFilePath">The path to the configuration file.</param>
/// <param name="updateInterval">The interval the route file should be checked for changes.</param>
public static void UseFileBasedRouting(this RoutingSettings config, string configurationFilePath, TimeSpan updateInterval)
{
config.UseFileBasedRouting(UriHelper.FilePathToUri(configurationFilePath), updateInterval);
}

/// <summary>
/// Enables routing configured with the routing configuration file under <paramref name="configurationFileUri"/>
/// </summary>
Expand All @@ -36,5 +61,21 @@ public static void UseFileBasedRouting(this RoutingSettings config, Uri configur
config.GetSettings().Set(FileBasedRoutingFeature.RoutingFilePathKey, configurationFileUri);
config.GetSettings().EnableFeatureByDefault<FileBasedRoutingFeature>();
}

/// <summary>
/// Enables routing configured with the routing configuration file under <paramref name="configurationFileUri"/>
/// </summary>
/// <param name="config">The configuration object.</param>
/// <param name="configurationFileUri">The <see cref="Uri"/> to the configuration file.</param>
/// <param name="updateInterval">The interval the route file should be checked for changes.</param>
public static void UseFileBasedRouting(this RoutingSettings config, Uri configurationFileUri, TimeSpan updateInterval)
{
if (updateInterval < TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update interval cannot be negative.");

config.GetSettings().Set(FileBasedRoutingFeature.RouteFileUpdateInterval, updateInterval);
config.GetSettings().Set(FileBasedRoutingFeature.RoutingFilePathKey, configurationFileUri);
config.GetSettings().EnableFeatureByDefault<FileBasedRoutingFeature>();
}
}
}
20 changes: 14 additions & 6 deletions src/NServiceBus.FileBasedRouting/FileBasedRoutingFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class FileBasedRoutingFeature : Feature
static ILog log = LogManager.GetLogger<FileBasedRoutingFeature>();

public const string RoutingFilePathKey = "NServiceBus.FileBasedRouting.RoutingFileUri";
public const string RouteFileUpdateInterval = "NServiceBus.FileBasedRouting.RouteFileUpdateInterval";

public FileBasedRoutingFeature()
{
Defaults(s=>
Defaults(s =>
{
s.SetDefault(RoutingFilePathKey, UriHelper.FilePathToUri("endpoints.xml"));
s.SetDefault(RouteFileUpdateInterval, TimeSpan.FromSeconds(30));
s.SetDefault<UnicastSubscriberTable>(new UnicastSubscriberTable());
});
}
Expand All @@ -31,6 +33,7 @@ protected override void Setup(FeatureConfigurationContext context)
var unicastRoutingTable = context.Settings.Get<UnicastRoutingTable>();
var unicastSubscriberTable = context.Settings.Get<UnicastSubscriberTable>();

var routeFileUpdateInterval = context.Settings.Get<TimeSpan>(RouteFileUpdateInterval);
var routingFileUri = context.Settings.Get<Uri>(RoutingFilePathKey);
var routingFile = new XmlRoutingFileAccess(routingFileUri);
var routingFileParser = new XmlRoutingFileParser();
Expand All @@ -41,7 +44,10 @@ protected override void Setup(FeatureConfigurationContext context)
// ensure the routing file is valid and the routing table is populated before running FeatureStartupTasks
UpdateRoutingTable(routingFileParser, routingFile, unicastRoutingTable, unicastSubscriberTable, nativeSends, nativePublishes);

context.RegisterStartupTask(new UpdateRoutingTask(() => UpdateRoutingTable(routingFileParser, routingFile, unicastRoutingTable, unicastSubscriberTable, nativeSends, nativePublishes)));
if (routeFileUpdateInterval > TimeSpan.Zero)
{
context.RegisterStartupTask(new UpdateRoutingTask(() => UpdateRoutingTable(routingFileParser, routingFile, unicastRoutingTable, unicastSubscriberTable, nativeSends, nativePublishes), routeFileUpdateInterval));
}

// if the transport provides native pub/sub support, don't plug in the FileBased pub/sub storage.
if (context.Settings.Get<TransportInfrastructure>().OutboundRoutingPolicy.Publishes == OutboundRoutingType.Unicast)
Expand Down Expand Up @@ -95,16 +101,18 @@ static void UpdateRoutingTable(XmlRoutingFileParser routingFileParser, XmlRoutin
class UpdateRoutingTask : FeatureStartupTask, IDisposable
{
Action updateRoutingCallback;
Timer updateTimer;
TimeSpan routeFileUpdateInterval;
Timer updateTimer;

public UpdateRoutingTask(Action updateRoutingCallback)
public UpdateRoutingTask(Action updateRoutingCallback, TimeSpan routeFileUpdateInterval)
{
this.updateRoutingCallback = updateRoutingCallback;
this.updateRoutingCallback = updateRoutingCallback;
this.routeFileUpdateInterval = routeFileUpdateInterval;
}

protected override Task OnStart(IMessageSession session)
{
updateTimer = new Timer(state => updateRoutingCallback(), null, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
updateTimer = new Timer(state => updateRoutingCallback(), null, routeFileUpdateInterval, routeFileUpdateInterval);

return Task.CompletedTask;
}
Expand Down

0 comments on commit 97c3b30

Please sign in to comment.