.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(); } }); } } }
-
- 创建依赖注入类
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
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,需要右键设置为可输出文件
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
<br>
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()}"
);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律