Friday, November 18, 2011

Creating, Deploying, and Debugging Custom Timer Jobs in Windows SharePoint

Windows SharePoint Services 3.0 lets you create custom jobs that are executed at set intervals. These jobs, known as timer jobs, are similar to those tasks that you can create in any version of Windows by using the Task Scheduler application. This capability is useful for scheduled process jobs because you keep everything in Windows SharePoint Services rather than create a console .exe file that is configured to run at set intervals by using the Windows Task Scheduler. The major benefits to using the Windows SharePoint Services timer service compared to using Windows Task Scheduler is that the timer service knows the topology of the Office SharePoint Server farm, and that you can load balance the jobs across all the servers in the farm or tie them to specific servers that are running particular services. This Microsoft Office Visual How To demonstrates how to create a job that requests the home page of each site collection in a Web application to force just-in-time (JIT) compilation and to speed up the first request.

public SharePointWarmupJob (SPWebApplication webApp)
  : base(JOB_NAME, webApp, null, SPJobLockType.ContentDatabase) {
    this.Title = JOB_NAME;
}
public override void Execute (Guid targetInstanceId) {
  if (this.WebApplication.Sites.Count > 0)
    WarmUpSiteCollection(this.WebApplication.Sites[0]);
}
private void WarmUpSiteCollection (SPSite siteCollecion) {
  WebRequest request = WebRequest.Create(siteCollecion.Url);
  request.Credentials = CredentialCache.DefaultCredentials;
  request.Method = "GET";
  WebResponse response = request.GetResponse();
  response.Close();
}

Installing Custom Timer Jobs
After you create a timer job, the next step is to install it. Unfortunately, you cannot do this through the provided browser-based user interface; however, you can use the Windows SharePoint Services API. Developers can create custom applications, use PowerShell, or create custom STSADM commands to install the custom timer job. However, another technique is to use a Windows SharePoint Services 3.0 Feature to install and uninstall the custom timer job. Use a Feature receiver to install the feature as shown in the following example.
public override void FeatureActivated (SPFeatureReceiverProperties props) {
  SPWebApplication webApp = props.Feature.Parent as SPWebApplication;
  if (webApp == null)
    throw new SPException("Error obtaining reference to Web application.");

  // Ensure the job is not already registered.
  foreach (SPJobDefinition job in webApp.JobDefinitions)
    if (job.Name == JOB_NAME) job.Delete();

  // Install job.
  SharePointWarmupJob warmupJob = new SharePointWarmupJob(webApplication);

  // Schedule the job to run every minute all the time.
  SPMinuteSchedule schedule = new SPMinuteSchedule();
  schedule.BeginSecond = 0;
  schedule.EndSecond = 59;
  schedule.Interval = 1;
  warmupJob.Schedule = schedule;

  // Save changes.
  warmupJob.Update();
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

Open Hardware Monitor A Handy Tool for System Monitoring

#Open #Hardware #Monitor: A Handy Tool for #System #Monitoring #OpenHardwareMonitor is a free, #opensource #software designed to monitor t...