专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »办公软件 » 计时器:MOSS中的计时器作业 »正文

计时器:MOSS中的计时器作业

来源: 发布时间:星期日, 2009年2月8日 浏览:5次 评论:0
  这方面文章园子里有几篇了不过大家基本上都是参考了http://www.andrewconnell.com/blog/articles/CreatingCustomSharePoTimerJobs.aspx这篇文章在此我也推荐大家仔细看下本文及文中涉及到相关文章工作中也遇到了类似需求在此也做个整理总结

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

using ;
using .Collections.Generic;
using .Text;
using Microsoft.SharePo.Administration;
using Microsoft.SharePo;
using Microsoft.SharePo.Utilities;
CaryTimer
{
  public ListRemindEvent : SPJobDefinition
  {
    public ListRemindEvent : base { }
    public ListRemindEvent( _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];
         sendTo = "";
         mailTitle = "";
         mailBody = "";
       //实现自己业务逻辑找出复合条件并发mail做相关通知
        SPUtility.SendEmail(web, false, false, sendTo, mailTitle, mailBody);         
    }
   }
}
  2.该类完成后我们使用Feature来部署该功能我们写个自己安装类如下:

using ;
using .Collections.Generic;
using .Text;
using Microsoft.SharePo;
using Microsoft.SharePo.Administration;
CaryTimer
{
  public ListRemindEventInstaller : SPFeatureReceiver
  {
    const caryTimerName = "Testtimer";
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    { }
    public override void FeatureUning(SPFeatureReceiverProperties properties)
    { }
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
      SPSite site = properties.Feature.Parent as SPSite;
      foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
      {
     //判断是否已存在
         (job.Name caryTimerName)
        {
          job.Delete;
        }
      }
      ListRemindEvent timer = ListRemindEvent(caryTimerName, site.WebApplication);
    /下边是设置定时器执行计划部分
       SPMinuteSchedule schedule = 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)
      {
         (job.Name caryTimerName)
        {
          job.Delete;
        }
      }
    }
  }
}
  3.需要在"c:program filescommon filesmicrosoft sharedweb server extensions12Templatefeatures"文件夹下建立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/sharepo/">
</Feature>
  4.然后我们使用个批处理来安装Feature批处理代码如下:

  @SET TEMPLATEDIR="c:program filescommon filesmicrosoft sharedweb server extensions12Template"

  @SET STSADM="c:program filescommon filesmicrosoft sharedweb server extensions12binstsadm"

  @SET GACUTIL="d:Program FilesMicrosoft Visual Studio 8SDKv2.0Bingacutil.exe"

  Echo Installing HelloWorld.dll in GAC

  %GACUTIL% - bindebugHelloWorld.dll

  Echo Copying files to TEMPLATE directory

  xcopy /e /y TEMPLATE* %TEMPLATEDIR%

  Echo Installing feature

  %STSADM% -o feature -filename HelloWorldfeature.xml -force

  IISRESET

  REM cscript c:windowssystem32iisapp.vbs /a "SharePoDefaultAppPool" /r

  5.项目完成后我们要添加强命名密钥部署成功后就可以在网站WebSite集功能中看见该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"); 来取得这个值




0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: