一、功能简介

用户购买物品生成订单后,系统将发送邮件提醒给用户

二、操作步骤

  1. 后台配置一个系统的默认发送邮箱
  2. 启动定时任务,这里包括多个任务,只需要启动邮件任务
  3. 查看邮件发送情况

三、数据库分析

  1. [dbo].[Log] 系统日志表,可查看邮件发送失败的异常信息
  2. [dbo].[EmailAccount] 系统发送邮件配置表
  3. [dbo].[QueuedEmail] 订单邮件序列表,[SentTries]为重试次数,默认尝试3次失败后不再发送。
  4. [dbo].[ScheduleTask] 定时任务信息表,存储定时任务信息。

四、源码解析

  1. 根据MVC命名规则,可定位到Nop.Admin.Controllers命名空间,
  2. 查看ScheduleTaskController的RunNow方法,可跟踪查看到任务调用机制。
    1. 通过反射类型,采用autofac实例化对象,然后执行。
    2. 任务实现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();
}
}

五、技术解析

  1. Autofac的依赖注入
  2. 反射

最新文章

  1. React 入门实例教程(转载)
  2. 【转】家庭wifi覆盖指导
  3. Bourbon – 简单轻量的 Sass 混入(Mixins)库
  4. lightoj 1022
  5. java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案
  6. Python3使用PyQt5制作简单的画板/手写板
  7. 手把手 学习Git
  8. lr 中cookie的解释与用法
  9. 新的编辑工具IDE
  10. 基本环境安装: Centos7+Java+Hadoop+Spark+HBase+ES+Azkaban
  11. 标准盒模型、IE盒模型
  12. noip2014无线网络发射器选址
  13. java中volatile关键字的理解
  14. LINUX分辨率修改
  15. Codeforces 96C - Hockey
  16. Alpha阶段敏捷冲刺④
  17. 【转发】Webdriver使用自定义Firefox Profile运行测试
  18. 场景设计以及Manual Scenario和Goal-OrientedScenario的区别
  19. Javascript prototype 及 继承机制的设计思想
  20. [PAT] 1147 Heaps(30 分)

热门文章

  1. kallsyms
  2. python 基础之第五天
  3. opencv 知识点笔记
  4. CF 757E Bash Plays with Functions——积性函数+dp+质因数分解
  5. Flutter实战视频-移动电商-36.FlutterToast插件使用
  6. myeclipse 去掉spring特性支持
  7. HDU2444 【二分图判定+最大匹配】
  8. CodeForces Canada Cup 2016【A,B,C,D】
  9. bzoj 3158: 千钧一发【最小割】
  10. [Xcode 实际操作]九、实用进阶-(6)在Swift文件中调用Object-C的类和方法