.net core .net5 asp.net core mvc 与quartz.net 3.3.3 新版本调用方式
参照了:https://www.cnblogs.com/LaoPaoEr/p/15129899.html
1.项目Nuget引用Quartz.AspNetCore和Quartz.Extensions.DependencyInjection,3.3.3 及以上版本。
2.新建一个MyTestJob类:
using Quartz;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks; namespace AspNetCoreQuartz333.QuartzJob
{
[DisallowConcurrentExecution]
public class MyTestJob : IJob
{
public MyTestJob()
{
//构造函数里可注入
} public Task Execute(IJobExecutionContext context)
{
return Task.Run(() =>
{
using (StreamWriter sw = new StreamWriter(@"d:/Quartz-NET.txt", true, Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff =>") + "1");
}
});
}
}
}
3.修改Startup-ConfigureServices方法,加入以下内容:
string thisJob = "TestJob";
string groupName = "gp" + thisJob;
string jobName = "job" + thisJob;
string triggerName = "trigger" + thisJob;
//Quartz的工作单元
services.AddTransient<MyTestJob>();
//Quartz调度中心
services.AddQuartz(q =>
{
//用于注入
q.UseMicrosoftDependencyInjectionJobFactory();
// 基本Quartz调度器、作业和触发器配置
var jobKey = new JobKey(jobName, groupName);
q.AddJob<MyTestJob>(jobKey, j => j
.WithDescription("My work")
);
q.AddTrigger(t => t
.WithIdentity(triggerName)
.ForJob(jobKey)
.StartNow()
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10))//开始秒数 10s
.RepeatForever())//持续工作
.WithDescription("My work trigger")
);
});
// ASP.NET核心托管-添加Quartz服务器
services.AddQuartzServer(options =>
{
// 关闭时,我们希望作业正常完成
options.WaitForJobsToComplete = false;
});
Startup完整代码:
using AspNetCoreQuartz333.QuartzJob;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace AspNetCoreQuartz333
{
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.AddControllersWithViews(); //避免ConfigureServices方法过长,Quartz相关配置提取到了单独方法
QuartzConfigureServices(services);
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
} /// <summary>
/// 避免ConfigureServices方法过长,Quartz相关配置提取到了单独方法
/// </summary>
/// <param name="services"></param>
public void QuartzConfigureServices(IServiceCollection services)
{
#region Quartz string thisJob = "TestJob"; string groupName = "gp" + thisJob;
string jobName = "job" + thisJob;
string triggerName = "trigger" + thisJob; //Quartz的工作单元
services.AddTransient<MyTestJob>();
//Quartz调度中心
services.AddQuartz(q =>
{
//用于注入
q.UseMicrosoftDependencyInjectionJobFactory();
// 基本Quartz调度器、作业和触发器配置
var jobKey = new JobKey(jobName, groupName);
q.AddJob<MyTestJob>(jobKey, j => j
.WithDescription("My work")
);
q.AddTrigger(t => t
.WithIdentity(triggerName)
.ForJob(jobKey)
.StartNow()
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10))//开始秒数 10s
.RepeatForever())//持续工作
.WithDescription("My work trigger")
);
}); // ASP.NET核心托管-添加Quartz服务器
services.AddQuartzServer(options =>
{
// 关闭时,我们希望作业正常完成
options.WaitForJobsToComplete = false;
}); #endregion
} }
}
END
.net core .net5 asp.net core mvc 与quartz.net 3.3.3 新版本调用方式的更多相关文章
- ASP.NET Core 用户注册 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 用户注册 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 用户注册 上一章节我们终于迁移完了 Identity 的数据,也创建 ...
- ASP.NET Core 视图 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 视图 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 视图 花了几章节,终于把 ASP.NET Core MVC 中的 C 控 ...
- ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 路由 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 路由 前两章节中,我们提到 ASP.NET Core 支持 MVC 开发 ...
- ASP.NET Core 简介 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 简介 - ASP.NET Core 基础教程 - 简单教程,简单编程 ← ASP.NET Core 基础教程 ASP.NET Core Windows 环境配置 → A ...
- ASP.NET Core 中间件 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 中间件 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 中间件 上一章节中,我们我们有讲到 Startup 类中的 Confi ...
- .NET平台系列16 .NET5/Asp.Net Core 在全球Web框架权威性能测试 Web Framework Benchmarks 中的吊炸天表现
系列目录 [已更新最新开发文章,点击查看详细] TechEmpower Web Framework Benchmarks 是许多Web应用程序框架执行基本任务(如JSON序列化.数据库访问和服 ...
- 【.NET Core】ASP.NET Core之IdentityServer4(1):快速入门
[.NET Core]ASP.NET Core之IdentityServer4 本文中的IdentityServer4基于上节的jenkins 进行docker自动化部署. 使用了MariaDB,EF ...
- 【ASP.NET Core】ASP.NET Core 依赖注入
一.什么是依赖注入(Denpendency Injection) 这也是个老身常谈的问题,到底依赖注入是什么? 为什么要用它? 初学者特别容易对控制反转IOC(Iversion of Control) ...
- 【ASP.NET Core 】ASP.NET Core 源码学习之 Logging[1]:Introduction
在ASP.NET 4.X中,我们通常使用 log4net, NLog 等来记录日志,但是当我们引用的一些第三方类库使用不同的日志框架时,就比较混乱了.而在 ASP.Net Core 中内置了日志系统, ...
- 【ASP.NET Core】ASP.NET Core API 版本控制
几天前,我和我的朋友们使用 ASP.NET Core 开发了一个API ,使用的是GET方式,将一些数据返回到客户端 APP.我们在前端进行了分页,意味着我们将所有数据发送给客户端,然后进行一些dat ...
随机推荐
- 技术门槛高?来看 Intel 机密计算技术在龙蜥社区的实践 | 龙蜥技术
简介: 数据可用不可见是怎么做到的? 编者按:龙蜥社区云原生机密计算 SIG 定位于云原生机密计算底层基础设施,专注于机密计算底层技术.在阿里巴巴开源开放周中, 龙蜥社区机密计算 SIG Mainta ...
- Datastream 开发打包问题
简介:Datastream作业开发时往往会遇到一些jar包冲突等问题,本文主要讲解作业开发时需要引入哪些依赖以及哪些需要被打包进作业的jar中,从而避免不必要的依赖被打入了作业jar中以及可能产生的 ...
- 【实践案例】Databricks 数据洞察在美的暖通与楼宇的应用实践
简介: 获取更详细的 Databricks 数据洞察相关信息,可至产品详情页查看:https://www.aliyun.com/product/bigdata/spark 作者 美的暖通与楼宇事业部 ...
- [FAQ] MySQL Workbench 设置界面字体大小
MySQL Workbench 连接数据库后,点击右上角的图标,进入 Fonts & Colors 设置. 示例: Tool:SQL美化工具 Ref:MySQLWorkbench Link ...
- [Contract] 一次搞懂 ETH Gas, GasPrice, GasLimit 之间关系与使用
Gas 是一个交易费的基本单位(unit),类似汽油的单位"升",比如一次链上交易可能消耗 5 Gas 汽油,由运行的程序决定. GasPrice 是一个 Gas 的单价,一般用 ...
- git fatal detected dubious ownership in repository 的解决方法
我换了一台电脑,将旧电脑的硬盘换到新电脑上:我装了双系统,切换到另一个系统时:我发现了 git 代码仓库无法执行 git 命令,不断报错 fatal: detected dubious ownersh ...
- dotnet 世界猜测 随机数的小测试
这是一个半技术向的博客,主题来源于我读过的某本书的片段,这是一个稍稍有些前置知识的故事,主题的大概内容就是假定世界存在某个规则序列,通过一代代的探索,可以获取到此序列的内容.本文将模拟此情形,写一个随 ...
- JS代码优化小技巧
下面介绍一种JS代码优化的一个小技巧,通过动态加载引入js外部文件来提高网页加载速度 [基本优化] 将所有需要的<script>标签都放在</body>之前,确保脚本执行之前完 ...
- LVGL 日志
一.启动日志 在 lv_conf.h 中将 LV_USE_LOG 设置为 1,如下图所示: 二.日志级别 在文件 lvgl/src/misc/lv_log.h 中定义了日志等级,等级是从小到大,所以 ...
- VGA显示文字
VGA显示文字 VGA字符显示的原理 把要显示的字符转换成字符点阵,然后编码存储,着色的部分为1,其它为0.然后在VGA上输出显示. 字符点阵生成软件: https://www.zhetao.com/ ...