`

MOSS中的计时器作业

阅读更多

这方面的文章园子里有几篇了,不过大家基本上都是参考了http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx这篇文章,在此我也推荐大家仔细看下本文及文中涉及到的相关文章。工作中也遇到了类似的需求,在此也做个总结。

1.MOSS中已经提供了定时器的功能,我们要开发自己的定时器需要继承自SPJobDefinition类,在重写的Execute方法写自己的业务逻辑。我的需求是找出列表中符合条件的item,并做mail通知,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace CaryTimer
{
    public class ListRemindEvent : SPJobDefinition
    {
        public ListRemindEvent() : base() { }

        public ListRemindEvent(string _timername, SPWebApplication _wp)
            : base(_timername, _wp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "TestTimer";
        }

        public override void Execute(Guid targetInstanceId)
        {    
                SPWebApplication webApp = this.Parent as SPWebApplication;
                SPContentDatabase contentDB = webApp.ContentDatabases[targetInstanceId];
                SPWeb web = contentDB.Sites[0].AllWebs[0];
                string sendTo = "";
                string mailTitle = "";
                string mailBody = "";
			 //实现自己业务逻辑,找出复合条件的并发mail做相关通知。
                SPUtility.SendEmail(web, false, false, sendTo, mailTitle, mailBody);                  
        }
     }
}

2.该类完成后我们使用Feature来部署该功能,我们写一个自己的安装类,如下:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CaryTimer
{
    public class ListRemindEventInstaller : SPFeatureReceiver
    {
        const string caryTimerName = "Testtimer";
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        { }
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        { }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
           SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
	      //判断是否已存在
                if (job.Name == caryTimerName)
                {
                    job.Delete();
                }
            }
            ListRemindEvent timer = new ListRemindEvent(caryTimerName, site.WebApplication);
	    /下边是设置定时器的执行计划部分。
              SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            timer.Schedule = schedule;
            timer.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == caryTimerName)
                {
                    job.Delete();
                }
            }
        }
    }
}

3.需要在"c:\program files\common files\microsoft shared\web server extensions\12\Template\features"文件夹下建立CaryTimer文件夹,在该文件夹下建一个Feature.xml,代码如下:

<Feature 
  Id="6283ADA0-B882-47fe-8507-D8CC763DC320" 
  Title="CaryTimer" 
  Description=" CaryTimer  des" 
  Scope="Site" 
  Hidden="FALSE"   
  ReceiverAssembly="HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b38a04419cc857d9"
  ReceiverClass="HelloWorld.ListRemindEventInstaller"
  xmlns="http://schemas.microsoft.com/sharepoint/"> 
</Feature>

4.然后我们使用一个批处理来安装Feature,批处理代码如下:

@SET TEMPLATEDIR="c:\program files\common files\microsoft shared\web server extensions\12\Template"

@SET STSADM="c:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm"

@SET GACUTIL="d:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe"

Echo Installing HelloWorld.dll in GAC

%GACUTIL% -if bin\debug\HelloWorld.dll

Echo Copying files to TEMPLATE directory

xcopy /e /y TEMPLATE\* %TEMPLATEDIR%

Echo Installing feature

%STSADM% -o installfeature -filename HelloWorld\feature.xml -force

IISRESET

REM cscript c:\windows\system32\iisapp.vbs /a "SharePointDefaultAppPool" /r

5.项目完成后,我们要添加强命名密钥。部署成功后就可以在网站集功能中看见该Feature,激活该Feature后,在管理中心—操作—计时器作业定义中可以看见该定时器的相关信息,并且可以禁用和启用该定时器,在管理中心—操作—计时器作业状态中可以该定时器最后一次运行的情况。

6.如果我们要调试该定时器我们需要附加OwsTimer.exe进程,每次更改后需走以下步骤:
6.1.使用批处理从新部署Feature
6.2.先Deactivate feature, 然后activate feature.
6.3.命令行:net stop SPTimerV3
6.4.命令行:net start SPTimerV3
6.5.Visual Studio: Attach to process: OWSTIMER.EXE

7.如果你在ListRemindEvent类中需要读取外部文件的配置,直接读web.config是读不到的,我们需要在c:/program files/common files/microsoft shared/…/12/bin目录里新建一个文件OwsTimer.exe.config,并做相关配置如下:

<configuration>
  <appSettings>
   <add key="key" value="value" />
  </appSettings>
</configuration>
然后用ConfigurationManager.AppSettings.Get("key"); 来取得这个值。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics