zoukankan      html  css  js  c++  java
  • Create and Install Timer Job in MOSS 2007

    Excute Timerjob

     public class TriggerLoadCacheTimerJob : SPJobDefinition
        {
            string ExceptionFlag = string.Empty;
    
            public TriggerLoadCacheTimerJob()
                : base()
            {
            }
            // Overriding the parameterized constructor 
            public TriggerLoadCacheTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
                : base(jobName, service, server, targetType) { }
    
            public TriggerLoadCacheTimerJob(string jobName, SPWebApplication webApp)
                : base(jobName, webApp, null, SPJobLockType.Job)
            {
                // create the timer job with the name passed in Constructor and assign title
                this.Title = Constants.TIMERJOB_NAME;
            }
            public override void Execute(Guid targetInstanceId)
            {
                
                TriggerLoadCacheWebService();
            }
    }
    View Code
    private void TriggerLoadCacheWebService()
            {
                try
                {
                    SPWebApplication webApp = this.Parent as SPWebApplication;
                    Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApp.Name);
                    string CustomWebServiceURLs = config.AppSettings.Settings["CustomWebServiceUrls"].Value;
                    string AccountName = config.AppSettings.Settings["AccountName"].Value;
                    string Password = config.AppSettings.Settings["Password"].Value;
                    string Domain = config.AppSettings.Settings["Domain"].Value;
                    ExceptionFlag = config.AppSettings.Settings[Constants.LogExceptionFlag].Value;
                    string[] customWSURLs = CustomWebServiceURLs.Split(';');
                    for (int i = 0; i < customWSURLs.Length; i++)
                    {
                        AEnhancement.PSWebService mywebService = new AEnhancement.PSWebService();
                        mywebService.Url = customWSURLs[i].ToString();
                        NetworkCredential credential = new NetworkCredential(AccountName, Password, Domain);
                        //mywebService.Timeout=300000;
                        mywebService.Credentials = credential;
                        mywebService.LoadCacheAsync();
                    }
                }
                catch (Exception ex)
                {
                    if (ExceptionFlag.ToLower() == "true")
                    {
                        TraceLog.Error("Message:" + ex.Message + "Source:" + ex.Source + "TargetSite:" + Convert.ToString(ex.TargetSite), "TriggerLoadCacheWebService Method in Timer Job");
                    }
                }
            }
    View Code

    Timerjob inherited SPJobDefinition, it is based on webapplication(CA). You are able to debug timerjob via OWSTimer.exe.

    Install TimerJob-Feature Active

    class TimerJobFeatureReceiver : SPFeatureReceiver
        {
            string ExceptionFlag = string.Empty;
            string InfoFlag = string.Empty;
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                try
                {
                    TraceLog.Information("Activate Feature Start", "Activate Feature");
                    SPSite CurrentSite = properties.Feature.Parent as SPSite;
                    
                    if (CurrentSite == null)
                    {
                        TraceLog.Information("Current Site is NULL", "Activate Feature");
                        return;
                    }
                    else
                    {
                        TraceLog.Information("Current Site " + CurrentSite.Url, "Activate Feature");
                    }
                        
                    SPWebApplication webapp = CurrentSite.WebApplication;
                    
                    if (webapp == null)
                    {
                        TraceLog.Information("Web APP is NULL", "Activate Feature");
                        return;
                    }
                    else
                    {
                        TraceLog.Information("WebAPP "+webapp.Name, "Activate Feature");
                    }
                    // deletes the timer job if already exists
                    foreach (SPJobDefinition job in webapp.JobDefinitions)
                    {
                        if (job.Name == Constants.TIMERJOB_NAME)
                        {
                            job.Delete();
                            TraceLog.Information("Timer Job Delete Firstly", "Activate Feature");
                            break;
                        }
                    }
    
                    // install the job
                    SPSecurity.RunWithElevatedPrivileges(() =>
                    { 
                        TraceLog.Information("Install Timer Job Begin", "Activate Feature");
                        TriggerLoadCacheTimerJob customTimerjob = new TriggerLoadCacheTimerJob(Constants.TIMERJOB_NAME, webapp);
                        TraceLog.Information("111", "Activate Feature");
                        Configuration config = WebConfigurationManager.OpenWebConfiguration("/", CurrentSite.WebApplication.Name);
                        TraceLog.Information("config created", "Activate Feature");
                        string TimerJobSchedule = config.AppSettings.Settings["TimerJobSchedule"].Value;
                        TraceLog.Information(TimerJobSchedule, "Activate Feature");
                        ExceptionFlag = config.AppSettings.Settings[Constants.LogExceptionFlag].Value;
                        SPSchedule schedule = SPSchedule.FromString("daily at " + TimerJobSchedule);
    
                        //SPMinuteSchedule schedule = new SPMinuteSchedule();
                        //schedule.BeginSecond = 0;
                        //schedule.EndSecond = 59;
                        //schedule.Interval = 5;
    
                        customTimerjob.Schedule = schedule;
                        TraceLog.Information("Timer Job UPdate Method Start", "Activate Feature");
                        customTimerjob.Update();
                        TraceLog.Information("Timer Job UPdate Method End", "Activate Feature");
                        TraceLog.Information("Install Timer Job End", "Activate Feature");
                    });
                }
                catch (Exception ex)
                {
                    if (ExceptionFlag.ToLower() == "true")
                    {
                        TraceLog.Error("Message:" + ex.Message + "Source:" + ex.Source + "TargetSite:" + Convert.ToString(ex.TargetSite), "TriggerLoadCacheWebService Method in Timer Job");
                    }
                }
                
            }
    
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
    
                try
                {
                    SPSite CurrentSite = properties.Feature.Parent as SPSite;
                    if (CurrentSite == null)
                        return;
                    SPWebApplication webapp = CurrentSite.WebApplication;
                    if (webapp == null)
                        return;
                    // deletes the timer job if already exists
                    foreach (SPJobDefinition job in webapp.JobDefinitions)
                    {
                        if (job.Name == Constants.TIMERJOB_NAME)
                        {
                            job.Delete();
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ExceptionFlag.ToLower() == "true")
                    {
                        TraceLog.Error("Message:" + ex.Message + "Source:" + ex.Source + "TargetSite:" + Convert.ToString(ex.TargetSite), "TriggerLoadCacheWebService Method in Timer Job");
                    }
                }
            }
    }
    View Code

    Feature base on site

    Create Config file in bin Folder

    1. Create OWSTimer.EXE.Config file in bin folder C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12in
    2. Add these infomation in the file or you can put the file in bin folder.

      

    <configuration>
    
      <appSettings>
    
        <add key="AccountName" value="Jenny" />
        <add key="Password" value="c003#" />
        <add key="Domain" value="abc" />
        <add key="CustomWebServiceUrls" value="http://sp2007:11908/_vti_bin/AWebService/PSWebService.asmx;http://sp2007:11909/_vti_bin/AWebService/PSWebService.asmx" />
        <add key="Detail_PeopleSearch_Debug" value="True" />
        <add key="PeopleSearch_Exception" value="True" />
    
      </appSettings>
    
    </configuration>

    Explanation of Key, Value in Configuration files

    1. AccountName, Password and Domain keys

    Account should be the same as account which is used to run Windows SharePoint Services Timer service

    1. CustomWebServiceUrls key

    It means customwebServiceUrls which you will use in several WFE, you have to configure these several links which is split by " ; ".

    1. MaxRecords key

    It means how many records will get back when user keys in characters.

    1. TimerJobSchedule key

    It stands for when timer job runs

    1. Detail_PeopleSearch_Debug and PeopleSearch_Exception keys

    They are used to enable to note down log when they are set as “True”.

  • 相关阅读:
    C# 压缩组件介绍与入门
    《Effective C#》快速笔记(六)-
    原生JS实现简单富文本编辑器
    oracle 给表字段把VARCHAR2 换成 CLOB
    java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
    Idea中Module is not specified解决办法
    JS用正则替换特殊字符
    对List<Map>里的map的某个属性重复的值进行处理的方法
    ……
    使用原生js 实现点击消失效果
  • 原文地址:https://www.cnblogs.com/CrystalWind/p/4435373.html
Copyright © 2011-2022 走看看