Autofac.Extras.Quartz目前无法使用Quartz属性,有解决方案吗?
创始人
2024-09-23 01:30:27
0

目前不支持直接在使用 Autofac.Extras.Quartz 的项目中配置 Quartz 属性。可以通过自定义 Quartz 属性注入器实现。

示例代码:

1.自定义属性注入器

public class AutofacJobFactory : IJobFactory { private readonly IContainer _container;

public AutofacJobFactory(IContainer container)
{
    _container = container;
}

public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
    var jobDetail = bundle.JobDetail;
    var jobType = jobDetail.JobType;
    try
    {
        return _container.Resolve(jobType) as IJob;
    }
    catch(Exception ex)
    {
        throw new SchedulerException($"Problem while instantiating job '{jobDetail.Key}' from the AutofacJobFactory.", ex);
    }
}

public void ReturnJob(IJob job)
{
}

}

2.注册AutofacJobFactory

// 注册 AutofacJobFactory builder.Register(c => new AutofacJobFactory(c.Resolve())).As().InstancePerLifetimeScope();

3.注册作业

// This uses the Quartz.Server assembly to find jobs via attributes. JobInitializer.InitializeJobs(typeof(MyJob).Assembly);

4.作业初始化

public static class JobInitializer { public static void InitializeJobs(Assembly assembly) { var jobs = from type in assembly.GetTypesWithAttribute() select new { Type = type, Attribute = type.GetCustomAttribute() } into job orderby job.Attribute.Order select job;

    foreach (var jobData in jobs)
    {
        var jobDetail = CreateJobDetail(jobData.Type, jobData.Attribute);
        var trigger = CreateTrigger(jobData.Attribute);
        var jobSchedule = new JobSchedule(jobDetail, trigger);

        // 注册 Autofac 流程
        var builder = new ContainerBuilder();
        builder.Register(c => jobDetail.JobType).InstancePerDependency();
        builder.RegisterType().As().InstancePerDependency();
        builder.RegisterType(jobData.Type).AsSelf().InstancePerDependency();
        builder.Update(ContainerProvider.Container);

        // 安排作业
        var scheduler = ContainerProvider.Container.Resolve();
        scheduler.ScheduleJob(jobDetail, trigger);
    }
}

private static IJobDetail CreateJobDetail(Type jobType, JobAttribute attribute)
{
    var jobDetail = JobBuilder
        .Create(jobType)
        .WithIdentity(Guid.NewGuid().ToString())
        .WithDescription(attribute.Description)
        .Build();

    if (!string.IsNullOrWhiteSpace(attribute.Group))
        jobDetail.Key = new JobKey(jobDetail.Key.Name, attribute.Group);

    return jobDetail;
}

private static ITrigger CreateTrigger(JobAttribute attribute)
{
    return TriggerBuilder
        .Create()
        .WithIdentity(attribute.TriggerName)
        .WithDescription(attribute.TriggerDescription)
        .WithCronSchedule(attribute.CronExpression)
        .Build();
}

}

相关内容

热门资讯

据权威媒体报道!hhpoker... 据权威媒体报道!hhpoker辅助靠谱吗,德扑之星私人局辅助器,原来存在有挂(有挂解惑)1、起透看视...
2026版技巧!wepoker... 2026版技巧!wepoker软件辅助程序,德普之星透视辅助插件,都是是有挂(有挂解惑)1、德普之星...
有了最新消息!hhpoker德... 有了最新消息!hhpoker德州真的假的,德扑之星透视免费,原来存在有挂(有挂神器)1、进入到德扑之...
黑科技教程!德州透视是真的假的... 您好,德扑之星辅助器app这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
据公告内容!we poker辅... 据公告内容!we poker辅助器v3.3,德普辅助软件,真是是真的有挂(有挂方式)1、下载好德普辅...
攻略辅助挂!哈糖大菠萝怎么开挂... 攻略辅助挂!哈糖大菠萝怎么开挂,德扑软件,确实确实有挂(有挂规律)1、进入游戏-大厅左侧-新手福利-...
随着!hhpoker辅助挂是真... 随着!hhpoker辅助挂是真的吗,智星菠萝有挂吗,果然真的是有挂(有挂方略)亲,关键说明,智星菠萝...
近期!wepoker私人局有透... 近期!wepoker私人局有透视吗,德普之星透视辅助软件下载,其实真的有挂(有挂规律)一、德普之星透...
近期!hhpoker是真的假的... 近期!hhpoker是真的假的,德扑之心免费透视,好像是有挂(确实有挂)1)德扑之心免费透视有没有挂...
方法辅助挂!hhpoker的辅... 方法辅助挂!hhpoker的辅助是真的吗,德普之星辅助工具如何设置,好像存在有挂(证实有挂)暗藏猫腻...