Creating Custom Single or Multi-threaded ASP.NET like DTS job

May 19, 2005
May 24, 2005 - (HOSTSEARCH.COM) - Freelance writer and consultant Kingsley Tagbo descirbes how to create a custom ASP.NET like DTS job that can run on a single thread or multiple threads in the background.

Creating a customized job (like a SQL Server DTS Job) for tasks that can be processed using background threads in ASP.NET involves working with 4 or 5 components in Community Server.
1. CommunityServer.Config xml file
2. IJob interface in CommunityServer.Configuration namesapce
3. A Job abstract class that implements the IJob interface (MailingListJob)
4. A Job class that inherits the Job abstract class (ForumsMailingListJob)
5. An abstract class that defines the method which will be invoked when the job runs (MailingList)
6. A class that implements the method that will be invoked when the job runs(ForumsMailingList)
7. A Jobs and Job class provided by CommunityServer that will manage the invocation, scheduling instantiation, threading of all asp.net jobs.
8. A SiteSettingsManager class
CommunityServer.Config file contains a 'Jobs' configurations section for setting up new jobs or changing the behavior of existing jobs. The can be singleThread or multi-threaded. The sample mailing list asp.net job can be added using the configurations settings below.
This interface is a type in CommunityServer.Configuration namespace and is the interface implemented by all ASP.NET Jobs. It includes an Execute(XmlNode node) method. You will have to inherit from this interface to create a custom asp.net job.
namespace CommunityServer.Configuration{
public interface IJob{
void Execute(XmlNode node); }
}
Create an abstract class that will implement the IJob interface and serve as the base class for your custom asp.net job. The custom Job abstract class will be a member of the your namespace. I used KDKEYS.ASPNET.JOBS as the namespace for the Mailing List job.
a.) namespace KDKEYS.ASPNET.JOBS{
public abstract class MailingListJob: IJob{
public MailingListJob(){}
}
}
Create an abstract method that returns the current application implementation of the Custom Job. See an example below.
b.) protected abstract MailingList CurrentMailingList{
get;
}
c.) Create a method which runs the custom ASP.NET job when invoked by theSiteSettingsListIterator delegate with a SettingsID parameter.
protected void CreateLists(int settingsID){
CurrentMailingList.CreateLists(settingsID);
}
The Execute(XmlNode node) method implements IJob.Execute by calling the SiteSettingsManager.IterateSiteSettings method.
d.) public void Execute(XmlNode node){
SiteSettingsManager.IterateSettings(new, SiteSettingsListIterator(this.CreateLists));
}

Create a class that derives from your abstract custom jobs class. This is the class that would be instantiated in your application to execute the custom job class. I created a ForumsMailingListJob class that inherits from the abstract KDKEYS.ASPNET.JOBS.MailingListJob class. Please refer to the example below:
namespace KDKEYS.ASPNET.JOBS{
public class ForumsMailingListJob : MailingListJob
public ForumsMailingListJob(){}
ForumMailingList fml = null;
protected override MailingList CurrentMailingList{
get{
if (fml == null)
fml = new ForumMailingList();
return fml;
}
}


Create an abstract class that defines the methods that will be executed to do some processing. Since the custom asp.net job I implemented finds out what mailing list you belong to and sends you periodic scheduled news update, I created an abstract class named mailing list with one method that is used to create mailing lists(s) for a website as in the code snippet below:
public abstract class MailingList {
public abstract void CreateLists (int settingsID);
}

Inherit from the abstract class above and implement the abstract member which executes and does some work when the custom asp.net job runs. I created a ForumsMailingList class which derives from the abstract MailingList class and implements the CreateLists method.
public abstract class ForumsMailingList {
public override void CreateLists (int settingsID){
SqlDataProvider provider = new SqlDataProvider();
provider.KDKEYSGroupsGetDigestMessage(settingsID);
provider = null;
}
}

You may be asking at this point, 'What Is The Object That Instantiates A Job From CommunityServer.Config?', the answer is the Jobs object. It holds a single instance of al the jobs that it activated on the web server, where each active jobis represented by a Job object. The Jobs class Starts a Job, Stops A Job, manages the threading and scheduling of jobs and uses the System.Threading.Timer class and a callback to invoke active asp.net jobs. The code goes like this:
singleTimer = new Timer(new TimerCallback(call_back),null,Interval, Interval);

You do not have to implement this method, however, you need to call it in the abstract custom job class. The methods enables you to iterate through all the settingsID available.
What about scheduling your asp.net job so that it runs every x minutes, background pooling, thread management and other concerns? Community Server's components will handle all that for you. CommunityServer uses the same architecture and components that I illustrated above to manage a default set of asp.net jobs for emailing, indexing content for its custom search algorithm, updating site statistics, managing pictures and other asp.net jobs.
In this article I have shown how you can take advantage of a popular and available ASP.NET application to create customized ASP.NET jobs that can processed on a background thread(s). I also demonstrated the ease and speed with which you can create scheduled processes without a lot of coding.

You can reach Kingsley via his blogs at http://www.kdkeys.net/blogs/kingsleytagbo



Top 3 Hosts From Our Search

1BlueRay Concepts
2iFast UK
3SatisfyHost