首页 > 其他分享 >Abp vNext : 使用 BackgroundJobs

Abp vNext : 使用 BackgroundJobs

时间:2022-11-13 20:59:33浏览次数:72  
标签:vNext 后台作业 BackgroundJobs Volo Abp using public

内存中的BackgroundJobs

第一步:定义类 EmailSendingArgs,用于保存后台作业的数据

namespace AbpManual.BackgroundJobs
{
    /// <summary>
    /// 后台作业数据
    /// </summary>
    public class EmailSendingArgs
    {
        public string EmailAddress { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
    }

}

第二步:定义后台作业类 EmailSendingJob

using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Emailing;

namespace AbpManual.BackgroundJobs
{
    /// <summary>
    /// https://docs.abp.io/zh-Hans/abp/latest/Background-Jobs
    /// 后台作业是一个实现IBackgroundJob<TArgs>接口或继承自BackgroundJob<TArgs>类的类
    /// </summary>
    public class EmailSendingJob : BackgroundJob<EmailSendingArgs>, ITransientDependency
    {
        private readonly IEmailSender _emailSender;
        private readonly ILogger<EmailSendingJob> _logger;

        public EmailSendingJob(
            IEmailSender emailSender,
            ILogger<EmailSendingJob> logger)
        {
            _emailSender = emailSender;
            _logger = logger;
        }

        public override  void Execute(EmailSendingArgs args)
        {
            // 模拟随机产生异常,看看后台作业是否因失败再次执行
            if (RandomHelper.GetRandom(0, 10) < 5)
            {
                throw new ApplicationException("A sample exception from the EmailSendingJob!");
            }

            _logger.LogInformation($"############### EmailSendingJob: 邮件已成功发送至邮箱:{args.EmailAddress} ###############");

            // 发送邮件
            // await _emailSender.SendAsync(
            //    args.EmailAddress,
            //    args.Subject,
            //    args.Body
            //);
        }
    }
}

第三步:使用 IBackgroundJobManager 发布后台作业

using AbpManual.BackgroundJobs;
using Volo.Abp.Application.Services;
using Volo.Abp.BackgroundJobs;

namespace AbpManaul.BackgroundJobs
{
    public class BackgroundJobService : ApplicationService, IBackgroundJobService
    {
        /// <summary>
        /// 默认后台作业管理器
        /// https://docs.abp.io/zh-Hans/abp/latest/Background-Jobs
        /// ABP framework 包含一个简单的 IBackgroundJobManager 实现;
        /// </summary>
        private readonly IBackgroundJobManager _backgroundJobManager;

        public BackgroundJobService(IBackgroundJobManager backgroundJobManager)
        {
            _backgroundJobManager = backgroundJobManager;
        }

        public async Task RegisterAsync(RegisterInput input)
        {
            //TODO: 创建一个新用户到数据库中...

            await _backgroundJobManager.EnqueueAsync(
                new EmailSendingArgs
                {
                    EmailAddress = input.EmailAddress,
                    Subject = "You've successfully registered!",
                    Body = "恭喜你注册成功!"
                }
            );
        }
    }
}

至此,就完成了了后台作业的全部过程,并且,如果此次作业执行失败,BackgroundJob 的会进行重试,但是执行失败的任务是保存在内存中

BackgroundJobs Moudle

上一节,BackgroundJobs 的执行失败的作业是存储内存中, 虽然也可以实现 BackgroundJob 的重试,但是毕竟是保存在内存中,如果重启了进程,内存中的任务就会丢失
,而 Abp vNext 社区版的 BackgroundJobs Moudle, 参见:后台作业模块,已经实现了将 BackgroundJob 存储到数据库中.
只需要集成到现有的项目中即可。下面是已入BackgroundJobs Moudle 的的步骤:

标签:vNext,后台作业,BackgroundJobs,Volo,Abp,using,public
From: https://www.cnblogs.com/easy5weikai/p/16886890.html

相关文章