首先介绍一下什么是定时器作业,说的再多,也不如一张图说的清楚
这两张图应该把我想说的已经表达清楚了,下一步介绍一下如何自定义Timer Job
第一步:创建一个类(CustomTimerJob.cs)
第二步:引用 using Microsoft.SharePoint.Administration;并继承SPJobDefinition
第三步:构造 三个函数
public CustomTimerJob()
: base()
{
}
public CustomTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public CustomTimerJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Custom Timer Job"; //在上面的图处上可以看到这个名字
}
第四步:override Execute 处理自己的业务
public override void Execute(Guid targetInstanceId)
{
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
SPList taskList = contentDb.Sites[0].RootWeb.Lists["Product"];
// create a new task, set the Title to the current day/time, and update the item
SPListItem newTask = taskList.Items.Add();
newTask["Title"] = DateTime.Now.ToString();
newTask.Update();
}
在上一篇 自定义 Features 里面,当激活功能时,到管理中心可以看到自己的定义的 Timer Job
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
if (site != null)
{
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == CUSTOM_TIMER_JOB)
{
job.Delete();
}
}
}
//开始Job
CustomTimerJob customTimerJob = new CustomTimerJob(CUSTOM_TIMER_JOB, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
customTimerJob.Schedule = schedule;
customTimerJob.Update();
}
之后部署,到网站网站集里停止再激活,之后去管理中心,就可以看到自己定义的Job了
更多精彩请关注
转载于:https://www.cnblogs.com/Fengger/archive/2012/06/02/2532047.html