.netcore控制台->定时任务Quartz

  之前做数据同步时,用过timer、window服务,现在不用那么费事了,可以使用Quartz,并且配置灵活,使用cron表达式配置XML就可以。我用的是3.0.7版本支持.netcore。

  • 首先创建一个.netcore控制台应用程序,添加Quartz、Quartz.Jobs、Quartz.Plugins引用,这里面添加了PostgreSql数据库的连接方法,其它数据库可以做为参考,添加Npgsql、Npgsql.EntityFrameworkCore.PostgreSQL引用,目录结构如下
  • 创建数据库DBContext类
  • using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.EntityFrameworkCore;
    
    namespace QuartzPro.DbContext
    {
        public class PostgreDbContext : Microsoft.EntityFrameworkCore.DbContext
        {
            private string _conn;
            public PostgreDbContext(DbContextOptions<PostgreDbContext> options) : base(options)
            {
            }
            public PostgreDbContext(string conn)
            {
                _conn = conn;
            }
    
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(builder);
                //builder.Entity<syrequest_main>().ToTable("book", "pro");
            }
             
           // public virtual DbSet<book> book { get; set; }
    
        }
    }

     

  • 创建Job类
    • using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Text;
      using System.Threading.Tasks;
      using Microsoft.Extensions.Logging;
      using Quartz;
      
      namespace QuartzPro.Jobs
      {
        [DisallowConcurrentExecution]
      public class SyncUserInfoService : IJob { private readonly ILogger<SyncUserInfoService> _logger; public SyncUserInfoService() { _logger = BootStrapIoc.GetService<ILoggerFactory>().CreateLogger<SyncUserInfoService>(); } public Task Execute(IJobExecutionContext context) { _logger.LogDebug($"SyncUserInfoService Execute start..."); return Task.Run(() => { using (StreamWriter sw = new StreamWriter(@"D:\print.txt", true, Encoding.UTF8)) { sw.WriteLine(DateTime.Now + "任务执行中..."); sw.Close(); } }); } } }

       

  • 创建依赖注入类
  • using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using QuartzPro.DbContext;
    
    namespace QuartzPro
    {
        public static class BootStrapIoc
        {
            /// <summary>
            /// IOC容器
            /// </summary>
            private static IServiceCollection serviceCollection { get; } = new ServiceCollection();
    
            /// <summary>
            /// 初始化IOC容器
            /// </summary>
            public static void InitIoc()
            {
    
                var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables().Build();
    
                var identityConn = configuration.GetConnectionString("IdentityConnection");
                //db
                serviceCollection.AddTransient(_ => new  PostgreDbContext(identityConn));
                //log
                serviceCollection.AddLogging(configure =>
                {
                    configure.AddConfiguration(configuration.GetSection("Logging"));
                    configure.AddConsole();
                });
                //config
                serviceCollection.AddSingleton<IConfiguration>(configuration);
          
            }
    
            /// <summary>
            /// 获取实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static T GetService<T>()
            {
                return serviceCollection.BuildServiceProvider().GetService<T>();
            }
        }
    }
    

      

  • 创建任务监听XML文件
  • <?xml version="1.0" encoding="utf-8" ?>
    <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>
      
        <!--同步用户信息:10分钟一次-->
        <job>
          <name>SyncUserInfoService</name>
          <group>GroupUserInfoManager</group>
          <description>同步用户信息</description>
          <job-type>QuartzPro.Jobs.SyncUserInfoService, QuartzPro</job-type>
          <durable>true</durable>
          <recover>false</recover>
        </job>
        <trigger>
          <cron>
            <name>TriggerSyncUserInfoService</name>
            <group>GroupTriggerUserInfoManager</group>
            <job-name>SyncUserInfoService</job-name>
            <job-group>GroupUserInfoManager</job-group>
            <start-time>2019-07-30T15:15:00.0Z</start-time>
            <cron-expression>0 0/10 * * * ?</cron-expression>
            <!--<cron-expression>1 0 0 * * ?</cron-expression>-->
          </cron>
        </trigger>
      </schedule>
    </job-scheduling-data>

     

  • json配置文件
  • {
      "Logging": {
        "LogLevel": {
          "Default": "Debug",
          "System": "Information",
          "Microsoft": "Information"
        },
        "Console": {
          "IncludeScopes": true
        }
      },
      "ConnectionStrings": {
        "IdentityConnection": "User ID=admin;Password=123456;Host=.;Port=5432;Database=identities;"
      }
    }

     

  • Program类,注意该项目为控制台程序 UserInfoManager.xml和 appsettings.json,需要右键设置为可输出文件

  • using System; using System.Collections.Specialized; using System.Threading.Tasks; using Quartz; using Quartz.Impl; namespace QuartzPro { class Program { private static void Main(string[] args) { BootStrapIoc.InitIoc(); var task = Run(); task.Wait(); task.ConfigureAwait(false); while (true) { Console.Read(); } } public static async Task Run() { try { var properties = new NameValueCollection { ["quartz.plugin.triggHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin, Quartz.Plugins", ["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins", ["quartz.plugin.jobInitializer.fileNames"] = "UserInfoManager.xml", ["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true", ["quartz.plugin.jobInitializer.scanInterval"] = "120" }; ISchedulerFactory sf = new StdSchedulerFactory(properties); IScheduler scheduler = await sf.GetScheduler(); Console.WriteLine("start the schedule"); await scheduler.Start(); Console.WriteLine("end"); } catch (SchedulerException se) { await Console.Error.WriteLineAsync(se.ToString()); } catch (Exception ex) { Console.Write($"err={ex.ToString()}"); } } } } 
posted @ 2019-07-31 16:58  低调码农哥!  阅读(8952)  评论(0编辑  收藏  举报