您可以运行一个控制台应用程序(或将其作为Windows 服务运行),该应用程序可以使用一个计时器扫描您的 TTL 列表,该计时器在设定的时间间隔内按您的意愿处理它们。都可以在 .net 中完成,从而避免您将业务逻辑存储在 SQL Server 内的 SSIS 包中。
如果您要走这条路,我建议您编写一个也可以作为控制台应用程序运行的 Windows 服务。查询Environment.UserInteractive
属性以确定正在运行的版本 - 这将有助于您的开发,因为控制台应用程序可能比 Windows 服务更冗长。
这是一个代码示例:
public partial class Service1 : ServiceBase
{
//Need to keep a reference to this object, else the Garbage Collector will clean it up and prevent further events from firing.
private System.Threading.Timer _timer;
static void Main(string[] args)
{
if (Environment.UserInteractive)
{
var service = new Service1();
Log.Debug("Starting Console Application");
service.OnStart(args);
// The service can now be accessed.
Console.WriteLine("Service ready.");
Console.WriteLine("Press <ENTER> to terminate the application.");
Console.ReadLine();
service.OnStop();
return;
}
var servicesToRun = new ServiceBase[]
{
new Service1()
};
Run(servicesToRun);
}
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// For a single instance, this is a bit heavy handed, but if you're creating of a number of them
// the NT service will need to return in a short period of time and thus I use QueueUserWorkItem
ThreadPool.QueueUserWorkItem(SetupTimer, args);
}
protected override void OnStop()
{
}
private void SetupTimer(object obj)
{
//Set the emailInterval to be 1 minute by default
const int interval = 1;
//Initialize the timer, wait 5 seconds before firing, and then fire every 15 minutes
_timer = new Timer(TimerDelegate, 5000, 1, 1000 * 60 * interval);
}
private static void TimerDelegate(object stateInfo)
{
//Perform your DB TTL Check here
}
}