程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> SharePoint中開發自定義Timer Job

SharePoint中開發自定義Timer Job

編輯:C++入門知識

SharePoint中開發自定義Timer Job


SharePoint中開發自定義Timer Job


1. Timer Job簡介

在SharePoint中有一個服務SharePoint timer service(owstimer.exe),這個服務用來進行異步處理一些SharePoint的數據,創建web application等等,為了緩解站點w3wp.exe的壓力,而且Timer 服務可以說是占據了SharePoint的半邊天,沒有他那麼SharePoint將不能正常工作


2. Timer Job 作用

很多時候我們需要定期自動去處理一些數據,例如:為了不影響業務性能有些數據在非工作時間系統自動處理,那麼timer job就能夠幫我們實現這個,在SharePoint中有很多原生的job,而且也支持自定義timer job去滿足業務需求。


3. timer job的基類:

一般我們要編寫一個自定義的timer job的話,我們需要繼承SPJobDefinition ,然後重寫Execute方法,在這個方法裡面添加自己的業務邏輯代碼,如下:


public class MyTimerJobDefinition : SPJobDefinition
{

public MyTimerJobDefinition () : base() { }


public MyTimerJobDefinition (SPWebApplication webApp) :
base(SapConstant.ReminderJobName, webApp, null, SPJobLockType.Job)
{}


public override void Execute(Guid targetInstanceId)
{
//base.Execute(targetInstanceId);
try
{
SPWebApplication webApp = this.Parent as SPWebApplication;

// todo....

}
catch (Exception ex)
{
//log
}
}


public override void Delete()
{
try
{
base.Delete();
}
catch (Exception ex)
{
//log
}
}
}


4. 自定義Timer Job的安裝

在SharePoint中大多數自定義功能都是通過Feature去安裝和部署的,當然Timer Job也是不例外的, 在項目中添加一個feature,然後重寫相應的激活方法,如下:


[Guid("418ce432-d5e5-4fff-9a3f-ec46445bc105")]
public class Feature1EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
DeleteJob(webApp.JobDefinitions);


MyTimerJobDefinition simpleJob = new MyTimerJobDefinition(webApp);


SPDailySchedule schedule = new SPDailySchedule();


schedule.BeginHour = 9;
schedule.BeginMinute = 1;
schedule.BeginSecond = 1;
schedule.EndSecond = 30;
schedule.EndMinute = 6;
schedule.EndHour = 12;


simpleJob.Schedule = schedule;


simpleJob.Title = "My Custom Timer";
simpleJob.Update();

}
catch (Exception ex)
{
//log
}
}

// Uncomment the method below to handle the event raised before a feature is deactivated.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
DeleteJob(webApp.JobDefinitions);
}
catch (Exception ex)
{
//log
}
}

private void DeleteJob(SPJobDefinitionCollection jobs)
{
foreach (SPJobDefinition job in jobs)
{
if (job.Name.Equals("JobName",
StringComparison.OrdinalIgnoreCase))
{
job.Delete();
}
}
}
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved