C Quartz 任务调度

霄
2022-05-30 / 0 评论 / 44 阅读 / 正在检测是否收录...

要实现任务调度的类继承父类

/// <summary>
/// 任务调度基类
/// </summary>
[DisallowConcurrentExecution()]
public abstract class JobBase : IJob
{
    #region IJob 成员

    public Task Execute(IJobExecutionContext context)
    {
        try
        {
            ExcuteJob(context);
            return Task.CompletedTask; ;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion
    /// <summary>
    /// 执行计划,子类可以重写
    /// </summary>
    public virtual string Cron => 0/1 * * * * ?;
    /// <summary>
    /// Job具体类去实现自己的逻辑
    /// </summary>
    protected abstract void ExcuteJob(IJobExecutionContext context);
}

任务调度工具类

public class QuartzManager
{
public static Action<Type> JoinToQuartz = (type) =>
{
StdSchedulerFactory.GetDefaultScheduler().Result.Start();
var obj = Activator.CreateInstance(type);
string cron = type.GetProperty(Cron).GetValue(obj).ToString();
var jobDetail = JobBuilder.Create(type)
.WithIdentity(type.Name)
.Build();
        var jobTrigger = TriggerBuilder.Create()
                                       .WithIdentity(type.Name + Trigger)
                                       .StartNow()
                                       .WithCronSchedule(cron)
                                       .Build();

        StdSchedulerFactory.GetDefaultScheduler().Result.ScheduleJob(jobDetail, jobTrigger);

    };
}
扫描二维码,在手机上阅读!
22

评论

博主关闭了当前页面的评论