WorkItemTimerJobState 类
用来跟踪每个迭代通过处理工作项的状态。
继承层次结构
System.Object
Microsoft.Office.Server.Utilities.WorkItemTimerJobState
命名空间: Microsoft.Office.Server.Utilities
程序集: Microsoft.Office.Server(位于 Microsoft.Office.Server.dll 中)
语法
声明
Public Class WorkItemTimerJobState
用法
Dim instance As WorkItemTimerJobState
public class WorkItemTimerJobState
备注
从此类派生一个类,以便支持在处理从一个SPWorkItemJobDefinition对象派生的计时器作业中的工作项时更改网站和网站集的位置的点处执行的代码。
下面的代码示例演示一个示例工作项作业定义。
示例
using System;
using System.Globalization;
using System.Threading;
using Microsoft.Office.Server.Utilities;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace Microsoft.SDK.SharePointServer.Samples
{
/// <summary>
/// This a sample work item timer job definition using TimerJobUtility
/// </summary>
/// <remarks>
/// <para>
/// This example shows the structure of a work item timer job and
/// has comments for where you would add your code to perform specific
/// tasks.
/// </para>
/// </remarks>
public sealed class SampleWorkItemJobDefinition : SPWorkItemJobDefinition
{
// Replace with your job name. Must be unique in the farm.
private static string jobName = "AdventureWorksSampleWorkItemTimerJob";
// Replace this with your own GUID
private static Guid sampleWorkItemType = new Guid("3E445118-77B7-4917-A8E0-C4D729AC5805");
/// <summary>
/// Instance of the state class.
/// </summary>
/// <remarks>
/// <para>
/// Keeps track of the state of the job across work items.
/// </para>
/// <para>
/// This is instantiated and disposed within the Execute method since the instance
/// of the SPWorkItemJobDefinition class can be reused by the timer job
/// infrasturcture.
/// </para>
/// </remarks>
private SampleWorkItemJobState timerJobState;
/// <summary>
/// This class keeps track of the state of the work item job.
/// </summary>
/// <remarks>
/// <para>
/// Add properties to this class for any state you need kept.
/// </para>
/// </remarks>
internal class SampleWorkItemJobState : WorkItemTimerJobState
{
// Set this to false if you do not require the WorkItemTimerJobState.Web
// property to be fetched for each work item
private static bool fetchWeb = true;
// Set this to false if you do not require the WorkItemTimerJobState.User
// property to be fetched for each work item
private static bool fetchUser = true;
/// <summary>
/// Default constructor.
/// </summary>
public SampleWorkItemJobState()
: base(SampleWorkItemJobState.fetchWeb, SampleWorkItemJobState.fetchUser)
{
}
/// <summary>
/// Called after changing the <see cref="Site" /> for next work item.
/// </summary>
/// <param name="wi"></param>
protected override void OnSiteChanged(SPWorkItem workItem)
{
base.OnSiteChanged(workItem);
// Add your code here if you need to update any state that is tied
// to the SPSite of the work item
// Similarly you can override OnSiteChanging to perform any per-SPSite
// processing required before the SPSite is change.
// Similarly you can override OnWebChanging/OnWebChanged for any state
// or processing required on a per-SPWeb basis.
}
}
/// <summary>
/// Process one work item
/// </summary>
/// <param name="workItem">work item to process</param>
/// <param name="timerJobState">job state</param>
/// <remarks>
/// <para>
/// This method is called in the culture of timerJobState.Web
/// </para>
/// </remarks>
private void ProcessWorkItemCore(SPWorkItem workItem, SampleWorkItemJobState timerJobState)
{
// Put your code here
}
/// <summary>
/// Default constructor. Only used for deserialization.
/// </summary>
public SampleWorkItemJobDefinition() : base()
{
}
/// <summary>
/// Public constructor. Use this to register the job on a web application.
/// </summary>
/// <param name="webApplication">web application to register job to run on</param>
/// <remarks>
/// <para>
/// This job will process all site collections within the web application.
/// </para>
/// <para>
/// See the registration PowerShell script for how to register the job.
/// </para>
/// </remarks>
public SampleWorkItemJobDefinition(SPWebApplication webApplication)
: base(SampleWorkItemJobDefinition.jobName, webApplication)
{
}
/// <summary>
/// Name of the job
/// </summary>
/// <remarks>
/// <para>
/// This will be shown in the central administration timer job UI.
/// </para>
/// </remarks>
public override string DisplayName
{
get
{
return Microsoft.SDK.SharePointServer.Samples.SampleWorkItemJob.JobDisplayName;
}
}
/// <summary>
/// Description of the job
/// </summary>
/// <remarks>
/// <para>
/// This will be shown in the central administration timer job UI.
/// </para>
/// </remarks>
public override string Description
{
get
{
return Microsoft.SDK.SharePointServer.Samples.SampleWorkItemJob.JobDescription;
}
}
/// <summary>
/// Type of work item
/// </summary>
/// <returns>The Guid representing the type of work item</returns>
public override Guid WorkItemType()
{
return SampleWorkItemJobDefinition.sampleWorkItemType;
}
public override void Execute(SPJobState jobState)
{
// create an instance of the state class
timerJobState = new SampleWorkItemJobState();
try
{
// Call the base class implementation which will process
// the work items
base.Execute(jobState);
}
finally
{
// destroy the job state instance to ensure it
// does not get reused
timerJobState.Dispose();
timerJobState = null;
}
}
protected override bool ProcessWorkItem(
SPContentDatabase db,
SPWorkItemCollection workItems,
SPWorkItem workItem,
SPJobState jobState)
{
TimerJobUtility tju = new TimerJobUtility(SampleWorkItemJobDefinition.jobName, jobState);
// Call ProcessWorkItem, passing in our timerJobState
return tju.ProcessWorkItem(workItems, workItem, this.timerJobState,
delegate(SPWorkItem wi, WorkItemTimerJobState tjs)
{
// Save the current cultures
CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
CultureInfo savedUICulture = Thread.CurrentThread.CurrentUICulture;
try
{
SampleWorkItemJobState sampleTJS = (SampleWorkItemJobState)tjs;
// Set the thread cultures to match the work item web
Thread.CurrentThread.CurrentCulture = sampleTJS.Web.Locale;
Thread.CurrentThread.CurrentUICulture = sampleTJS.Web.UICulture;
// Process the work item
ProcessWorkItemCore(wi, sampleTJS);
}
catch
{
// protect against exception filters running in the web's culture
throw;
}
finally
{
// Restore the thread cultures
Thread.CurrentThread.CurrentCulture = savedCulture;
Thread.CurrentThread.CurrentUICulture = savedUICulture;
}
}
);
}
}
}
线程安全性
该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。