Quartz.Net—基本操作

Quratz基本架构

 

 

 

Scheduler基本操作
/// <summary>
/// 调度器信息
/// </summary>
/// <returns></returns>
public async Task<SchedulerMetaData> GetSchedulerI
{
    var mate = await schedu.GetMetaData();
    var json = JsonConvert.SerializeObject(mate);
    return mate;
}

/// <summary>
/// 开始schedulequetzthread开始调度
/// </summary>
/// <returns></returns>
public async Task SchedulerStart()
{
    if (!schedu.IsStarted)
    {
        await schedu.Start();
    }
}

/// <summary>
/// 关闭,关机。并清理关联的资源。
/// </summary>
/// <returns></returns>
public async Task SchedulerShutdown()
{
    if (!schedu.IsShutdown)
    {
        await schedu.Shutdown();
    }
}

/// <summary>
/// 停止
/// </summary>
/// <returns></returns>
public async Task StopScheduler()
{
    if (schedu.IsStarted)
    {
        await schedu.Standby();
    }
}

/// <summary>
/// 重启
/// </summary>
/// <returns></returns>
public async Task ResumScheduler()
{
    if (schedu.InStandbyMode)
    {
        //Standby  靠边站的,
        await schedu.Start();
    }
}
View Code

 对大容器进行 暂停 重启。整个调度都会停止的。

Job增删查改
public async Task AddJob(string name, string group, string des, string dll, string typename)
{
    if (await schedu.CheckExists(new JobKey(name, group)))
    {

    }
    else
    {
        //先建立好一个job项目,然后只用创建一个实现了IJob接口的类库。在把这个类库添加到动态的添加到job项目中。
        var dllinstanck = Assembly.LoadFile(dll).CreateInstance(typename);
        IJobDetail job = JobBuilder.Create(dllinstanck.GetType()).WithIdentity(name, group).WithDescription(des).StoreDurably(true).Build();
        await schedu.AddJob(job, true);

    }
}

public async Task AddOrUpdateJob(string name, string group, string des, string dll, string typename)
{
    //StoreDurably  持久化存储
    //该工作在孤儿之后是否应该继续存储(没有触发器就是孤儿)
    var dllinstanck = Assembly.LoadFile(dll).CreateInstance(typename);
    IJobDetail job = JobBuilder.Create(dllinstanck.GetType()).WithIdentity(name, group).WithDescription(des).StoreDurably(true).Build();
    await schedu.AddJob(job, true);


    //var jobde = await schedu.GetJobDetail(new JobKey(name, group));
    //jobde.GetJobBuilder().OfType().WithDescription("sdfsdf");

}
public async Task DelJob(string name, string group)
{
    await schedu.DeleteJob(new JobKey(name, group));
}
View Code
public async Task<IList<IJobDetail>> GetJobs()
{
    List<IJobDetail> joblist = new List<IJobDetail>();
    var keys = await schedu.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
    foreach (var item in keys)
    {
        var job =await schedu.GetJobDetail(item);
        joblist.Add(job);
    }
    return joblist;
}

 

/// <summary>
/// 停止job就是停止job关联的trigger。
/// job是没有状态的。
/// 
/// 播放器 暂停 。在开始将 接着原来的。
/// 
/// </summary>
/// <param name="name"></param>
/// <param name="group"></param>
/// <returns></returns>
public async Task SchedulerPauseJob(string name, string group)
{
    await schedu.PauseJob(new JobKey(name, group));
}

/// <summary>
/// 恢复job 和 pause相对
/// </summary>
/// <param name="name"></param>
/// <param name="group"></param>
/// <returns></returns>
public async Task SchedulerResumeJob(string name, string group)
{
    await schedu.ResumeJob(new JobKey(name, group));
}
View Code

 

Trigger增删查改
public async Task AddTrigger(string jobName, string jobGroupName, string triggerName, string triggerGroupName, string cron, string des)
        {
            var exitTrigger = await schedu.CheckExists(new TriggerKey(triggerName, triggerGroupName));
            if (!exitTrigger)
            {
                if (await schedu.CheckExists(new JobKey(jobName, jobGroupName)))
                {
                    var trigger = TriggerBuilder.Create().WithIdentity(triggerName, triggerGroupName).WithDescription(des).WithCronSchedule(cron).ForJob(jobName, jobGroupName).Build();
                    await schedu.ScheduleJob(trigger);
                }
            }
        }

public async Task<IList<ITrigger>> GetTrrigers()
{
    List<ITrigger> triggerList = new List<ITrigger>();
    var triggerKeys = await schedu.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup());
    foreach (var item in triggerKeys)
    {
        var trigger = await schedu.GetTrigger(item);
        var job = await schedu.GetJobDetail(trigger.JobKey);
        var actionName = job.GetType().FullName;
        var nextTime = trigger.GetNextFireTimeUtc();
        var preTime = trigger.GetPreviousFireTimeUtc();
        var startTime = trigger.StartTimeUtc;
        triggerList.Add(trigger);
    }
    return triggerList;
}

public async Task UpdateTrigger(string triggerName, string triggerGroupName, string des)
{
    var trigger = await schedu.GetTrigger(new TriggerKey(triggerName, triggerGroupName));
    var triggerNew = TriggerBuilder.Create().WithIdentity(triggerName, triggerGroupName).WithDescription(des).ForJob(trigger.JobKey).Build();
    await schedu.RescheduleJob(trigger.Key, triggerNew);
}
View Code
public async Task PauseTrigger(string name, string group)
{
    await schedu.PauseTrigger(new TriggerKey(name, group));
}

public async Task ResumeTrigger(string name, string group)
{
    await schedu.ResumeTrigger(new TriggerKey(name, group));
}

public async Task DelTrigger(string name, string group)
{
    await schedu.UnscheduleJob(new TriggerKey(name, group));
}
View Code

 

Calendar增删查改
public async Task AddCalendar(string caName, string triggerKey, string value)
{
    var oldCal=await schedu.GetCalendar(caName);
    if (oldCal != null)
    {
        throw new Exception("");
    }
    else
    {
        CronCalendar cronCal = new CronCalendar(value);
         await schedu.AddCalendar(caName, cronCal,true,true);

       var oldTrigger=await  schedu.GetTrigger(new TriggerKey(triggerKey));
        if (oldTrigger != null)
        {
           var newTrigger=  oldTrigger.GetTriggerBuilder().ModifiedByCalendar(caName).Build();
          await   schedu.RescheduleJob(newTrigger.Key, newTrigger);
        }

    }
}

public async Task UpdateCalendar(string caName, string triggerKey, string value)
{
    var oldCal = await schedu.GetCalendar(caName);
    if (oldCal != null)
    {
        throw new Exception("");
    }
    else
    {
        CronCalendar cronCal = new CronCalendar(value);
        //                                       替换原来老的    自动更新触发器
        await schedu.AddCalendar(caName, cronCal,    true,               true); 
    }
}

public async Task DelCalendar(string caName, string triggerKey)
{
    //删除calendar之前 必须先接触trigger和calendar的关系。
    var trigger=await schedu.GetTrigger(new TriggerKey(triggerKey));
                  //一般这里要遍历所有的trigger,
    var newTrigger= trigger.GetTriggerBuilder().ModifiedByCalendar(null).Build();
    await schedu.RescheduleJob(newTrigger.Key, newTrigger);

  await   schedu.DeleteCalendar(caName);
}

public async Task<IList<ICalendar>> GetCalendars()
{
    IList<ICalendar> calList = new List<ICalendar>();
    var nameList = await schedu.GetCalendarNames();
    for (int i = 0; i < nameList.Count; i++)
    {
        var cal= await schedu.GetCalendar(nameList.ElementAt(i));
        calList.Add(cal);
    }
    return calList;
}
View Code

 

动态的排除一些时间

 

posted @ 2018-03-17 02:07  西伯利亚的狼  阅读(974)  评论(0编辑  收藏