在吉日嘎拉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:// ...
随机推荐
- Web语义化
在昨天和做SEO的同学聊了一会儿,当然我没有学会搜索引擎优化的技巧和知识,但在此之前一直对HTML5中header.footer.sidebar.article等标签嗤之以鼻,觉得这个和div没有什么 ...
- Linux网路编程系列-网络I/O模型
应用程序从网络中拿数据,要经历两个阶段:1.等待数据准备好-分组到达,被拷贝到内核缓冲区,组装数据报:2.数据从内核缓冲区拷贝至用户态应用程序的缓冲区.Unix下五个I/O模型: 阻塞I/O: 进程调 ...
- 自制操作系统 (三) 从启动区执行操作系统并进入C世界
qq:992591601 欢迎交流 2016.04.03 2016.05.31 2016.06.29 这一章是有些复杂的,我不太懂作者为什么要把这么多内容都放进一天. 1读入了十个柱面 2从启动区执行 ...
- Redis集群~StackExchange.Redis(10月6号版1.1.608.0)连接Twemproxy支持Auth指令了
回到目录 对于StackExchange.Redis这个驱动来说,之前的版本在使用Proxy为Twemproxy代理时,它是不支持Password属性的,即不支持原始的Auth指令,而我也修改过源代码 ...
- .NetCore~框架版本号不同引起dotnet不能run它
对于.netCore来说,今年已经推出了正式版,这要求使用vs2015的开发者需要升级到beta3版,而如果使用老版VS开始的.netCore应用程序,它的架构版本将为是测试版"versio ...
- js 事件
事件:一般用于浏览器与用户操作进行交互 js事件的三种模型:内联模型.脚本模型.DOM2模型 内联模型:事件处理函数是HTML标签的属性 <input type="button&quo ...
- 手机页面的 HTML<meta> 标签使用与说明
name="viewport" 设置窗口(网页可绘制的区域) width="device-width" 应用宽与屏幕的宽一样的 (height同width) i ...
- Entity Framework 5中应用表值函数进行Linq查询
Entity Framework 5引入了表值函数(Table-Valued Functions TVFs).表值函数的返回类型是一个Table类型,可用在SQL查询语句中.最简单的表值函数,读取客户 ...
- IL指令速查
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- Oracle Concept
1. Truncate Truncate是DDL命令.表的物理位置是保存在数据字典中表的定义的一部分.首次创建时,在数据库的数据文件内给表分配了一个固定大小的空间.这就是所谓的区间并且为空.那么当插入 ...