Sharepoint学习笔记—习题系列--70-573习题解析 -(Q115-Q117)

2023-05-07,,

Question 115
You create a timer job.
You need to debug the timer job.
To which process should you attach the debugger?
A. devenv.exe
B. owstimer.exe
C. spucworkerprocess.exe
D. w3wp.exe

解析:
 这是关于调试的问题,参见Question56
在Sharepoint的开发环境中调试不同的对象有时需要Attach到不同的进程,下面就是一些常见的情况
   1、Farm Solution     -----W3WP.EXE
   2、SandBox Solution   -----PUCWorkerProcess.exe,( SharePoint 在一个独立于主 IIS 应用程序池 (w3wp.exe) 进程的进程 (SPUCWorkerProcess.exe) 中运行沙盒解决方案代码。所以你必须在SPUCWorkerProcess上进行调试)
   3、用到了Full-trust Proxy的SandBox Solution--SPUCWorkerProcessProxy.exe
   4、feature receivers----Feature Receiver默认情况下是自动被Visual Studio 启动。你可以在Visual Studio的部署设置中更改这种设置。
        4.1、feature receivers的 activation/deactivation :根据它被activation/deactivation 的方式,你需要Attaching到不同的进程。
             • 在web界面上启动或停止----W2WP.EXE
             • 在PowerShell中启动或停止---- PowerShell.exe
        4.2、feature receivers的 FeatureInstalled/FeatureUninstalling/FeatureUpgrading(安装/卸载/升级)----- owstimer.exe
 至于其它选项:
  选项A. devenv.exe是微软Microsoft Visual Studio的一部分,用于应用程序开发。存放在目录 C:\Windows\System32下, 这个进程在 Windows 启动时自动载入。
  选项B.C.D均可在上面找到对应描述。
所以本题目正确选项应该是B

参考:
http://msdn.microsoft.com/en-us/library/ff798310.aspx

Question 116
You need to create a timer job that queries a list.
What should you do?
A. Create a class that inherits SPJobDefinition and override the Execute method.
B. Create a class that inherits SPJobDefinition and override the Provision method.
C. Create a class that inherits SPServiceApplication and override the Provision method.
D. Create a class that inherits SPServiceApplication and override the ProvisionInstances method.

解析:
    本题考你如何创建一个Timer Job去查询一个列表。
  Microsoft SharePoint 2010 计时器作业执行维护您的 SharePoint 场所需的大部分后端作业。计时器作业是在计划的时间在一台或多台服务器上运行的可执行任务。可将计时器作业配置为正好运行一次,或按定期计划运行。
计时器作业类似于 Microsoft SQL Server 代理作业,这些代理作业可通过备份数据库、整理数据库文件碎片和更新数据库统计信息来维护 SQL Server 安装。SharePoint 使用计时器作业来维护长时间运行的工作流、清理旧的网站和日志以及监控服务器场以发现问题。 计时器作业有一些优势。它们可以定期并独立于正在访问您的 SharePoint 网站的用户运行,它们可以从您的 Web 前端服务器卸载长时间运行的进程,从而提高您页面的性能和响应能力,它们还可在比您的 SharePoint 网站和应用程序页中的代码更高的权限下运行代码。
   创建 SharePoint 计时器作业:包括随 SharePoint 安装的所有计时器作业均是通过 SPJobDefinition 类创建和执行的。要创建新的 SharePoint 计时器作业,首先必须向继承自 SPJobDefinition 类的项目添加一个类。
  以下代码段显示继承自 SPJobDefinition 类的一个类的示例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint; namespace MonitoringJob {
public class MonitoringJob : SPJobDefinition {
public MonitoringJob() : base() { } public MonitoringJob(string jobName, SPService service)
: base(jobName, service, null, SPJobLockType.None) {
this.Title = jobName;
} public override void Execute(Guid targetInstanceId) {
// Put your job's code here.
}
}
}

在该代码段中,MonitoringJob 类继承自 SPJobDefinition 类,定义一个非默认构造函数,并重写基类的 Execute 方法。非默认构造函数是必需的,因为 SPJobDefinition 类的默认构造函数仅供内部使用。
创建构造函数后,您必须重写 SPJobDefinition 类的 Execute 方法,并用您的作业所需的代码替换该方法中的代码。如果您的代码可以在没有其他配置的情况下运行,则您已完成创建此类。否则,您必须向您的项目添加配置屏幕和类。
  至此,本题的答案就已经出来了:即选项A。
 进一步看其它选项:
选项B. Create a class that inherits SPJobDefinition and override the Provision method.: 重写SPJobDefinition 类的Porvision方法,此方法是用来修改本地服务器以使相关对象能够被使用。
选项C.D 均是继承自SPServiceApplication类,此类是一个抽象类,表示在服务器上运行的服务应用程序。
所以本题目正确选项应该是A
参考:
http://tomaszrabinski.pl/wordpress/2010/05/27/spjobdefinition-as-the-way-to-create-scheduled-tasks/
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spserviceapplication.aspx

Question 117
You need to schedule a timer job to run every two hours.
Which schedule should you use?
A. SPDailySchedule
B. SPHourlySchedule
C. SPMinuteSchedule
D. SPOneTimeSchedule

解析:
 本题要设置一个定时器工作每2个小时运行一次,那么你需要设置哪个计划类?
 当我们创建了TimerJob后,就需要把它部署到我们的Sharepoint场里去。通常推荐的做法 是创建一个Reature Receiver,然后利用FeatureActivated事件实例化你的Job,设置它的执行计划以及更新这个Job。
  下面就是一段Feature Receiver示例代码:

Class SyncListsFeatureReceiver:SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
//Good Practice to delete the older version of Job if it exists
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name.ToLower() == myJobName.ToLower())
{
job.Delete();
}
}

//Install the job definition
 SyncListsJob tempJob = new SyncListsJob (myJobName,webApp);
 
 //如果你想每个小时运行一次作业,那么就使用SPHourlySchedule类

 //This one will run between 0 to 5 mintues of every hour
SPHourlySchedule schedule = new SPHourlySchedule();
schedule.BeginMinute = 0;
schedule.EndMinute = 5;
tempJob.Schedule = schedule;
tempJob.Update();
}
catch (Exception ex)
{
}
} public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
//delete the job
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name.ToLower() == myJobName.ToLower()
{
job.Delete();
}
}
}
catch (Exception ex)
{
}
}
}

所以如何计划作业的运行就取决于我们在上代码中使用什么类,下面列举了可用的类:
1. SPOneTimeSchedule :如果你想在指定时间运行且仅运行一次作业,则使用此类。
2. SPMinuteSchedule 如果你想每隔多少分钟运行一次作业,那么就使用此类
3. SPHourlySchedule 如果你想每个小时运行作业,则使用此类
4. SPDailySchedule 如果你想每天的某个时间(如:11:05AM)准时运行一次作业,则使用此类
5. SPWeeklySchedule :如果你想每周的哪一天的指定时间运行作业(如:每周三的2:01:00 am to 2:01:05 am)那么就使用此类。
6. SPMonthlySchedule:如果你想每个月的某一天指定时间运行作业(每个月的1号在21:15am 到 1:30am之间运行),那么就使用此类
7. SPYearlySchedule:如果你想在每一年的某一天的指定时间运行作业(如:每年的1月18号9:05AM),则使用此类。
  本题是想每2个小时运行一次,所以只有用SPMinuteSchedule类,因为此类有间隔属性,可以用来设置每间隔多少时间运行作业。为什么不能用SPHourlySchedule类呢?因为很明显,SPHourlySchedule类只能每个小时都运行,它没有间隔属性,无法设置每间隔多少时间运行作业。
所以本题目正确选项应该是C

参考:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spminuteschedule_members.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sphourlyschedule_members.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sphourlyschedule.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sponetimeschedule.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spdailyschedule.aspx
http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spminuteschedule_members.aspx

Sharepoint学习笔记习题系列--70-573习题解析 -(Q115-Q117)的相关教程结束。

《Sharepoint学习笔记—习题系列--70-573习题解析 -(Q115-Q117).doc》

下载本文的Word格式文档,以方便收藏与打印。