在吉日嘎拉DotNet.WebForm中使用FluentScheduler调度任务
有些用户一直说系统发送的邮件一直收不到,投诉系统不正常,这时候怎么洗刷冤屈呢?将发送的每一封Email都保存到数据库中,并记录发送的日志,让用户无话可说。
自己创建3个表:
- MessageFailed - 失败记录(超过5次发送失败就保存到这里)
- MessageQueue - 信息队列 (成功了就放MessageSucceed,失败5次就保存到MessageFailed)
- MessageSucceed - 成功记录
使用FluentScheduler,直接在Web端调度,省去Windows服务程序。

FluentScheduler
Automated job scheduler with fluent interface.
相关代码,我是用了IRegisteredObject,避免Application Pool和进程重启造成的Job中断等异常:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace DotNet.Business
{
using DotNet.Model;
using DotNet.Business;
using DotNet.Utilities; using FluentScheduler;
using System.Web.Hosting; #region MessageRegistry
public partial class MessageRegistry : Registry
{
public MessageRegistry()
{
//不允许重复进入
NonReentrantAsDefault(); // Schedule an IJob to run at an interval
Schedule<MessageJob>().NonReentrant().ToRunNow().AndEvery().Seconds(); //// Schedule an IJob to run once, delayed by a specific time interval
//Schedule<MessageJob>().ToRunOnceIn(5).Seconds(); //// Schedule a simple job to run at a specific time
//Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15); //// Schedule a more complex action to run immediately and on an monthly interval
//Schedule<MessageJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0); //// Schedule multiple jobs to be run in a single schedule
//Schedule<MessageJob>().AndThen<MessageJob>().ToRunNow().AndEvery(5).Minutes();
}
}
#endregion #region MessageJob
public partial class MessageJob : BaseManager, IBaseManager, IJob, IRegisteredObject
{
private readonly object _lock = new object(); private bool _shuttingDown; public MessageJob()
{
//Register this job with the hosting environment.
//Allows for a more graceful stop of the job, in the case of IIS shutting down.
HostingEnvironment.RegisterObject(this);
} public void Execute()
{
lock (_lock)
{
if (_shuttingDown)
return; //Do work, son!
new MessageQueueManager(this.UserInfo).Resend();
}
} public void Stop(bool immediate)
{
//Locking here will wait for the lock in Execute to be released until this code can continue.
lock (_lock)
{
_shuttingDown = true;
} HostingEnvironment.UnregisterObject(this);
} }
#endregion
}
调度的时候,在Global.asax中调度如下:
//FluentScheduler任务调度
FluentScheduler.JobManager.Initialize(new DotNet.Business.MessageRegistry());
Message相关的代码,MessageFailed中的邮件发送Error的记录暂未实现:
#region 重新发送消息
/// <summary>
/// 重新发送消息
/// </summary>
/// <param name="id">主键</param>
/// <returns>是否成功</returns>
public bool Resend(MessageQueueEntity entity, int maxFailCount = )
{
bool result = false;
if (entity.MessageType.ToLower().Contains("mail"))
{
if (MailUtil.Send(entity.Recipient, entity.Subject, entity.Body))
{
//发送成功,移动数据到MessageSucceed表
MessageSucceedEntity entitySuccesed = new MessageSucceedEntity();
entitySuccesed.MessageType = entity.MessageType;
entitySuccesed.Recipient = entity.Recipient;
entitySuccesed.Subject = entity.Subject;
entitySuccesed.Body = entity.Body;
entitySuccesed.CreateOn = entity.CreateOn;
new MessageSucceedManager(this.UserInfo).Add(entitySuccesed);
//删除MessageQueue表中的数据
//this.Delete(entity.Id);
this.DeleteObject(entity.Id);
result = true;
}
else
{
//更新MessageQueue表中的失败次数
entity.FailCount = entity.FailCount + ;
this.UpdateObject(entity);
if (entity.FailCount >= maxFailCount)
{
//发送失败超过5次,移动数据到MessageFailed表
MessageFailedEntity entityFailed = new MessageFailedEntity();
entityFailed.MessageType = entity.MessageType;
entityFailed.Recipient = entity.Recipient;
entityFailed.Subject = entity.Subject;
entityFailed.Body = entity.Body;
entityFailed.FailCount = entity.FailCount;
entityFailed.CreateOn = entity.CreateOn;
//entityFailed.Error = "";
new MessageFailedManager(this.UserInfo).Add(entityFailed);
//删除MessageQueue表中的数据
//this.Delete(entity.Id);
this.DeleteObject(entity.Id);
result = false;
}
result = false;
} }
return result;
}
#endregion #region 重新发送所有队列
/// <summary>
/// 重新发送所有队列
/// </summary>
/// <returns>发送成功数量</returns>
public int Resend(int maxFailCount = )
{
int result = ;
//每次发一封,避免超时,任务不停启动而listEntity并未重新获取
List<MessageQueueEntity> listEntity = this.GetList<MessageQueueEntity>(, MessageQueueEntity.FieldId);
foreach (var entity in listEntity)
{
if (this.Resend(entity, maxFailCount))
{
result++;
}
} return result;
}
#endregion
因为这个后台Job的调度是邮件发送,其实MVC的项目也可以直接用上述代码。
在吉日嘎拉DotNet.WebForm中使用FluentScheduler调度任务的更多相关文章
- 吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展
bug所在位置:DotNet.Business\Utilities\BaseManager.GetDataTableByPage.cs的函数 public virtual DataTable GetD ...
- C#开发中Windows域认证登录2(扩展吉日嘎拉GPM系统)
原文地址:http://www.cuiwenyuan.com/shanghai/post/Windows-AD-Logon-Intergrated-into-Jirigala-GPM-DotNet-B ...
- C#开发中Windows域认证登录2016(扩展吉日嘎拉GPM系统V4.2)
2013年搞公司的OA时,为了统一用户登录,将Windows AD的用户和OA的账号对接,OA用户名的规则就是使用Windows AD的用户名,格式举例:Troy.Cui,原理就是先进行域服务器的认证 ...
- 基于吉日嘎拉的通用权限管理Webform版老界面bug修复
虽然弄了新界面<基于吉日嘎底层架构的通用权限管理Web端UI更新:参考DTcms后台界面>,但老界面的一点菜单显示的问题还是让我这种强迫症揪心,终于今晚可以美美的睡觉了. 老代码用了Ses ...
- 吉日嘎拉C#快速开发平台V4.0到V4.2升级记
目前我用的版本是4.0的,也有近2年没更新了,狠了狠心升级一下,没想到真的行动起来,也没那么难! 用了3天时间,将吉日嘎拉的代码升级到了4.2版本,并让原来的DotNet.WebApplication ...
- webform 中使用ajax
常用的方式有 js –> WebService , js->*.ashx, js->WebAPI, js->MVC Controller->Action. 前两种就不说 ...
- 基于吉日嘎拉的OA协同办公模块重写
这一个月的业余时间主要是在忙这个重构的事情,将吉日嘎拉自带的文档管理.公司公告.留言板.通讯录.周任务.考勤,全部重新建表,重构代码和UI. 目前根据中小企业常用的日常办公需要,搞定了公告栏.任务中心 ...
- webform中使用webapi,并且使用autofac
private void AutofacIoCRegister() { HttpConfiguration config = GlobalConfiguration.Configuration; if ...
- 【Ext.Net学习笔记】01:在ASP.NET WebForm中使用Ext.Net
Ext.NET是基于跨浏览器的ExtJS库和.NET Framework的一套支持ASP.NET AJAX的开源Web控件,包含有丰富的Ajax运用,其前身是Coolite. 下载地址:http:// ...
随机推荐
- 在Grunt task中集成Protractor
Protractor是专为AngularJS应用程序编写的UI自动化测试框架.前端构建有很多构建工具,比如Grunt.Gulp等.一般我们会把这些构建工具作为集成集成的脚本执行工具.所以如果把Prot ...
- AngularJS入门教程1--配置环境
首先需要下载AngualrJS,下载地址 https://angularjs.org/ 官方网站提供2种下载使用AngularJS方法: 1. 去GitHub下载 ,点击按钮会跳转到GitHub页面, ...
- EF架构~在global.asax里写了一个异常跳转,不错!
回到目录 一般地,网站出现异常后,我们会通过设置web.config的方法来实现友好页的显示,这个方法比较常用,但捕捉的信息不是很具体,在程序测试阶段,我们可以通过global.asax来实现友好的, ...
- 第二天 Linux常见命令
复习: 判断题 1.fedora.redhat.Centos.suse.ubuntu.都是常见的linux 2./分区.swap分区./boot分区都是linux的必须分区 3./dev/sda5在l ...
- 2013 duilib入门简明教程 -- 总结 (20)
duilib的入门系列就到尾声了,再次提醒下,Alberl用的duilib版本是SVN上第个版本,时间是2013.08.15~ 这里给出Alberl最后汇总的一个工程,戳我下载,效 ...
- iOS--------坐标系统(UIView的frame、bounds跟center属性)
1.概要翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下: frame:描述当前视图在其父视图中的位置和大小. bounds:描述当前视图在其自身坐标系统中的位置和大小. center:描述 ...
- Android笔记——Android框架
本篇将站在顶级的高度--架构,来看android.我开篇就说了,这个系列适合0基础的人且我也是从0开始按照这个步骤来 学的,谈架构是不是有点螳臂挡车,自不量力呢?我觉得其实不然,如果一开始就对整个an ...
- linker command failed with exit code 1 (use -v to see invocation)
背景:用U盘从另一台电脑考过来后,出现错误 linker command failed with exit code 1 (use -v to see invocation) 出现这种情况很可能是,项 ...
- 快速入门系列--MVC--03控制器和IOC应用
Asp.net MVC也接触好久了,但由于自己一直主要负责后台,尤其是数据库方面的工作对于该框架并没有一个很好的了解,尤其是蒋金楠大师的ASP.NET MVC4框架剖析一书都买了2年多了,真正认真看过 ...
- ssh(sturts2_spring_hibernate) 框架搭建之spring
一.spring总结: ⑴.spring是一个轻量级的JAVA开发框架,主要的作用是用来管理实例(可以解决JAVA类中new对象的问题,节省内存资源.)和降低代码之间的耦合性,促进代码模块化. ⑵.促 ...