在AbpVnext中简单使用原生的Quartz.net


1:一个静态帮助类

复制代码
using Quartz;
using Quartz.Impl;
using Quartz.Core;
using System;
using System.Threading.Tasks;
namespace GDBS.LogService.HttpApi.Hosting
{
    public class QuartzHelper
    {
        public static async Task DoWork<T>(int Seconds = 10, int RepeatCount = int.MaxValue, string cronExpressionStr = null) where T : IJob, new()
        {
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            IScheduler scheduler = schedulerFactory.GetScheduler().Result;
            IJobDetail jobDetail = JobBuilder.Create<T>().Build();
            TriggerBuilder trigger = TriggerBuilder.Create();
            if (cronExpressionStr == null)
            {
                trigger.WithSimpleSchedule(c => c.WithIntervalInSeconds(Seconds).WithMisfireHandlingInstructionNextWithRemainingCount().WithRepeatCount(RepeatCount));
            }
            else
            {
                trigger.WithCronSchedule(cronExpressionStr);
            }
            var builderTrigger = trigger.Build();

            await scheduler.ScheduleJob(jobDetail, builderTrigger);
            await scheduler.Start();
        }
    }
}
复制代码

2:在AbpVnext中使用,实现接口,继承IJob

复制代码
    /// <summary>
    /// ********************
    /// </summary>
    [DisallowConcurrentExecution]
    public class QQ爱Quartz03Services : IQQ爱Quartz03Services, ITransientDependency, IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                await GetProvinQQ爱TypeInfoEveryCity();
                await QQ爱RedisHelper.GetRedisDB(5).ListRightPushAsync(LogServiceQuartzConsts.Log_quartzCountInfo, JsonConvert.SerializeObject(new QuartzLogInfoDto { addtime = DateTime.Now, quartz_type = 3, quartz_name = "测试Quartz03Services abc" }));
            }
            catch (Exception)
            {
            }
        }
    }
复制代码

3:加入进来

复制代码
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseCors();
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("v1/swagger.json", "MainteReinfor Api");
                options.DocumentTitle = "***相关服务Swagger LMZ";
            });
            app.UseAuthentication();
            app.UseMiddleware<CurrentSysAuthenticationMiddleware>();
            app.UseAuthorization();
            app.UseMiddleware<CustomUserClaimMiddleware>();
            app.UseConfiguredEndpoints();

           //==========在abp尾部新增如下code即可
            Task.Factory.StartNew(() =>
            {
                var lodata = LmzQuartzHelper.DoWork<AbcService>(CronString: "0 15 3,12 * * ? *").Id;
                var aa = LmzQuartzHelper.DoWork<LogRedisQuartzService>(Seconds: 10).Id;          
                var disdata = LmzQuartzHelper.DoWork<QuartzLogsCheckDisDataService>(CronString: "0 0 1/1 * * ? *").Id;
            });
        }
复制代码

4:个人建议项目发布在Linux Docker环境中来使用,IIS存在定时回收的机制,
生产环境中妥妥的运行中,下面为简单的测试消费记录日志

 

posted @   天天向上518  阅读(272)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示