内存中的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 的的步骤:

原文地址:http://www.cnblogs.com/easy5weikai/p/16886890.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性