Path: redb-route/redb.Route/Components/TimerComponent.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Components/TimerComponent.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Components;
///
/// Timer component. Scheme: "timer".
/// Generates exchanges on a periodic schedule. Used as a route source (From).
/// URI: timer://name?period=1000&delay=0&repeatCount=-1
///
[EndpointRole(EndpointRole.Consumer, Hint = "Use it as a From() source.")]
public class TimerComponent : ComponentBase
{
///
public override string Scheme => "timer";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new TimerEndpointOptions();
options.BindFromUri(uri.RawParameters);
options.Validate();
return new TimerEndpoint(uri, this, options);
}
}
///
/// Timer endpoint options.
///
public class TimerEndpointOptions : EndpointOptions
{
/// Period between fires in milliseconds (default: 1000).
public int Period { get; set; } = 1000;
/// Initial delay before first fire in milliseconds (default: 0).
public int Delay { get; set; }
/// Number of times to fire. -1 means indefinitely (default: -1).
public int RepeatCount { get; set; } = -1;
///
public override void Validate()
{
if (Period <= 0)
throw new ArgumentOutOfRangeException(nameof(Period), Period, "Period must be greater than 0.");
}
}
///
/// Timer endpoint. The consumer is the primary artifact — a timer that fires periodically.
///
public class TimerEndpoint : EndpointBase
{
/// Creates a timer endpoint.
public TimerEndpoint(EndpointUri uri, TimerComponent component, TimerEndpointOptions options)
: base(uri, component, options)
{
}
///
public override IProducer CreateProducer()
{
throw new NotSupportedException("Timer endpoints do not support producers. Use them only as From() source.");
}
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
return new TimerConsumer(this, processor, Options);
}
}
///
/// Timer consumer. Fires the processor on a periodic schedule.
/// Creates a new exchange per tick.
///
public class TimerConsumer : DrainableConsumer
{
private readonly TimerEndpoint _endpoint;
private readonly TimerEndpointOptions _options;
///
protected override IEndpoint ConsumerEndpoint => _endpoint;
///
protected override string ConsumerName => _endpoint.Uri.NormalizedKey;
/// Gets the number of fires completed.
public long FireCount { get; private set; }
/// Creates a timer consumer.
public TimerConsumer(TimerEndpoint endpoint, IProcessor processor, TimerEndpointOptions options)
: base(processor)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_options = options ?? throw new ArgumentNullException(nameof(options));
}
///
protected override async Task RunAsync(CancellationToken pollCt, CancellationToken processingCt)
{
if (_options.Delay > 0)
await Task.Delay(_options.Delay, pollCt).ConfigureAwait(false);
var period = TimeSpan.FromMilliseconds(_options.Period);
var count = 0;
while (!pollCt.IsCancellationRequested)
{
if (_options.RepeatCount >= 0 && count >= _options.RepeatCount)
break;
var exchange = CreateTimerExchange(count);
await ProcessWithTracking(exchange, processingCt).ConfigureAwait(false);
FireCount++;
count++;
if (_options.RepeatCount >= 0 && count >= _options.RepeatCount)
break;
try
{
await Task.Delay(period, pollCt).ConfigureAwait(false);
}
catch (OperationCanceledException) when (pollCt.IsCancellationRequested)
{
break;
}
}
}
private Exchange CreateTimerExchange(int fireIndex)
{
var message = new Message
{
Body = null,
Headers =
{
["CamelTimerName"] = _endpoint.Uri.Path,
["CamelTimerFiredTime"] = DateTimeOffset.UtcNow,
["CamelTimerCounter"] = fireIndex
}
};
var exchange = Exchange.Create(message, _endpoint.ScopeFactory);
exchange.Pattern = ExchangePattern.InOnly;
return exchange;
}
}