MOSS Job 开发简介
简介
前段时间在给客户用MOSS开发叉车租赁与展示系统时,碰到要根据一定的业务规则来提醒用户进入系统操作与定时计算的功能.当时就想MOSS中有没有这种特性,经过查SDK,发现MOSS已经提供了定时器的功能.我们要开发自定义的定时器必需继承SPJobDefinition类,在重写Execute方法时加入自己的业务逻辑.部署JOB时要用MOSS的Feature,使用SPFeatureReceiver这个类来部署.
开发步骤
-
建立MOSS的解决方案.最后利用WSP文件格式来发布方案到MOSS中,因此我们借助开源工具STSDEV创建VS2008的方案文件.
首先到http://www.codeplex.com/stsdev此站点下载工具软件.(具体使用见官方语言网站).
下载完后双击STSDEV.EXE文件创建方案如下:
必须要点击Signing Key按钮创建强名,其它设置如上图,点创建后如下图:
点"OK"创建成功
- 用VS2008打开创建好的方案.在工程下添加TimeJobTest.cs类文件,此文件写JOB的业务逻辑如下代码.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace TimeJob
{
public class TimeJobTest:SPJobDefinition
{
public TimeJobTest() : base() { }
public TimeJobTest(string _timername, SPWebApplication _wp)
: base(_timername, _wp, null, SPJobLockType.ContentDatabase)
{
this.Title = "TimeJobTest";
}
public override void Execute(Guid targetInstanceId)
{
//base.Execute(targetInstanceId);
try
{
SPWebApplication webapp = this.Parent as SPWebApplication;
SPContentDatabase contentdb = webapp.ContentDatabases[targetInstanceId];
SPWeb web = contentdb.Sites[0].AllWebs[0];
SPList list = web.Lists["Config"];
string body = "No Body";
foreach (SPListItem item in list.Items)
{
if (item["Title"].ToString() == "dd")
body = item["ddd"].ToString();
}
SPUtility.SendEmail(web, false, false, "test@qsh.com", "test send moss mail", body.Replace("{body}", " replace body is ok"));
// body.Replace:在Config list中定义了一个RICH TEXT做为MAIL的模板
logMessage("MOSS Time Job Execute---test ,email body:" + body.Replace("{body}", " replace body is ok"), EventLogEntryType.SuccessAudit);
}
catch (Exception e)
{
logMessage("MOSS Time Job Execute---Fail: " + e.Message, EventLogEntryType.Error);
throw new Exception("time job Error" + e.Message);
}
}
/// <summary>
/// Common Log Method
/// </summary>
/// <param name="message">string</param>
/// <param name="type">EventLogEntryType</param>
private void logMessage(string message, EventLogEntryType type)
{
if (!EventLog.SourceExists("Leave SharePoint Workflow"))
EventLog.CreateEventSource("Leave SharePoint Workflow", "Application");
EventLog.WriteEntry("Leave SharePoint Workflow", message, type);
}
}
}
在FeatureReceiver.cs文件如下:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;
using Microsoft.SharePoint.Utilities;
namespace TimeJob {
public class FeatureReceiver : SPFeatureReceiver
{
const string MY_Task = "TimeJobTest";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
try
{
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == MY_Task)
job.Delete();
//logMessage("Execute FeatureActivated's job cycle:" + job.Name, EventLogEntryType.Information);
}
}
catch (Exception e)
{
logMessage("Execute Error FeatureDeactivating's job :" +e.Message, EventLogEntryType.Error);
//SPUtility.SendEmail
throw new Exception("time job Error" + e.Message);
}
TimeJobTest timer = new TimeJobTest(MY_Task, webApp);
SPSchedule schedule = SPSchedule.FromString("daily at 16:46:00"); // executes at 3:25 pm local time
timer.Schedule = schedule;
timer.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == MY_Task)
{
job.Delete();
logMessage("Execute delete FeatureDeactivating's job :" + job.Name, EventLogEntryType.Information);
}
}
/* no op */
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
/* no op */
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
/* no op */
}
/// <summary>
/// Common Log Method
/// </summary>
/// <param name="message">string</param>
/// <param name="type">EventLogEntryType</param>
private void logMessage(string message, EventLogEntryType type)
{
if (!EventLog.SourceExists("MOSS Job"))
EventLog.CreateEventSource("MOSS Job", "Application");
EventLog.WriteEntry("MOSS Job", message, type);
}
}
}
最后点部署.
注意如果更改了代码,在次部署时,要设置成如图红线所指的选项"DebugRedeploy",不然会部署失败.
部署成功后我们进入MOSS的管理站点启动Feature
点启动后如下
到了JOB指定的运行时间后我们可以查看MAIL与日志文件看运行情况.也可以从MOSS上查看运行情况如下:
注意:如果修改了JOB程序再部署了,我们必须要把OWSTIME.EXE进程查杀,才可以生效.
在测试环境开发完成后.我们要到生成的方案文件来下打开DeploymentFiles文件来COPY生成的TimeJob.WSP文件到发布文件夹中,在发布文件中编写安装文件install.bat
echo.
echo Activating the feature...
echo.
pushd %programfiles%\common files\microsoft shared\web server extensions\12\bin
stsadm -o addsolution -filename TimeJob.wsp
stsadm -o deploysolution -name TimeJob.wsp -immediate -allowgacdeployment -force
stsadm.exe -o execadmsvcjobs '这一段是立即执行部署操作
net stop "Windows SharePoint Services Timer" '重启计时器服务,
net start "Windows SharePoint Services Timer"
iisreset
::在解决方案管理里部署你的解决方案
::在网站集功能里打开这个功能,就OK了。
::如果你要卸载Timer,就比较麻烦~~~
::首先需要在网站集功能里关闭功能,然后执行
::stsadm -o deletesolution -name ZTEsoft.Management.Employees.EventCheckTimer.wsp -override
我们要部署到正式SERVER上只要运行install.bat就可以了.到此介绍完毕.