zoukankan      html  css  js  c++  java
  • Asp.Net Core 中利用QuartzHostedService 实现 Quartz 注入依赖 (DI)

    QuartzHostedService  是一个用来在Asp.Net Core 中实现 Quartz 的任务注入依赖的nuget 包:

    基本示例如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using AspNetCoreSampleQuartzHostedService.Jobs;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Quartz;
    using QuartzHostedService;
    
    namespace AspNetCoreSampleQuartzHostedService
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                services.AddOptions();
                services.Configure<AppSettings>(Configuration);
                services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
                services.AddQuartzHostedService()
                        .AddQuartzJob<HelloJob>()
                        .AddQuartzJob<InjectSampleJob>()
                        .AddQuartzJob<HelloJobSingle>()
                        .AddQuartzJob<InjectSampleJobSingle>();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<AppSettings> settings)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                app.UseMvc();
                if (settings.Value.EnableHelloSingleJob)
                {
                    app.UseQuartzJob<HelloJobSingle>(TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()))
                    .UseQuartzJob<InjectSampleJobSingle>(() =>
                    {
                        return TriggerBuilder.Create()
                           .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever());
                    });
                }
                if (settings.Value.EnableHelloJob)
                {
                    app.UseQuartzJob<HelloJob>(new List<TriggerBuilder>
                    {
                        TriggerBuilder.Create()
                        .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()),
                        TriggerBuilder.Create()
                        .WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever())
                    });
    
                    app.UseQuartzJob<InjectSampleJob>(() =>
                    {
                        var result = new List<TriggerBuilder>();
                        result.Add(TriggerBuilder.Create()
                            .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()));
                        return result;
                    });
                }
            }
    
        }
    }

    扩展方式示例:

    public static class ModBusScheduler
        {
            public static void AddQuartzJobsService(this IServiceCollection services)
            {
                services.AddQuartzHostedService()
                        .AddQuartzJob<Slaver>("HAVC_Elev")
                        .AddQuartzJob<Slaver>("Lighting", "Lighting")
                        .AddQuartzJobDetail(() => JobBuilder.Create<Slaver>().WithIdentity("Meter").Build())
                        .AddQuartzJobDetail(() => JobBuilder.Create<Slaver>().WithIdentity("PowerDis").Build());
            }
    
            public static void UserQuartzJobsService(this IApplicationBuilder app, AppSettings settings)
            {
                app.UseQuartzJob<Slaver>("HAVC_Elev", () =>
                 {
                     return TriggerBuilder.Create()
                               .WithIdentity("HAVC_Elev")
                               .UsingJobData("modbus", settings.AC_ElevatorSlave)
                               .UsingJobData("devicetype", "HAVC_Elev")
                               .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever()).StartNow();
                 });
    } 
    

      

    nuget 连接  https://www.nuget.org/packages/QuartzHostedService/

    github地址  https://github.com/mukmyash/QuartzHostedService

  • 相关阅读:
    HTML
    初学网页
    刚学了函数,关于有无参数和返回值的四种情况的查找数字的代码
    输入五个学生的成绩,得到成绩表
    验证你的邮箱是不是qq邮箱
    摘自评论。
    LINQ Except “引用类型” 用法
    梦到钢笔
    MVC传参数给js的时候 如果是数值 变量要进行一下转换才能正确识别 例如var aaa = parseInt('@Model.ClickIndex');
    绝对路径相对路径
  • 原文地址:https://www.cnblogs.com/MysticBoy/p/11003030.html
Copyright © 2011-2022 走看看