我的NopCommerce之旅(4): 定时任务之邮件
2024-08-26 01:30:03
一、功能简介
用户购买物品生成订单后,系统将发送邮件提醒给用户
二、操作步骤
- 后台配置一个系统的默认发送邮箱
- 启动定时任务,这里包括多个任务,只需要启动邮件任务
- 查看邮件发送情况
三、数据库分析
- [dbo].[Log] 系统日志表,可查看邮件发送失败的异常信息
- [dbo].[EmailAccount] 系统发送邮件配置表
- [dbo].[QueuedEmail] 订单邮件序列表,[SentTries]为重试次数,默认尝试3次失败后不再发送。
- [dbo].[ScheduleTask] 定时任务信息表,存储定时任务信息。
四、源码解析
- 根据MVC命名规则,可定位到Nop.Admin.Controllers命名空间,
- 查看ScheduleTaskController的RunNow方法,可跟踪查看到任务调用机制。
- 通过反射类型,采用autofac实例化对象,然后执行。
- 任务实现Nop.Services.Tasks.ITask接口的Execute()方法,如Nop.Services.Messages.QueuedMessagesSendTask。
private ITask CreateTask(ILifetimeScope scope)
{
ITask task = null;
if (this.Enabled)
{
var type2 = System.Type.GetType(this.Type);
if (type2 != null)
{
object instance;
if (!EngineContext.Current.ContainerManager.TryResolve(type2, scope, out instance))
{
//not resolved
instance = EngineContext.Current.ContainerManager.ResolveUnregistered(type2, scope);
}
task = instance as ITask;
}
}
return task;
}
/// <summary>
/// Executes the task
/// </summary>
/// <param name="throwException">A value indicating whether exception should be thrown if some error happens</param>
/// <param name="dispose">A value indicating whether all instances should be disposed after task run</param>
/// <param name="ensureRunOnOneWebFarmInstance">A value indicating whether we should ensure this task is run on one farm node at a time</param>
public void Execute(bool throwException = false, bool dispose = true, bool ensureRunOnOneWebFarmInstance = true)
{
...
//initialize and execute 初始化成功后执行任务
var task = this.CreateTask(scope);
if (task != null)
{
this.LastStartUtc = DateTime.UtcNow;
if (scheduleTask != null)
{
//update appropriate datetime properties
scheduleTask.LastStartUtc = this.LastStartUtc;
scheduleTaskService.UpdateTask(scheduleTask);
}
task.Execute();
this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow;
}
}
catch (Exception exc)
{
this.Enabled = !this.StopOnError;
this.LastEndUtc = DateTime.UtcNow; //log error
var logger = EngineContext.Current.ContainerManager.Resolve<ILogger>("", scope);
logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", this.Name, exc.Message), exc);
if (throwException)
throw;
} if (scheduleTask != null)
{
//update appropriate datetime properties
scheduleTask.LastEndUtc = this.LastEndUtc;
scheduleTask.LastSuccessUtc = this.LastSuccessUtc;
scheduleTaskService.UpdateTask(scheduleTask);
} //dispose all resources
if (dispose)
{
scope.Dispose();
}
}
五、技术解析
- Autofac的依赖注入
- 反射
最新文章
- React 入门实例教程(转载)
- 【转】家庭wifi覆盖指导
- Bourbon – 简单轻量的 Sass 混入(Mixins)库
- lightoj 1022
- java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案
- Python3使用PyQt5制作简单的画板/手写板
- 手把手 学习Git
- lr 中cookie的解释与用法
- 新的编辑工具IDE
- 基本环境安装: Centos7+Java+Hadoop+Spark+HBase+ES+Azkaban
- 标准盒模型、IE盒模型
- noip2014无线网络发射器选址
- java中volatile关键字的理解
- LINUX分辨率修改
- Codeforces 96C - Hockey
- Alpha阶段敏捷冲刺④
- 【转发】Webdriver使用自定义Firefox Profile运行测试
- 场景设计以及Manual Scenario和Goal-OrientedScenario的区别
- Javascript prototype 及 继承机制的设计思想
- [PAT] 1147 Heaps(30 分)
热门文章
- kallsyms
- python 基础之第五天
- opencv 知识点笔记
- CF 757E Bash Plays with Functions——积性函数+dp+质因数分解
- Flutter实战视频-移动电商-36.FlutterToast插件使用
- myeclipse 去掉spring特性支持
- HDU2444 【二分图判定+最大匹配】
- CodeForces Canada Cup 2016【A,B,C,D】
- bzoj 3158: 千钧一发【最小割】
- [Xcode 实际操作]九、实用进阶-(6)在Swift文件中调用Object-C的类和方法