If you need simple yet easy configurable scheduler in your ASP.NET application, AnjLab.FX Scheduler might be your choise.
To use AnjLab.FX Scheduler you need to do 3 simple steps:
- Get the latest version of AnjLab.FX from github, make a build and add AnjLab.FX.dll as a reference to your project;
- Implement
AnjLab.FX.Sys.ICommand
interface on your task workers, like this:public class HelloWorldTask : ICommand
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(HelloWorldTask));
public void Execute()
{
Log.Info("Hello World!");
}
} - Configure tasks schedule in
web.config
. To do this you need to:- add the following line to
web.config/configuration/configSections
:<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
...
<section name="triggers" type="AnjLab.FX.Tasks.Scheduling.SchedulerConfigSection, AnjLab.FX"/>
... - define
triggers
section:<configuration>
...
<triggers>
<!--
<daily tag='restoreDB' timeOfDay='23:00'/>
<weekly tag='backupDB' timeOfDay='01:30' weekDays='monday,friday'/>
<hourly tag='delTempFiles' minutes='30'/>
<interval tag='dumpLog' interval='00:05:00'/>
<once tag='upgradeDB' dateTime='01/15/2007 23:00'/>
<monthly tag='archiveDB' monthDay='29' timeOfDay='23:00'/>
-->
<interval tag='helloworld-task' interval='00:00:10'/>
</triggers>
...
</configuration>
Here we defined named trigger "helloworld-task" to be triggered every 10 seconds.
- add the following line to
- Map trigger names to your task workers and start up the scheduler.
To map your task workers you create instance of KeyedFactory and register your tasks. We propose you do this inGlobal.asax
Application_Start
method:protected void Application_Start(object sender, EventArgs e)
{
// Map trigger names to task workers
var factory = new KeyedFactory<string, ICommand>();
factory.RegisterType<HelloWorldTask>("helloworld-task");
// Start up scheduler
var scheduler = new Scheduler<ICommand>(factory);
var triggers = (List<ITrigger>)ConfigurationManager.GetSection("triggers");
scheduler.RegisterTriggers(triggers.ToArray());
scheduler.Start();
}
Thats it!
Resources:
- AnjLab.FX on github: http://github.com/anjlab/fx
- Here is an article about KeyedFactory pattern in Russian
P.S.
By the way, you can also use this API to schedule your tasks in Windows.Forms applications as well.
P.P.S.
AnjLab.FX is a framework we built during development of our projects. Its continue evolving and you can use it in your applications without any restrictions.