首页 > 其他分享 >net core-Scheduling Background Jobs With Quartz

net core-Scheduling Background Jobs With Quartz

时间:2023-06-06 22:35:30浏览次数:36  
标签:core Quartz Jobs configure publisher dbContext ProcessOutboxMessagesJob jobKey

一 安装包

Install-Package Quartz.Extensions.Hosting

二 注入依赖关系

services.AddQuartz(configure =>
{
    configure.UseMicrosoftDependencyInjectionJobFactory();
});

services.AddQuartzHostedService(options =>
{
    options.WaitForJobsToComplete = true;
});

三 添加Job任务

[DisallowConcurrentExecution]
public class ProcessOutboxMessagesJob : IJob
{
    private readonly ApplicationDbContext _dbContext;
    private readonly IPublisher _publisher;

    public ProcessOutboxMessagesJob(
        ApplicationDbContext dbContext,
        IPublisher publisher)
    {
        _dbContext = dbContext;
        _publisher = publisher;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        List<OutboxMessage> messages = await _dbContext
            .Set<OutboxMessage>()
            .Where(m => m.ProcessedOnUtc == null)
            .Take(20)
            .ToListAsync(context.CancellationToken);

        foreach (OutboxMessage outboxMessage in messages)
        {
            IDomainEvent? domainEvent = JsonConvert
                .DeserializeObject<IDomainEvent>(
                    outboxMessage.Content,
                    new JsonSerializerSettings
                    {
                        TypeNameHandling = TypeNameHandling.All
                    });

            if (domainEvent is null)
            {
                continue;
            }

            await _publisher.Publish(domainEvent, context.CancellationToken);

            outboxMessage.ProcessedOnUtc = DateTime.UtcNow;

            await _dbContext.SaveChangesAsync();
        }
    }
}

四 Services中添加任务。

services.AddQuartz(configure =>
{
    var jobKey = new JobKey(nameof(ProcessOutboxMessagesJob));

    configure
        .AddJob<ProcessOutboxMessagesJob>(jobKey)
        .AddTrigger(
            trigger => trigger.ForJob(jobKey).WithSimpleSchedule(
                schedule => schedule.WithIntervalInSeconds(10).RepeatForever()));

    configure.UseMicrosoftDependencyInjectionJobFactory();
});

 

标签:core,Quartz,Jobs,configure,publisher,dbContext,ProcessOutboxMessagesJob,jobKey
From: https://www.cnblogs.com/Insist-Y/p/17461923.html

相关文章

  • mysql使用efcore实现乐观并发控制
    为了避免多个用户同时操作同一个资源造成的并发冲突问题,通常需要进行并发控制。并发控制分为:乐观和悲观两策略悲观:悲观并发控制一般采用行锁、表锁等排它销对资源进行锁定,确保一个时间点只有一个用户在操作被锁定的资源。 悲观并发控件的使用比较简单,仅对要进行并发控制的资......
  • Taurus.mvc .Net Core 微服务开源框架发布V3.1.7:让分布式应用更高效。
    前言:自首个带微服务版本的框架发布:Taurus.MVCV3.0.3微服务开源框架发布:让.NET架构在大并发的演进过程更简单已经过去快1年了,在这近一年的时间里,版本经历了N个版本的迭代。如今,是时候写文章介绍一下了:以下介绍中,仅以.NetCore6为示例代码。框架支持在.NetFramework2.0+......
  • Asp.Net Core 程序部署到Linux(centos)生产环境(二):docker部署
    运行环境照例,先亮环境;软件的话我这里假设你已经批准好了.netcore运行环境,未配置可以看我的这篇[linux(centos)搭建.netcore运行环境]腾讯云centos:7.2cpu:1核2G内存1M带宽docker:18.06.1-ce安装docker安装其实很简单,就不重复说了,请查看纯洁大神这篇文章准备你的.netcoreweb程......
  • NET Core CLI命令小结
    dotnetrundotnetrun--urls=http://*:8080//urls指定ip和端口dotnetwatchrun//watch监听程序修改,一旦修改则重新启动dotnetwatchrun--urls=http://*:8080dotnetrun-pNETCoreCLI.csproj//一个“-”是简称的pdotnetrun--projectNETCoreCLI.csproj//两个“-”是全......
  • net core+mediatr+EF实现事件触发
    参考杨中科的教程1.先添加接口usingMediatR;namespaceNetOptions.Entities;publicinterfaceIDomainEnvent{voidAddNotification(INotificationnotification);IEnumerable<INotification>GetNotifications();voidClearNotifications();}2.添加抽......
  • 手把手教你AspNetCore WebApi:Swagger(Api文档)
    前言小明已经实现“待办事项”的增删改查,并美滋滋向负责前端的小红介绍Api接口,小红很忙,暂时没有时间听小明介绍,希望小明能给个Api文档。对于码农小明来说能不写文档就尽量不要写,不过这也难不倒小明,他知道Swagger不仅可以自动生成Api文档,并还可以用Swagger进行接口测试。Swagger是什......
  • 手把手教你AspNetCore WebApi:数据验证
    前言小明最近又遇到麻烦了,小红希望对接接口传送的数据进行验证,既然是小红要求,那小明说什么都得满足呀,这还不简单嘛。传统验证[HttpPost]publicasyncTask<ActionResult<Todo>>PostTodo(Todotodo){if(string.IsNullOrEmpty(todo.Name)){returnOk("名称不......
  • 手把手教你AspNetCore WebApi:Serilog(日志)
    前言小明目前已经把“待办事项”功能实现了,API文档也搞定了,但是马老板说过,绝对不能让没有任何监控的项目上线的。Serilog是什么?在.NET使用日志框架第一时间会想到NLog或是Log4Net,Serilog是这几年快速崛起的Log框架之一,Serilog是以Structuredlogging为基础进行设计,透过loggingAP......
  • 手把手教你AspNetCore WebApi:缓存(MemoryCache和Redis)
    前言这几天小明又有烦恼了,系统上线一段时间后,系统性能出现了问题,马老板很生气,叫小明一定要解决这个问题。性能问题一般用什么来解决呢?小明第一时间想到了缓存。什么是缓存缓存是实际工作中非常常用的一种提高性能的方法。缓存可以减少生成内容所需的工作,从而显著提高应用程序的性能......
  • ASP.NET Core MVC 项目在IIS中部署
    一、vs中把MVC项目以文件系统发布、设置保存的路径二、安装.NETCore3.1Runtime网址:https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-3.1.5-windows-x64-installer 三、安装AspNetCoreModule网址:https://dotnet.microsoft.com/download/dotne......