« If everything could be hosted! | Main | Silverlight Desktop Applications »

Using locks in Custom MOSS Timer Jobs

Microsoft Office SharePoint Server(MOSS) 2007 provides both out of box timer jobs as well as the option to create your own custom timer.The MOSS OWSTimer.exe process controls and executes all timer definition jobs for SharePoint.Custom timer jobs can be created using either a feature or using a solution.
Microsoft.SharePoint.Administration.SPJobDefinition class contains the definition for the MOSS timer jobs. MOSS passes the GUID of the content database for the site the job is registered with. You can use this GUID to obtain a reference to a content database, then a site collection, and finally a site within the collection (SPWeb).

The class containing the invocation code for the timer jobs must inherit from the Microsoft.SharePoint.Administration.SPJobDefinition class. The constructors need to be created and the Execute() method must be overridden as mentioned below. The Execute() method is overridden for performing the custom activity that the user has planned to do using the timer jobs. The Execute() is invoked by the MOSS timer services but if there is no lock applied on the code then concurrency issues arise if different users are planning to trigger the timer job at the same time. To overcome concurrency issues the Execute() code must be locked by the process that is executing and the other concurrent process must wait till the lock is removed.

Scenario: Users in an organization are planning to synchronize lists(which they have access on) in a MOSS web application. They are performing the activity by using a UI (ASP.Net web application running under MOSS context) and creating timer jobs that in turn run and complete the process. Users who are requesting for the sync activity are notified with a mail in the end on the success or failure of the job. In our current scenario we are planning to use the included property bag (SPJobDefinintion.Properties) to store your custom properties. The property bag stores the requestor alias(UserAlias) and the SiteUrl. namespace Infosys.Blogs
{public class TimerJobClass : SPJobDefinition
{
            
/// <summary>
/// Base class constructor
/// </summary>
public TimerJobClass() : base()
{    
}
  
public TimerJobClass(string jobName, SPService service, SPServer server, SPJobLockType targetType)  : base (jobName, service, server, targetType)
{           
} /// <summary>
/// Constructor that accepts the JobName,SPWebapplication 
/// </summary> 
public TimerJobClass(string jobName, SPWebApplication webApplication)
        : base (jobName, webApplication, null, SPJobLockType.ContentDatabase) 
{    

 

 

/// <summary>
/// Called by MOSS when the Job is kicked off for execution
/// </summary>
/// <param name="contentDbId"></param>
public override void Execute(Guid contentDbId)
{
lock (timerLock)
{
 //Fetch the value from the Job PropertyBag
if (this.Properties.ContainsKey(JobPBValue))
{
fetchJobPB = this.Properties[timerJobName].ToString();
}
string[] jobPropertyBag = fetchJobPB.Split(','); 

string siteUrl = jobPropertyBag[jobPropertyBag.Length - 0];
string userAlias = jobPropertyBag[jobPropertyBag.Length - 1];
//Queue at the Root site collection level of the web application
currentSite = new SPSite(siteUrl);
currentSite = new SPSite(currentSite.Url);
currentWeb  = currentSite.OpenWeb();  //Body of code - writes to a list
..
//End of code - writes to a list
SPUtility.SendEmail(currentWeb, false, false, userAlias, "Status of Timer Job" + this.Name, “The Job is Completed”);
}

}}}

 

 

TrackBack

TrackBack URL for this entry:
http://www.infosysblogs.com/apps/mt-tb.cgi/1852

Comments

Good post.

I have created one timer job that sends reminder emails for over due tasks. I am all done with activating the timer job as a site collection feature. but the problem is when the feature is activated, the Execute() method is not getting fired. What could be the reason for this?

Thanks,
Sudheer

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter