Quartz_TimeJob例子(C#)

时间:2023-03-10 04:39:33
Quartz_TimeJob例子(C#)

执行入口:

using System;
using System.Collections.Generic;
using log4net;
using Quartz;
using ypt_base.Common.Model;
using ypt_base.Common.Util.AD;
using ypt_base.Urms.BLL;
using ypt_base.Urms.Model.Res; namespace Quartz_TimerJob.TimeDevice
{
/// <summary>
/// 江苏YPT组织机构信息同步任务
/// </summary>
[DisallowConcurrentExecution] //不允许此 Job 并发执行任务(禁止新开线程执行)
public class SyncAdOrgUnitJob : IJob
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SyncAdOrgUnitJob)); public void Execute(IJobExecutionContext context)
{
try
{
Log.InfoFormat("从AD域同步组织机构信息开始:{0}", DateTime.Now); // 获取AD域组织信息
List<ADModel> adList = new AdHelper().SyncOrgUnit(); // 获取组织维度信息
DictionaryManagerBLL dictBll = new DictionaryManagerBLL();
IList<UrmsDictionary> dimensionList = dictBll.GetDictionaryItemByType("orgDimension"); bool flag = false;
MetaOrgBLL orgBll = new MetaOrgBLL();
if (adList != null && adList.Count > )
{
flag = orgBll.SaveOrgUnitAndOrgUser(adList, dimensionList);
}
else
{
Log.WarnFormat("AD域中不存在所要获取的组织机构信息!");
} Log.InfoFormat(flag ? "从AD域同步组织机构信息成功!" : "从AD域同步组织机构信息失败!");
} catch (Exception ex)
{
Log.Error("从AD域同步组织机构信息异常", ex);
}
}
}
}

配置文件:

?xml version="1.0" encoding="UTF-8"?>

<!-- This file contains job definitions in schema version 2.0 format -->

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">

  <processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
</processing-directives> <schedule>
<!--江苏YPT同步组织机构任务配置-->
<job>
<name>SyncAdOrgUnitJob</name>
<group>SyncAdOrgUnitGroup</group>
<description>SyncAdOrgUnit job for Quartz Server</description>
<job-type>Quartz_TimerJob.TimeDevice.SyncAdOrgUnitJob, Quartz_TimerJob</job-type>
<durable>true</durable>
<recover>false</recover>
</job> <trigger>
<cron>
<name>SyncAdOrgUnitTrigger</name>
<group>SyncAdOrgUnitGroup</group>
<job-name>SyncAdOrgUnitJob</job-name>
<job-group>SyncAdOrgUnitGroup</job-group>
<cron-expression>0 23 15 * * ?</cron-expression>
<!--[秒] [分] [小时] [日] [月] [周] [年]-->
</cron>
</trigger> </schedule>
</job-scheduling-data>